#!/bin/sh
#******************************************************************************
#                       ISTP LZPR SOFTWARE
#               Property of the U.S. Government
#                       NASA/GSFC/Code 560
#******************************************************************************
#
# UNIT NAME: dlog
#
# PURPOSE: To automatically dump and truncate DPS software error logs
#
# INVOCATION METHOD: dlog [-hft] [-n #files] [-k #days to keep]
#                         [-a archive] [-d logdir] [-c command]
#
# CHANGE HISTORY:
# Author        Change ID  Build/Release   Date      Description of Change
# ------        ---------  ------------- ---------   ---------------------
# R. Wiechert   ISTP       R1.2          04/22/98    Enhancement of PacorII code
# R. Wiechert   ISTP       R1.2A         05/06/98    Enhancements for cron usage
# R. Wiechert   ISTP       R1.3          06/16/98    Enhancements for -c option
# R. Wiechert   ISTP       R1.3          06/30/98    Added -k option
#
# NOTES:
# see help section below
#
#******************************************************************************
# default options
date=`date "+%Y%j%H%M%S" 2>/dev/null`
log_file_path="$PACOR2HOME/pdp"
log_file_name=`hostname`-Log
log_table_name=db_EventLog
days_to_keep=1
maxlognum=0
archive=/dev/null
logdir=$HOME/opslog
command=false
error_count=0
file=true
table=true
help=null

# determine runtime options
while getopts hftn:k:a:d:c option
do
   case $option in
      f) file=true;  table=false ;;
      t) file=false; table=true ;;
      n) maxlognum=$OPTARG ;;
      k) days_to_keep=$OPTARG ;;
      a) archive=$OPTARG ;;
      d) logdir=$OPTARG ;;
      c) file=true;  table=false;  command=true;  break ;;
      *) help=true ;;
   esac
done

# validate runtime arguments
shift `expr $OPTIND - 1`
maxlognum=`echo "$maxlognum" | tr -cd "0123456789"`
days_to_keep=`echo "$days_to_keep" | tr -cd "0123456789"`
if [ $# -ne 0 -a "$command" != true ]
then
   help=true
fi

# display help if needed
if [ "$help" = "true" ]
then
more <<END_USAGE_TEXT

USAGE:     dlog [-hft] [-n #files] [-k #days to keep]
                [-a archive directory] [-d log directory]
                [-c command]

PURPOSE:   Automatically dumps software error logs into the
           specified directory, truncating original log files
           and tables once they have been preserved.

OPTIONS:   -h      display help screen
           -f      dump log files only
           -t      dump log tables only
           -n      preserve recent #files
           -k      preserve #days in tables
           -a      specify archive directory
           -d      specify log directory
           -c      specify log command

SYNOPSIS:  The traditional DPS software error log files and
           tables are dumped into the specified log directory,
           preserving a specified number of old dump files in
           FIFO fashion.  By default, dump files are located
           in the opslog directory without preserving files.

           The -k option may be used to specify the number of
           days to be preserved in the log tables in addition
           to dumping those days into the log directory.  This
           may result in overlaps between the dump files.  By
           default 1 day of table information is preserved.
           Table deletions may fail to due lack of rollback
           resources when a nonzero -k option is specified.

           The -a option may be used to specify an archive
           directory into which the current log file should
           be copied.  The log file extension is determined
           by executing date to obtain a YYYYDDDHHMMSS string.
           This ensures that log files are sorted and unique.

           The -c option may be used to specify log commands
           for the purpose of generating additional log files.
           The parameters following the -c option are taken
           as a command to be executed in order to generate a
           "command-Log" file within the specified directory.
           For example, in order to generate and dump the 7
           most recent invocations of the command lzpshow -m:
           dlog -n7 -c lzpshow -m

NOTES:     A valid mission environment is required in order
           to execute dlog properly.  Be sure to setup the
           mission environment when using cron to run dlog.
           For example, one might schedule a daily cron job
           using dlog to dump the DPS software error logs,
           always preserving the 7 most recent files online:
           csh -fc "source .login; source .cshrc; dlog -n7"

END_USAGE_TEXT
exit 9
fi

# validate log directory
if [ ! -d $logdir -o ! -x $logdir -o ! -r $logdir -o ! -w $logdir ]
then
   echo "dlog: failed to access $logdir"
   exit 8
fi

# parse and execute command
if [ "$command" = true ]
then
   command="$*"
   log_file_path="$logdir"
   log_file_name=`basename $1`"-Log"
   eval "$command" 1>$log_file_path/$log_file_name 2>&1
fi

# truncate file
if [ "$file" = "true" ]
then
   echo ""
   echo "Dumping $log_file_name in $logdir"
   lastlog=`expr $maxlognum + 0 2>/dev/null`
   while [ "$lastlog" -a "$lastlog" -gt "0" ]
   do
      thislog=`expr $lastlog - 1 2>/dev/null`
      /usr/bin/mv $logdir/$log_file_name.$thislog $logdir/$log_file_name.$lastlog 2>/dev/null
      lastlog="$thislog"
   done
   /usr/bin/cp -p $log_file_path/$log_file_name $logdir/$log_file_name.0 2>/dev/null
   if [ $? -ne 0 ]
   then
      /usr/bin/rm $logdir/$log_file_name.0 2>/dev/null
      echo "cp: failed to access $log_file_path/$log_file_name"
      echo "cp: failed to access $log_file_path/$log_file_name" > $logdir/$log_file_name.0
      error_count=`expr $error_count + 1`
   elif [ -d "$archive" ]
   then
      echo "Archiving $log_file_name.$date"
      /usr/bin/cp -p $logdir/$log_file_name.0 $archive/$log_file_name.$date 2>/dev/null
   fi
   echo "Truncating $log_file_name"
   /usr/bin/cp /dev/null $log_file_path/$log_file_name 2>/dev/null
fi

# truncate table
if [ "$table" = "true" ]
then
   echo ""
   echo "Dumping $log_table_name in $logdir"
   lastlog=`expr $maxlognum + 0 2>/dev/null`
   while [ "$lastlog" -a "$lastlog" -gt "0" ]
   do
      thislog=`expr $lastlog - 1 2>/dev/null`
      /usr/bin/mv $logdir/$log_table_name.$thislog $logdir/$log_table_name.$lastlog 2>/dev/null
      lastlog="$thislog"
   done
   if [ "$days_to_keep" -gt "0" ]
   then
      truncate_table="delete from $log_table_name where EVENTDATE <"
      truncate_table="$truncate_table (select max(EVENTDATE) - $days_to_keep"
      truncate_table="$truncate_table from $log_table_name)"
   else
      truncate_table="truncate table $log_table_name"
   fi
sqlplus `decrypt.sql $PICSHOME` 1>/dev/null 2>/dev/null << ENDORACLEARGS
   column MESSAGENUM format 99999999
   column MESSAGEID format 99999999
   column ACQUISITIONSESSIONID heading SESSIONID format 99999999
   column PRODUCTID format 99999999
   column PARM0 format A32
   column PARM1 format A32
   column PARM2 format A32
   set pagesize 1000
   set linesize 160
   spool $logdir/$log_table_name.0
   select MESSAGENUM, EVENTDATE, MESSAGEID,
   ACQUISITIONSESSIONID, PRODUCTID, PARM0, PARM1, PARM2
   from $log_table_name order by MESSAGENUM;
   spool off
   $truncate_table;
   commit;
ENDORACLEARGS
   if [ $? -ne 0 ]
   then
      /usr/bin/rm $logdir/$log_table_name.0 2>/dev/null
      echo "sqlplus: failed to access $ORACLE_SID database"
      echo "sqlplus: failed to access $ORACLE_SID database" > $logdir/$log_table_name.0
      error_count=`expr $error_count + 1`
   elif [ -d "$archive" ]
   then
      echo "Archiving $log_table_name.$date"
      /usr/bin/cp -p $logdir/$log_table_name.0 $archive/$log_table_name.$date 2>/dev/null
   fi
   echo "Truncating $log_table_name"
fi

echo ""
exit $error_count
