#!/bin/sh

# Version 1.0
# Last modified: September 25, 2012
#
# Gather and upload the contents of \var\Crash
#
# There are no arguments

# Logging
LOGFILE=/var/log/remotesupport.log
log() {
    echo `date +"%FT%"T"%z"` $@ >> ${LOGFILE}
    logger -i "" ${@}
}

# The output filename, in the format that gstools expects:
Prefix='IsilonLogs'
GatherType='VarCrashContents'
TimeTag=`date "+%Y%m%d-%H%M%S"`
Cluster=$(grep "<name>" /etc/ifs/array.xml | awk -F'</?name>' '{print $2}')
Suffix="tgz"
OutputFile="${Prefix}-${GatherType}-${Cluster}-${TimeTag}.${Suffix}"

# The file upload directory:
IFS_OutputDir='/ifs/data/Isilon_Support/pkg'  #Use this if /ifs is available
OutputDir="${IFS_OutputDir}"                  #The default
mkdir -p "${IFS_OutputDir}"                   #Make sure the /ifs/ output directory exists

# SupportIQ output file sym link; used if the isi_gather info FPT upload fails:
WatchDir=/var/crash/SupportIQ/upload/ftp
SymLink=${WatchDir}/${OutputFile}

# Drop error file here if we are unable to proceed with the requested operation:
ErrDir=/var/crash/SupportIQ/upload/web
ErrFile='var-crash-contents-error.log'
ErrPath=${ErrDir}/${ErrFile}
ErrMsg="Cannot perform /var/crash gather on local storage"

# Store will be /ifs for the output and temporary file(s):
Store=${UseIFS}               #default: use /ifs


# Learn whether or not /ifs/ is available:
ls -d /ifs > /dev/null
Status=$?
case ${Status} in
  "0")
    # The /ifs/ directory is readable. Is it mounted?
    DF=`df -l -n /ifs | tail -1 | awk '{print $1 " " $6}'`
    if [ "${DF}" != 'OneFS /ifs' ] ; then
      echo Warning: /ifs is not mounted
	  ErrMsg="/ifs is not mounted.  Cannot use local storage as that is the directory we are getting and it would be recursive!"
      #logging
      log "$0: ${ErrMsg}"
      log "$0: exited with: ${Status} But /ifs not mounted"
      exit 1
    fi
    ;;
  *)
    #The ls command failed; probably because of an I/O error.
    echo Warning: /ifs is not readable
    ErrMsg="/ifs is not readable.  Cannot use local storage as that is the directory we are getting and it would be recursive!"
	#logging
	log "$0: ${ErrMsg}"
	log "$0: exited with: ${Status}. /ifs is not readable"
	exit 1
esac


OutputPath=${OutputDir}/${OutputFile}
 
CMD="tar -zcvf ${OutputPath} /var/crash"
echo "${CMD}"
CmdOutput=`eval "${CMD}" 2>&1`
Status=$?

# If the command failed we assume that is because the log
# upload failed. In that case, we make a sym link in a SupportIQ "watch"
# directory, pointing to the isi_gather_info output file. The SupportIQ Agent
# will see that sym link, and follow it to upload the file to the Enterprise.
# The Enterprise should then upload the file to the Isilon FTP site.
case ${Status} in
  "0")
    MSG="The gather of /var/crash contents command Succeeded."
    echo ${MSG}
    log "$0: ${MSG}"
    ;;
  *)
    echo The var crash contents gather command failed
   
    if [ ! -f "$OutputPath" ] ; then
      ErrMsg="/var/crash contents gather output file $OutputPath does not exist"
      echo "$ErrMsg"
      echo ErrPath is "$ErrPath"
      date > $ErrPath
      echo "$CmdOutput" >> $ErrPath
      echo "$ErrMsg" >> $ErrPath
      #logging
      log "$0: ${ErrMsg}"
      log "$0: exited with: ${Status}"
      exit 1
    fi
    if [ ! -s "$OutputPath" ] ; then
      ErrMsg="isi_gather_info output file $OutputPath is empty"
      echo "$ErrMsg"
      date > $ErrPath
      echo "$CmdOutput" >> $ErrPath
      echo "$ErrMsg" >> $ErrPath
      #logging
      log "$0: ${ErrMsg}"
      log "$0: exited with: ${Status}"
      exit 1
    fi
esac

isi_gather_info --esrs --noftp --re-upload=${OutputPath}


#Success!
log "$0: exited with: ${Status}"
exit 0
