#!/bin/sh
#******************************************************************************
#                              ISTP LZP SOFTWARE
#                      Property of the U.S. Government
#                              NASA/GSFC/Code 500
#******************************************************************************
#
#    UNIT NAME:  lzp_verify
#
#    PURPOSE:  Verify DPS hash indexes for the specified APIDs
#
#    INVOCATION METHOD:  lzp_verify [-m MissionId] [-s MaxSequence]
#                                   [-p HashDisk] [-k] <apid list>
#
#    ARGUMENT LIST:
#    Name            Type            Use  Description
#    --------------  --------------  ---  -----------
#    MissionId       number           I   mission id
#    MaxSequence     number           I   max du seqence number
#    HashDisk        path string      I   location of hash disk
#    apid list       number(s)        I   application ids to verify
#
#    EXTERNAL VARIABLES:
#    Source          Name            Type       Use  Description
#    --------------  --------------  ---------  ---  -----------
#    Bourne-Shell    $HOME           path        I   user's home directory
#    Bourne-Shell    $?              string     I/O  success = 0, else failure
#
#    EXTERNAL UNIT/MACRO/PROCEDURE/FUNCTION REFERENCES:
#    Name            Purpose
#    --------------  ----------------
#    none
#
#    ABNORMAL TERMINATION CONDITIONS, ERROR MESSAGES:
#    $? <> 0 when any stage of processing fails
#
#    ASSUMPTIONS, CONSTRAINTS, RESTRICTIONS:
#    none
#
#    DEVELOPMENT HISTORY:
#    Author           Change-ID        Release  Date      Description of Change
#    ---------------  ---------------  -------  --------  ---------------------
#    R. Wiechert      ISTP             R1.2A    05/20/98  Original code
#
#    NOTES:
#    MissionId, MaxSequence, HashDisk are extremely mission dependent.
#    Invalid configuration of these parameters will cause false alarms.
#    See embedded help section for more details.
#
#******************************************************************************
corrupt=false
kill=echo
help=false

# add process names here (like "CGS|RM|...|...")
kill_list="CGS"

# determine username and default options
username=`/usr/ucb/whoami 2>/dev/null`
if [ $? = 0 ]
then
   hashdisk=$HOME/data2
   case $username in
      geotail)  mission=24; sequence=16383 ;;
      geodev)   mission=24; sequence=16383 ;;
      wind)     mission=25; sequence=255 ;;
      winddev)  mission=25; sequence=255 ;;
      polar)    mission=26; sequence=255 ;;
      polardev) mission=26; sequence=255 ;;
      *)        mission=0;  sequence=0 ;;
   esac
else
   echo "lzp_verify:  unable to determine username"
   exit 9
fi

# determine runtime options
while getopts m:s:p:kh option
do
   case $option in
      m) mission=$OPTARG ;;
      s) sequence=$OPTARG ;;
      p) hashdisk=$OPTARG ;;
      k) kill=kill ;;
      h) help=true ;;
   esac
done

# validate runtime options
shift `expr $OPTIND - 1`
if [ $# = 0 -o "$help" = true ]
then
more << END_USAGE_TEXT

USAGE:     lzp_verify [-m MissionId] [-s MaxSequence]
                      [-p HashDisk] [-k] <apid list>

PURPOSE:   Verify DPS hash indexes for the specified APIDs

OPTIONS:   -h      display help screen
           -m      specify mission id
           -s      specify maximum sequence count
           -p      specify hash disk directory
           -k      kill processes in case of problems

SYNOPSIS:  The verfiyhash utility is invoked in order to
           verify DPS hash indexes for the specified APIDs.
           Although the correct mission, sequence, and hash
           disk parameters are selected by default based on
           the current user, the -msp options may be used
           to override these defaults.  The -k option is
           used to kill DPS processes in the event that the
           hash indexes are found to be corrupt.  The CGS
           process is typically killed in order to prevent
           products based on corrupt indexes from being
           generated and transmitted to the CDHF.

NOTES:     In the event that problems are detected and the
           CGS task is killed, the operator should rerun
           verify hash manually in order to verify that the
           problems are persistant.  If no problems are
           detected, use the DPS resource monitor window to
           restart the CGS task.  If problems are detected,
           use lzp_system stop to halt the system, execute
           hashinit, use lzp_system start to restart the
           system, and manually pre-process DPS sessions.

           If the -msp options are not set correctly, this
           script may detect false corruption.  This script
           may also detect false corruption if executed
           while hash indexes are being updated.

           When calling this script as a cron job, be sure
           to setup the proper environment by sourcing the
           user .login and .cshrc files.  The following
           example shows how to verify apids 1, 2, and 99,
           killing the CGS process when problems are found:
           lzp_verify -k 1 2 99
           
END_USAGE_TEXT
exit 9
fi

# verify specified apids
if [ -d $hashdisk ]
then
   while [ $# -gt 0 ]
   do
      problems=`verifyhash -m $mission -s $sequence -p $hashdisk -a $1 | grep -c bogus`
      echo "verifyhash -m $mission -s $sequence -p $hashdisk -a $1 found $problems errors"
      if [ "$problems" != "0" ]
      then
         corrupt=true
      fi
      shift 1
   done
else
   echo "lzp_verify:  path does not exist:  $hashdisk"
   exit 9
fi

# kill processes if problems
if [ "$corrupt" = "true" ]
then
   pid_list=`/usr/5bin/ps -eo "user pid comm" | grep "$username " \
   | egrep "$kill_list" | grep -v grep | awk '{ print $2 " " }'`
   if [ "$pid_list" ]
   then
      echo "killing $kill_list using SIGTERM signal"
      eval $kill $pid_list
   else
      echo "processes already killed"
   fi
   echo ""
   echo "The CGS process has been killed in order to prevent"
   echo "automatic products from being created based on a"
   echo "possibly corrupt hash index."
   echo ""
   echo "Please verify hash tables manually to ensure that the"
   echo "problem still exists.  Use one of the same commands"
   echo "which had failed above..."
   echo ""
   echo "If the problem STILL exists then use lzp_system stop"
   echo "in order to stop the software, execute hashinit, and"
   echo "use lzp_system start to start the software again."
   echo "You do NOT have to replay any sessions;  you only"
   echo "have to pre-process sessions which should still be"
   echo "included in products."
   echo ""
   echo "If the problem NO LONGER exists then this was a false"
   echo "alarm.  Use the DPS resource monitor window in order"
   echo "to restart the CGS process only.  No further action"
   echo "is required."
   echo ""
   echo "In any case, please report this to the developers."
   echo ""
   exit 1
fi

# no problems
exit 0
