#! /usr/bin/sh
set -xva
# ##############################################################
#
# Script name:  exsmsjobs.sh.sms
# Script description:  This scripts setup the SMS job history
#                      program.
#
# Author:   David Ma         Org: NP11       Date: 2004-12-04
#
# Abstract: It generates all environment variables, and data files.
#           It also produces a sms job history files in the
#           /com/web/prod directory for the jobview program.  
#           It does not generate such job history file if the 
#           machine is switched  anytime of the week. 
#
# Script history log:
# 2004-12-04  This is created for the new cirrus and stratus CCS.
# 2002-10-09  Implemented into production on IBM
# 2004-02-17  Changed to get the file everyday and 
#               store the file for 1 week.
#
################################################################
i=0
qmach=`hostname | cut -c 1`
smain="getsmsjobs.sh"
smsrun="smsjobs"
ddat="LastSMSJobs.dat"
track_log="track_sms_machine"
adat="Aborted.dat"
#smshost="smspmb01 smspmb02"
smslog="/sms/rundir/log/smslog1"
rsmslog="/sms/rundir/log/smslog2"
smstodaylog="/sms/rundir/sms.log"
csmshost=`echo ${SMSNODE} | cut -d "." -f 1`
lsmshost=`cat ${COMIN}/${track_log} | cut -d " " -f 1`
lsmsday=`cat ${COMIN}/${track_log} | cut -d " " -f 2`
tmpweb="/com/web/prod/ptmp/web"
fnode="Node_file"
smsrunlog="SMS_RUN_LOG"
tPDYm1=`${utilscript}/finddate.sh $PDY s-1`
tPDY12=`${utilscript}/finddate.sh $PDY s-12 | cut -d " " -f 12 ` 
tPDY11=`${utilscript}/finddate.sh $PDY s-11 | cut -d " " -f 11 `
tPDY10=`${utilscript}/finddate.sh $PDY s-10 | cut -d " " -f 10 `
cd ${DATA}
cp ${COMOUT}/${ddat} ${COMOUT}/${smsrunlog}/SMS${tPDYm1}.dat 
cp ${tmpweb}/${fnode} Node_file_prod
prodhost=`hostname | cut -c1`
quser="nwprod"
if [ "${prodhost}" = "c" ]
then
  othermach="stratus"
else
  othermach="cirrus"
fi
ping -c 3 ${othermach}
pingerr=$?

if [ $pingerr -eq 0 ]
then
  scp ${quser}@${othermach}:${tmpweb}/${fnode} Node_file_dev
else
  echo "${othermach} is not available"
fi
#
echo
smsmsg="..."
date -u
#
# Get most recent sms log information from active SMS machine
#
qtime=`date -u`
qdate=`echo $qtime | cut -d " " -f 1`
case $qdate in
  Mon ) qndate=1;;
  Tue ) qndate=2;;
  Wed ) qndate=3;;
  Thu ) qndate=4;;
  Fri ) qndate=5;;
  Sat ) qndate=6;;
  Sun ) qndate=7;;
    * ) qndate=8;;
esac
qhr=`echo $qtime | cut -d " " -f 4 | cut -d ":" -f 1`
qmm=`echo $qtime | cut -d " " -f 4 | cut -d ":" -f 2`
qsec=`echo $qtime | cut -d " " -f 4 | cut -d ":" -f 3`
((qjtime=$qhr*3600+$qmm*60+$qsec))
msg="SMSJOBS $csmshost - BEGIN Xfer smslog $qtime $qjtime $lsmshost"
sh $utilscript/postmsg.sh "$jlogfile" "$msg "
lSMSNODE=${lsmshost}.ncep.noaa.gov
  if [ "${csmshost}" = "${lsmshost}" ]
  then

    ping -c 3 ${SMSNODE}
    pingerr=$?

    if [ $pingerr -eq 0 ]
    then
      scp nwprod@${SMSNODE}:${smslog} $DATA/smslog.dat
      head -30 smslog.dat
      smslogline=`ls -rlt smslog.dat`
      smslogsize=`echo $smslogline | cut -d " " -f 5`
      if [ $smslogsize -gt 400000 ]
      then 
        wc -l smslog.dat
        ls -rlt
        grep active smslog.dat | grep LOG | grep -v suspend | grep -v admin > submit.log
        grep complete:/ smslog.dat | grep LOG | grep -v suspend | grep -v set: | grep -v admin > complete.log
        grep abort smslog.dat | grep ERR | grep -v suspend | grep -v test | grep -v admin > abort.log

# program
        ls -rlt
        qtime=`date -u`
        msg="SMSJOBS $csmshost - End xfer smslog $qtime"
        sh $utilscript/postmsg.sh "$jlogfile" "$msg "

        ${utilexec}/${smsrun}
        qtime=`date -u`
        msg="SMSJOBS $csmshost - End smsrun program $qtime"
        sh $utilscript/postmsg.sh "$jlogfile" "$msg "
        cp ${ddat} ${COMOUT}/${ddat}
        cp ${ddat} ${COMOUT}/${smsrunlog}/SMS${PDY}.dat
        chmod 775 ${COMOUT}/${ddat} ${COMOUT}/${smsrunlog}/SMS${PDY}.dat
        ls -rlt
        grep 10000 ${ddat} > ${adat}
        cat ${adat}
      else
        echo "SMSLOG file is too small"
      fi
    else
      smsmsg=`echo "SMS machine not responding, program terminated without process"`
    fi
  else
    ping -c 3 ${SMSNODE}
    pingerr=$?

    if [ $pingerr -eq 0 ]
    then
      scp nwprod@${SMSNODE}:${smslog} $DATA/smslog.dat
      scp nwprod@${SMSNODE}:${rsmslog} $DATA/lsmslog.dat

      lsize=`ls -l $DATA/lsmslog.dat`
      rsize=`ls -l $DATA/smslog.dat`
      lsize=`echo $lsize | cut -d " " -f 5`
      rsize=`echo $rsize | cut -d " " -f 5`
      ((rsize=$rsize+20000))
      if [ ${lsize} -gt ${rsize} ]
      then
         cp $DATA/lsmslog.dat $DATA/smslog.dat
      fi
#      cursmsdate=`tail -1 smslog.dat | cut -d " " -f 4 | sed 's/]//g'`
       
      head -30 smslog.dat
      smslogline=`ls -rlt smslog.dat`
      smslogsize=`echo $smslogline | cut -d " " -f 5`
      if [ $smslogsize -gt 2000000 ]
      then
        wc -l smslog.dat
        ls -rlt
        grep active smslog.dat | grep LOG | grep -v suspend > submit.log
        grep complete:/ smslog.dat | grep LOG | grep -v suspend | grep -v set: > complete.log
        grep abort smslog.dat | grep ERR | grep -v suspend | grep -v test > abort.log

# program
        ls -rlt
        qtime=`date -u`
        msg="SMSJOBS $csmshost - End xfer smslog $qtime"
        sh $utilscript/postmsg.sh "$jlogfile" "$msg "

        ${utilexec}/${smsrun}
        qtime=`date -u`
        msg="SMSJOBS $csmshost - End smsrun program $qtime"
        sh $utilscript/postmsg.sh "$jlogfile" "$msg "
        cp ${ddat} ${COMOUT}/${ddat}
        cp ${ddat} ${COMOUT}/${smsrunlog}/SMS${PDY}.dat
        chmod 775 ${COMOUT}/${ddat} ${COMOUT}/${smsrunlog}/SMS${PDY}.dat
        ls -rlt
        grep 10000 ${ddat} > ${adat}
        cat ${adat}
      else
        echo "SMSLOG file is too small"
      fi
    else
      smsmsg=`echo "SMS machine not responding, program terminated without process"`
    fi
  fi
((lsmsday=$qndate))
rm -f ${COMOUT}/${smsrunlog}/SMS${tPDY10}.dat ${COMOUT}/${smsrunlog}/SMS${tPDY11}.dat ${COMOUT}/${smsrunlog}/SMS${tPDY12}.dat
echo "${COMOUT}/${smsrunlog}/SMS${tPDY10}.dat file removed"
echo "${COMOUT}/${smsrunlog}/SMS${tPDY11}.dat file removed"
echo "${COMOUT}/${smsrunlog}/SMS${tPDY12}.dat file removed"
echo "${csmshost} ${lsmsday}" > $track_log
cp ${adat} ${tmpweb}/${adat}
cp ${track_log} ${COMOUT}/${track_log}
qtime=`date -u`
qhr=`echo $qtime | cut -d " " -f 4 | cut -d ":" -f 1`
qmm=`echo $qtime | cut -d " " -f 4 | cut -d ":" -f 2`
qsec=`echo $qtime | cut -d " " -f 4 | cut -d ":" -f 3`
((qejtime=$qhr*3600+$qmm*60+$qsec))
((qjtime=$qejtime-$qjtime))
msg="SMSJOBS $csmshost - Program ended $qtime $qejtime $qjtime $smsmsg $lsmshost"
sh $utilscript/postmsg.sh "$jlogfile" "$msg "

jlog=`ls -lrt $jlogfile`
jsize=`echo $jlog | cut -d " " -f 5`
if [ $jsize -gt 750000 ]
then
  rm -f $jlogfile
  touch $jlogfile
fi

