#!/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_sys,v $
##       A script to check on the health of the system.
##
## 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)


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

##############################################################################
## PLEASE DON'T CHANGE THESE
##############################################################################

TEST="imta-sys"
COLOR="green"
OS_TYPE="`uname -s`"
OUTPUT_FILE=$HEALTHMON_TMPDIR/check_system.output.$$
TEMP_FILE=$HEALTHMON_TMPDIR/check_system.temp.$$
SMTP_FILE=$HEALTHMON_TMPDIR/check_system.smtp.$$
INDEX_FILE=$HEALTHMON_TMPDIR/check_system.index.$$
BODY_FILE=$HEALTHMON_TMPDIR/check_system.body.$$
TOTAL_FILE=$HEALTHMON_TMPDIR/check_system.total.$$
LOGFILE=$HEALTHMON_TMPDIR/check_system.LOGFILE.$$

## function to get the value of an option from an option file.
getValue() {
  x=$1
  y=$2
  z=`/bin/grep "^$x=" $y |/bin/awk -F= '{print $2}' | sort | head -1`
  echo $z
}

OptionFile="`getValue IMTA_OPTION_FILE $TAILOR_FILE`"
LOGOPTION="`getValue LOG_PROCESS $OptionFile`"
IMTA_LOGFILE="`getValue IMTA_PRIMARY_LOG $TAILOR_FILE`"

if [ -z "$LOGOPTION" ]; then
	LOGOPTION=0
fi

## function to get a snapshot of message traffic through the system.


###############################################################################
#
# Real work begins
#
###############################################################################

##
## Process the log file to compute accept/deliver rates
##

        touch $OUTPUT_FILE

        if [ -f "$_CURR_WDIR/../lib/logtail" -a -f  "$IMTA_LOGFILE" ]
        then
                OFFSET_FILE="$HEALTHMON_TMPDIR/.IMTA_LOGFILE.offset"
                $_CURR_WDIR/../lib/logtail $IMTA_LOGFILE $OFFSET_FILE >$LOGFILE
                . $_CURR_WDIR/../lib/MTALogParser > $SMTP_FILE

                ## Add the accessrate information

                printf "\nAccessrate:\n\n">> $OUTPUT_FILE
                head -1 $SMTP_FILE >>$OUTPUT_FILE
                grep "^TOTAL" $SMTP_FILE >>$OUTPUT_FILE
                printf "\n">> $OUTPUT_FILE

        fi

## Get the tcp/ip connection information

        $_CURR_WDIR/../lib/tcprate >> $OUTPUT_FILE

##
## Get the dispatcher stats
##

	printf "\n\nDispatcher Statistics:\n\n" >> $OUTPUT_FILE
	$IMSIMTA dispatcher_stats_tty | head -4 >> $OUTPUT_FILE

##
##  disk space usage
##

        list="`grep store.partition.*.nsmsgPath $LOCAL_CONF_FILE | awk '{print $NF}'`"

        printf "\n\nDisk Space Usage:\n" >>$OUTPUT_FILE
        printf "-----------------\n" >> $OUTPUT_FILE
        printf "Disk size        used      avail Use%%  Partition Path\n" >> $OUTPUT_FILE
        printf "----------------------------------------------------------------\n" \
                >> $OUTPUT_FILE

        for fsys in $messaging_disks $list
        do
            df -k $fsys |tail +2 | \
            $AWK -v fsys=$fsys '{ printf("%10s %10s %10s %4s  %s\n", $2,$3,$4,$5,fsys)}' \
                >>$OUTPUT_FILE
        done

	_full_report="
`cat $OUTPUT_FILE`"

	if [ "$COLOR" = "green" ]
	then
		_subject="`date` $TEST - on $MACHINEDOTS OK"
                print_${report_style}_report
	else
		## non green event...
		_subject="`date` $TEST - Problems reported on $MACHINEDOTS"
                print_${report_style}_report
	fi

## cleanup the temp files 

        rm -f $BODY_FILE $TOTAL_FILE $LOGFILE
        rm -f $OUTPUT_FILE $TEMP_FILE $SMTP_FILE $INDEX_FILE

## end of script
