#!/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_procs,v $
##       (Big Brother ext plugin , adapted from bb-procs.sh) 
##	 Checks that a important services/processes are up and running.
##
## 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

TEST=imta-procs

case `uname -s` in
SunOS )
    /bin/ps -ef |grep mailsrv > $HEALTHMON_TMPDIR/check_procs.$$
    ;;
* )
    /bin/ps -u mailsrv > $HEALTHMON_TMPDIR/check_procs.$$
    ;;
esac

GREENLINES=""
YELLOWLINES=""
REDLINES=""
STATLINE="No iMS processes to check $MACHINEDOTS"
COLOR="green"

grep -i enable $MSG_CONF_FILE > $HEALTHMON_TMPDIR/check_procs.services
grep -i enable $LOCAL_CONF_FILE >> $HEALTHMON_TMPDIR/check_procs.services

# use imta-procs.cfg to define the list of processes

if [ -f "$_CURR_WDIR/../etc/imta-procs.cfg" ]
then
    # All mask should allow for spaces/tabs before :
    # Grep for each mask
    $RM -f $HEALTHMON_TMPDIR/imtaprocs.$$
    for mask in "[ 	]*:" "[ 	]*localhost[ 	]*:" "[ 	]*${MACHINEMASK}[ 	]*:" "[ 	]*${MACHINEMASK}\..*:"
    do
         # Save procs in FILE
         procsline=`grep "^${mask}" $_CURR_WDIR/../lib/imta-procs.cfg >>$HEALTHMON_TMPDIR/imtaprocs.$$ 2>/dev/null`
    done

    # IF any line matched then process
    if [ -s $HEALTHMON_TMPDIR/imtaprocs.$$ ]
    then
        # Get rid of defaults
        imtaprocs=""
        imtapageproc=""
        $RM -f $HEALTHMON_TMPDIR/imtaprocs.$$
        $RM -f $HEALTHMON_TMPDIR/imtapageproc.$$
        cat $HEALTHMON_TMPDIR/imtaprocs.$$ | $SORT -u |
        while read line
        do
            line=`echo "$line" | sed 's/:/ : /g'`
            #line=`echo "$line" | sed 's/::/: :/g'`
            OLDIFS=$IFS
            IFS=':'
            set $line
            IFS=$OLDIFS
            echo $2 >> $HEALTHMON_TMPDIR/imtaprocs.$$
            echo $3 >> $HEALTHMON_TMPDIR/imtapageproc.$$
        done
        imtaprocs=`$CAT $HEALTHMON_TMPDIR/imtaprocs.$$`
        imtapageproc=`$CAT $TMPDIR/imtapageproc.$$`
        $RM -f $HEALTHMON_TMPDIR/imtaprocs.$$
        $RM -f $HEALTHMON_TMPDIR/PAGEPROC.$$
    fi
    $RM -f $HEALTHMON_TMPDIR/imtaprocs.$$
fi

imtaprocs=`echo $imtaprocs $imtapageproc | sed 's/\![ 	]*"/"\!/g'`

if [ -n "$imtaprocs" ]
then
    OLDIFS=$IFS
    IFS='"'
    set " "$imtaprocs >/dev/null 2>&1
    IFS=$OLDIFS
    imtaprocs=""
    while [ $# -gt 0 ]
    do
       imtaprocs="$imtaprocs $1"
       if [ $# -gt 1 ]
       then
          imtaprocs="$imtaprocs "`echo $2 | sed 's/ /\&_/g'`
          shift
       fi
       shift
    done
fi

for proc in $imtaprocs
do
    proc=`echo "$proc" | sed 's/\&_/ /g'`
    case $proc
    in
       *!* )
          proc=`echo "$proc" | sed 's/!//g'`
          TESTREVERSED=TRUE
          OK=DOWN
          NOTOK=UP
          ;;
       *)      
          TESTREVERSED=FALSE
          OK=UP
          NOTOK=DOWN
          ;;
    esac

    grep "$proc" $HEALTHMON_TMPDIR/check_procs.$$ > /dev/null 2>&1
    RC=$?
    # Check if service must NOT be running
    if [ "$TESTREVERSED" = "TRUE" ]
    then    
       if [ "$RC" -ne 0 ]
       then
          RC=0
       else
          RC=1
       fi
    fi   
    if test "$RC" != "0"
    then
       STATLINE="Some iMS processes are in error on $MACHINEDOTS"
       #
       # NOW SEE IF THIS IS A PROCESS WE SHOULD NOTIFY ON...
       #
       echo "$imtapageproc" | grep "$proc" > /dev/null 2>&1
       if test "$?" = "0"
       then
          COLOR="red"
          REDLINES="&red $proc  $NOTOK
$REDLINES"
       else
          if test "$COLOR" != "red"    # WASN'T A PANIC
          then
             COLOR="yellow"       # SO JUST WARN
          fi
          YELLOWLINES="&yellow $proc  $NOTOK
$YELLOWLINES"
       fi	
    else
    if [ "$COLOR" = "green" ]
    then
          STATLINE="All iMS processes are OK on $MACHINEDOTS"
    fi
    GREENLINES="&green $proc  $OK
$GREENLINES"
    fi
done

_subject="`date` $STATLINE"

_full_report="
${REDLINES}${YELLOWLINES}${GREENLINES}"

print_${report_style}_report

rm -f $HEALTHMON_TMPDIR/check_procs.$$ $HEALTHMON_TMPDIR/check_procs.services

## end of script
