#!/bin/ksh
#
#############################################################################
##
## iMS monitoring and alarm script
##
## This script is part an example framework of what can be done.
## There are MANY ways that you can take this from here.
##
## File: $RCSfile: check_files,v $
##
## Monitors the build up of checkpoint logfiles sends an alarm if a 
## configurable number of checkpoint logfiles is exceeded.
##
## While this package is provided AS-IS and unsupported, we would appreciate
## your comments, feedback and bug-reports at:
##
##      mailtools-feedback @Sign sun.com
##
#############################################################################

_CURR_WDIR=$(dirname $0)  # Use built in ksh cmds to find out where we are 

Usage () {
    echo "$0\t "
    echo "\t [-S foo <use reporting style foo for alarms> ]"
    exit 
}

while [ $# -gt 0 ]
do
    case "$1" in
        -S)   shift; report_style=$1;;
        -h)   Usage;;
    esac
    shift
done

## include definitions if necessary
if [ -z "$HEALTHMON_TMPDIR" ]
then
    . $_CURR_WDIR/../etc/alarms.cfg
fi

TEST="imta-tlf"

## flags related to status reporting

COLOR="green"
FWARN="3"        # GO YELLOW AT THIS LEVEL
FPANIC="5"       # GO RED AND PAGE AT THIS LEVEL
MAXDURATION="1200" # PANIC Delay

timestamp="`perl -e 'print time'`"

# get the total number of log files
total_logfiles=`find $MBOXLIST_PATH -type f -name "log.00*" |grep -v "log*/" | wc -l |awk '{print $NF}'` 

if [ "$total_logfiles" -ge "$FPANIC"  ]
then
    if [ ! -f "$HEALTHMON_TMPDIR/.lastpanic.timestamp" ]
    then
	echo $timestamp >  $HEALTHMON_TMPDIR/.lastpanic.timestamp
	COLOR="yellow"
	MARKLEVEL="WARNING"
        _subject="`date` - Pending Panic for CheckPoint logfile count ($total_logfiles) on $MACHINEDOTS at $MARKLEVEL level"
    else
	lasttime="`cat $HEALTHMON_TMPDIR/.lastpanic.timestamp`"
	elapsedtime="`expr $timestamp - $lasttime`"

	if test  "$elapsedtime" -ge "$MAXDURATION" 
	then
		COLOR="red"
		MARKLEVEL="FPANIC"
                _subject="`date` - CheckPoint logfile count ($total_logfiles) on $MACHINEDOTS at $MARKLEVEL level"
	else
		COLOR="yellow"
		MARKLEVEL="WARNING"
                _subject="`date` - CheckPoint logfile count ($total_logfiles) on $MACHINEDOTS at $MARKLEVEL level"
	fi
    fi
else 
    rm -f $HEALTHMON_TMPDIR/.lastpanic.timestamp

    if [ "$total_logfiles" -ge "$FWARN"  ]
    then
        MARKLEVEL="WARNING" 
        COLOR="yellow"
        _subject="`date` - CheckPoint logfile count ($total_logfiles) on $MACHINEDOTS at $MARKLEVEL level"
     else
        _subject="`date` $TEST - Checkpoint logfile count OK on $MACHINEDOTS"
        COLOR="green"
     fi
fi

_full_report="
--- ( ls ~/store/mboxlist/log.00* ) ---

Number of CheckPoint Log files: $total_logfiles
"
print_${report_style}_report

## end of script
