#!/bin/sh

# Version 1.0
# Last modified: September 19, 2012
#
# Gather and upload the output of isi job status -v 
# Helpful for quickly getting details on a job we 
# monitoring without having to get a whole/incremental gather
#
# There are no arguments

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

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

OutputDir='/var/crash/support/pkg'        #Use this if /ifs is not available
mkdir -p "${OutputDir}"                   #Make sure the local-storage output directory exists

OutputPath=${OutputDir}/${OutputFile}

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

# Run isi job status -v command:
CMD="isi job jobs list --format=table --verbose >> ${OutputPath};
isi job events list --verbose >> ${OutputPath}";
echo "${CMD}"
CmdOutput=`eval "${CMD}" 2>&1`
Status=$?

# If the command failed find out why and log it.
case ${Status} in
  "0")
    MSG="The isi job status command Succeeded."
    echo ${MSG}
    log "$0: ${MSG}"
    ;;
  *)
    echo The isi job status command failed
    OutputPath=${OutputDir}/${OutputFile}
    if [ ! -f "$OutputPath" ] ; then
      ErrMsg="isi job status 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 job status 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 --re-upload=${OutputPath}

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