#!/bin/ksh 
#
#       This is OSW Scripts page version.
#       Refer questions to "gs-w osw scripts"
#
#  Version 1.0
#
###################################################3
# program lrgs_check - jdjohnsn 2/28/2006 

# Purpose: run as a cron job hourly (or on any specified schedule) to compare
# current date/time with the last recorded lrgs date/time (as reported by
# 'dbstat'). 
# Sends email to the list of recipients defined in the $TO variable 
# if there is a delay greater than 60 minutes (or the specified
# <time_check_in_minutes>
 
# Usage: lrgs_check satin_queue_name <time_check_in_minutes>
#
# where the satin queue name is mandatory. The optional time difference
# check defaults to 60 minutes, but can be specified on the command line.

function jul_day #
{
echo $1 | awk 'BEGIN {split("31 59 90 120 151 181 212 243 273 304 334 365",mth)}
        {y = substr($0,1,4)
        m = substr($0,5,2)
        d = substr($0,7,2)
        day = mth[m-1] + d
        if (y % 4 == 0 && m > 2) day = day + 1
        if (day < 10) day = "0" day
        if (day < 100) day = "0" day
        print day}'
}

# set list of email recipients
TO=user1@usgs.gov,user2@usgs.gov


# get SATIN queue name to check from command line argument
if [ -z "$1" ]
 then
echo 'An attempt was made to run lrgs_check but no queue name was supplied.\n 
Check your crontab and make sure there is one arguement that is the satin queue name.' | \
/usr/bin/mailx -s "**** ERROR in running lrgs_check" $TO
exit
else
satqueue=$1
fi


# Default time check is 60 minutes, but can be reset on the command line

if [ -n "$2" ]
 then CKTM=$2
else
 CKTM=60
fi


# get LAST time in the SATIN QUEUE database for specified queue

/usr/local/bin/lrgs $satqueue << e_o_f > /dev/null
dbstat > /tmp/dbs$satqueue 
exit
e_o_f


# get lrgs most current jday and time from dbstat
# from formast such as
# Last ->DD30D7A8 at 2006/075 21:26:29
# or
# Last ->10255550       METEOR   1 at 2006/075 21:17:58
#

ljday=`grep 'Last' /tmp/dbs$satqueue | sed -e 's/^.*at //' -e 's/\// /'  | awk 'BEGIN {FS=OFS=" "}{print $2}'`
lhh=`grep 'Last' /tmp/dbs$satqueue | sed -e 's/^.*at //' -e 's/:/ /g' | awk 'BEGIN {FS=OFS=" "}{print $2}'`
lmm=`grep 'Last' /tmp/dbs$satqueue | sed -e 's/^.*at //' -e 's/:/ /g' | awk 'BEGIN {FS=OFS=" "}{print $3}'`


# compute LRGS time in min from 0-1440
((lmin=( $lhh * $CKTM ) + $lmm ))


# get CURRENT jday and time in UTC
cd=`date -u '+%Y%m%d'`
cjday=`jul_day $cd`
chh=`date -u '+%H'`
cmm=`date -u '+%M'`


# compute time in total min from 0-1440
((cmin=( $chh * $CKTM ) + $cmm ))


# check difference in julian days
((DD=$cjday-$ljday))


#####################################################
# if the same julian day, then just compare the difference in minutes

if [ $DD -eq 0 ]
  then
  ((TDIFF = $cmin - $lmin))
  if [ $TDIFF -gt $CKTM ]
     then
     echo 'Difference in Current and LRGS times is '$TDIFF' minutes' | \
     /usr/bin/mailx -s "**** WARNING - QUEUE '$satqueue'  IS $TDIFF MINUTES BEHIND" $TO
  fi
fi
#####################################################
# check for really bad delay of more than one day....
# could happen if SATIN has been down for an extended period and is just
# catching up. 

if [ $DD -gt 1 ]
  then
  echo " THE LRGS DATA IS NOW MORE THAN ONE DAY BEHIND!!!!" | \
  /usr/bin/mailx -s "**** WARNING - ACT NOW - QUEUE '$satqueue' IS $DD DAYS BEHIND" $TO
fi

#####################################################
# and if jday diff is exactly 1 day, then adjust time to compensate and then
# compare times - if > $CKTM minutes different, send warning. 

if [ $DD -eq 1 ]
  then
  ((cmin=$cmin+1440))
  ((TDIFF=$cmin-$lmin))

   if [ $TDIFF -gt $CKTM ]
     then
     echo 'Difference in Current and LRGS times is '$TDIFF' minutes' | \
     /usr/bin/mailx -s "**** WARNING - QUEUE '$satqueue' IS $TDIFF MINUTES BEHIND" $TO
   fi
fi
#####################################################

rm /tmp/dbs$satqueue
