#!/bin/ksh

#############################################################################
##
## Messaging Server queue 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_queues,v $
##       (Bigbrother ext plugin script, based on bb-disk.sh)
##
## Purpose: 
##
##    1. Collect system and queue data. 
##    2. Send an alarm if a the number of messages in the queue is greater
##        than a configurable threshold
##    3. Mail the daily report to a list of recipients 
##
## Operation:
##
##      a. Parses the output of the command 'imsimta qm summarize -held'
##         which looks like:
##
##         ChannelName  Num Queued   Num Held   Size (Kb)   Oldest
##
##      b. Compares each output line to a qm tab file which looks like:
##
##            channelname:warnCOUNT:panicCOUNT:heldCOUNT
##                 tcp_local:3000:4000:200
##                 process:200:300:500:-1 (where -1 means ignore .HELD messages for this channel)
##
## 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)

## decide wether or not a report should be sent.
_sendreport=0

## error codes returned by script

## flags related to queue status reporting
COLOR="green"
REDLINE=""
YELLOWLINE=""
TEST="imta-qm"

## ---- start of main script ----

while [ $# -gt 0 ]
do
    case "$1" in
        -r)             _sendreport=1;;
        -S)             shift; report_style=$1 ;;
        -p)             shift; channel=$1;;
        -w)             shift; warnlimit=$1;;
        -c)             shift; paniclimit=$1;;
        -H)             shift; heldwarn=$1;;
        -h)             Usage;;
    esac
    shift
done

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

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

## variables used in the script
## please change with care
_msgfile=$HEALTHMON_TMPDIR/alarmmsg.$$
_qmout=$HEALTHMON_TMPDIR/qmout.$$

## Starting conditions
COLOR="green"
REDLINE=""
YELLOWLINE=""
HELDLINE=""
HELDFOUND="FALSE"
_full_report=""

## core command to issue
QMOPTIONS="qm summarize -held -nohead -notrail $channel"
QMCMD="$IMSIMTA $QMOPTIONS"

## The header which was suppressed by use of the "-nohead" qualifier 
HEADER="
		--- (imsimta qm summarize -held) ---

                                                   Messages
Channel                  Channel  Queued   Held   Size (Kb)   Oldest
-------------------------------- -------- ------ ----------- -----------------

"

## A divider which comes after the data output

DIVIDER="
-------------------------------- -------- ------ ----------- -----------------
"

## Real work begins below.

if [ -x "$IMSIMTA" ]; then
	eval "$QMCMD > $_qmout 2>/dev/null"
else
        COLOR="yellow"
        _subject="`date` - cannot execute imsimta call"
        _full_report="$IMSIMTA program is not executable or does not exist"

        print_${report_style}_report

fi

if [ ! -f $_qmout ]
then
	_subject="`date` - cannot create $_qmout (permissions problem ?)"
        COLOR="red"
        print_${report_style}_report 
elif [ ! -r $_qmout ]
then
	_subject="`date` - cannot read $_qmout"
        COLOR="red"
	print_${report_style}_report 
elif [ ! -w $_qmout ]
then
	_subject="`date` - cannot write $_qmout"
        COLOR="red"
	print_${report_style}_report 
elif [ ! -s $_qmout ]
then
     #
     ## There must have been a problem executing the qm command properly
     ## classify this as a yellow status (but not a panic) so that it gets attention.
     #

     COLOR="yellow"
     _subject="`date` - Problem With QM Command: Empty output file)"

     _full_report="
     <center><B>--- (imsimta qm summarize -held) ---</B></center>
     There was a problem evaluating

                 '$IMSIMTA qm summarize -held'

          (The result was an empty output file)

     Be sure that the QMCMD is properly setup and/OR that the .rhosts file of
     '$imtauserid' is setup to allow the rsh action.

	Further, check that the Filesystem of $HEALTHMON_TMPDIR is not FULL"

    print_${report_style}_report "${_subject}${_full_report}"

else

    ## Each line has the number of pending messages, number of held messages, and total bytes
    ## Calculate the grand totals.

    SUMMARY=`awk '{ nmsg += $2; nheld += $3 ; nbytes +=$4 } 
    END { 
      printf("%32s %7s %6s %11.2f\n", "Totals", nmsg, nheld, nbytes) 
    }' $_qmout`

    set $SUMMARY
    _totalmsgs=$2
    _totalheldmsgs=$3

    # Now process the lines for comparisons against our alarm thresholds.

    export REDLINE YELLOWLINE

    $GREP -v 0.00 $_qmout | {	#skip lines with 0.00 as message size
    while read line
    do
       if [ -z "$line" ] ; then continue ; fi	#skip blank lines

       set $line

       CHANNELNAME=$1
       CHANNELCOUNT=$2
       HELDCOUNT=$3

       QMWARNVAL=$queuewarnlimit   # USE DEFAULT WARN VALUE from the alarm.cfg
       QMPANICVAL=$queuepaniclimit # USE DEFAULT PANIC VALUE from the alarm.cfg
       if [ -z $heldwarn ]; then
           QMHELDWARNVAL=$queueheldpaniclimit # USE value from the alarm.cfg
       else
           QMHELDWARNVAL=$heldwarn 
       fi

       # If we were given a channel name and panic limits on the command line,
       # override the ones from the alarms.cfg and ignore the qmtab file.

       if [ ! -z "$channel" ]
       then
           QMWARNVAL=$warnlimit
           QMPANICVAL=$paniclimit
           QMHELDWARNVAL=$heldwarn
       elif test -f "${qmtab}"
       then
          #
          # A custom $qmtab file
          # IF WE HAVE A CUSTOM QMTAB FILE, USE IT.
          # DEFAULTS TO PACKAGEDIR/etc/qmtab.cfg
          # FORMAT:    channelname:warnCOUNT:panicCOUNT:heldCOUNT
          # I.E.:       tcp_local:3000:4000:200
          #        process:200:300:500:-1 where -1 means ignore .HELD messages for this channel
          #
          # File format:  [host:]channelname:WARN:PANIC
          # Check for the channelname without the leading host
          # echo "*** CHECKING FOR CHANNEL $CHANNELNAME IN $qmtab"

          # Of highest precedence if host is defined
          CHANNELLINE=`$GREP "^${MACHINEMASK}[   ]*:[ 	]*${CHANNELNAME}[ 	]*:" $qmtab`
          # echo "*** CHANNELLINE: $CHANNELLINE"
          if test "$CHANNELLINE" = ""
          then
          CHANNELLINE=`$GREP "^${MACHINEMASK}\..**:[ 	]*${CHANNELNAME}[ 	]*:" $qmtab`
          fi

          # Then look for localhost:
          if test "$CHANNELLINE" = ""
          then
             CHANNELLINE=`$GREP "^localhost[ 	]*:[ 	]*${CHANNELNAME}[ 	]*:" $qmtab`
          fi

          # Then look for a line that starts with : (equivalent of localhost:)
          if test "$CHANNELLINE" = ""
          then
             CHANNELLINE=`$GREP "^[ 	]*:[ 	]*${CHANNELNAME}[ 	]*:" $qmtab`
             if test "$CHANNELLINE" != ""
             then
                CHANNELLINE="localhost${CHANNELLINE}"
             fi
          fi

          # Finally look for a line that starts with the channelname
          if test "$CHANNELLINE" = ""
          then
             CHANNELLINE=`$GREP "^[ 	]*${CHANNELNAME}[ 	]*:" $qmtab`
             # echo "*** CHANNELLINE: $CHANNELLINE"
             if test "$CHANNELLINE" != ""
             then
                CHANNELLINE="localhost:${CHANNELLINE}"
             fi
          fi

          # We have a match
          if test "$CHANNELLINE" != ""
          then
             # echo "*** CHANNELLINE NOT NULL"
             set `echo $CHANNELLINE | $SED "s/:/ /g"`
             QMWARNVAL=$3            # GET CURRENT QM WARNING LEVEL
             QMPANICVAL=$4           # GET CURRENT QM PANIC LEVEL
             QMHELDWARNVAL=$5        # GET CURRENT QM WARN LEVEL for .HELD Messages
             # echo "*** WARN AT $QMWARNVAL - PANIC AT $QMPANICVAL - HELD AT $HELDWARNVAL"
          fi
       fi

       MARK=""

       # Check for .HELD messages

       if test "$QMHELDWARNVAL" -ne "-1"         # -1 means ignore HELD messages for $CHANNEL
       then
           if test "$HELDCOUNT" -gt "$QMHELDWARNVAL"    # ABOVE HELD THRESHOLD, SHOULD SET WARNING LEVEL,
           then
                if test "$COLOR" = "green"          # ONLY IF NOT ALREADY SET
                then
                       COLOR="yellow"
                       HIGHMARK="WARNING"
                fi

                HELDFOUND="TRUE"	# Set flag to say we have found .HELD messages
                HELDLINE="${GIFCOLOR} ${CHANNELNAME} (${CHANNELCOUNT} .HELD msgs) has reached the defined Message count ${MARK} level (${MARKLEVEL} msgs)
${HELDLINE}"
           fi
       fi

       if test "$CHANNELCOUNT" -ge "$QMWARNVAL"         # ABOVE WARNING LEVEL
       then
           if test "$CHANNELCOUNT" -ge "$QMPANICVAL"    # ABOVE PANIC, RED ALERT !!!
           then
               COLOR="red"
               MARK="PANIC"
               MARKLEVEL=$QMPANICVAL
               HIGHMARK="PANIC"
               GIFCOLOR="&red"
               REDLINE="${GIFCOLOR} ${CHANNELNAME} (${CHANNELCOUNT} msgs) has reached the defined Message count ${MARK} level (${MARKLEVEL} msgs)
${REDLINE}"
           else
               if test "$COLOR" = "green"          # ONLY IF NOT ALREADY SET
               then
                     COLOR="yellow"
                     HIGHMARK="WARNING"
               fi
               MARK="WARNING"
               MARKLEVEL=$QMWARNVAL
               GIFCOLOR="&yellow"
               YELLOWLINE="${GIFCOLOR} ${CHANNELNAME} (${CHANNELCOUNT} msgs) has reached the defined Message count ${MARK} level (${MARKLEVEL} msgs)
${YELLOWLINE}"
           fi
       fi
   done

   if [ ! -z "$HELDFOUND" ]
   then
       YELLOWLINE="${YELLOWLINE}${HELDLINE}"
   fi

   printf "\n${REDLINE}${YELLOWLINE}\n${HEADER}`cat $_qmout`${DIVIDER}" >> $_msgfile

   _full_report="`cat $_msgfile`"

   if [ "$COLOR" = "green" ]
   then
        _subject="`date` - $channel msg count on $MACHINEDOTS OK"
   else
        _subject="`date` - $channel msg count on $MACHINEDOTS at $HIGHMARK level"
   fi

   print_${report_style}_report
   
   }

fi

##
## Do cleanup of temporary files
##

rm ${_qmout}* $_msgfile

exit
