#!/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: runmonitor,v $
##       A script to check the flow of messages . This script should be 
##       called by check_rndtrip 
##
## 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)

## MAYBE MODIFIED BASED ON SITE SPECIFIC INFORMATION

#uid=""
DELIVER_REPORT=0
TIMEOUT=180
ERROR=""
RETR_MAX=99999
RETR_TIME=9999999
MAILHOST=""
COLOR="green"
BLURB="All circuits Clear
"

while [ $# -gt 0 ]
do
    case "$1" in
        -s)     shift; SMTP=$1;;
        -p)     shift; POP=$1;;
        -t)     shift; TIMEOUT=$1;;
        -u)     shift; uid=$1;;
	-r)	DELIVER_REPORT=1; break ;;
	-S) 	shift ; report_style="$1";;
    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

TEST="imta-cct"

ALERTFILE=$HEALTHMON_TMPDIR/check_rndtrip.alert.$$
_time="`$_CURR_WDIR/../lib/date_util.sh normalize`"
SUBJECT="Delivery test results for $domain"

LOGNAME=systest.log
LOGFILE=$reportlogdir/$LOGNAME

writeHead () {
        _output_file=$1

        DATE=`date|$AWK '{print $3,$2,$6}'`
        printf "\nDATE COLLECTED: $DATE\n" > $_output_file
        printf "+---------------------------------------------------------+-----------------+\n" >> $_output_file
        printf "|                 Message Delivery time                   |                 |\n" >> $_output_file
        printf "+----------+-----------+------------+------------+--------+-----------------+\n" >> $_output_file
        printf "| Test run | Username: | Msg submit | Msg retr   | Total  | Error           |\n" >> $_output_file
        printf "| at time: |           | SMTP:      | POP:       | Time:  |                 |\n" >> $_output_file
        printf "+----------+-----------+------------+------------+--------+-----------------+\n" >> $_output_file
}

getErrType () {

    for x in SMTP POP delivery
    do
        grep Total $HEALTHMON_TMPDIR/check_rndtrip.stdout.$$|grep $x >/dev/null 2>/dev/null
        check=$?
        if [ $check -ne 0 ]
        then
            ERROR="$x"
            BLURB="$ERROR "
            for j in access connect 
            do
                BLURB="$BLURB `grep -i $j: $HEALTHMON_TMPDIR/check_rndtrip.stdout.$$`"
                BLURB=" $BLURB `grep -i $j: $HEALTHMON_TMPDIR/check_rndtrip.stderr.$$`"
            done
            break
        fi
    done
}

if [ ! -f $LOGFILE ]; then
        writeHead $LOGFILE
fi

if [ $DELIVER_REPORT -eq 1 ]; then

        printf "+----------+-----------+------------+------------+--------+-----------------+\n" >> $LOGFILE
        $IMMONITOR_ACCESS -f $sender -k "$SUBJECT" -u $reportrcpts -m $LOGFILE
        $rotatelog -l $LOGNAME
        writeHead $LOGFILE

else

        LDAPBASE=`grep -i local.ugldapbasedn $MSG_CONF_FILE | cut -d"=" -f2-`
        LDAPBIND=`grep -i local.ugldapbinddn.=.".*" $MSG_CONF_FILE | cut -d"\"" -f2-`
        LDAPBINDCRED=`grep -i local.ugldapbindcred $MSG_CONF_FILE | cut -d"=" -f2- |cut -d" " -f2-`
        LDAPHOSTS=`grep -i local.ugldaphost $MSG_CONF_FILE | cut -d"=" -f2-`
        LDAPPORT=`grep -i local.ugldapport $MSG_CONF_FILE | cut -d"=" -f2-`

        for LDAPHOST in $LDAPHOSTS 
        do
            LDAP_CMD="$LDAPSEARCH -h $LDAPHOST -p $LDAPPORT -b $LDAPBASE -D \"$LDAPBIND -w \"$LDAPBINDCRED\" \"uid=$uid\" mailhost" 
            MAILHOST=`eval $LDAP_CMD |$AWK '/mailhost:/ {print $2}'`

            if [ ! -z "$MAILHOST" ] ; then
               break
            fi
        done

        if [ -z "$MAILHOST" ]
        then
            COLOR="red"
            ERROR="LDAP down"
            BLURB=""
            echo "&$COLOR LDAP down on one or more of $LDAPHOSTS" > $ALERTFILE
            echo "" >> $ALERTFILE
            printf "| %8s | %9s | %10s | %10s |%7d | %15s |\n" \
                   $_time $uid $SMTP $POP $RETR_TIME "$ERROR" >> $LOGFILE
        else
            for smtphost in $SMTP $MAILHOST
            do

                for pophost in $POP $MAILHOST
                do
	             smtphost=`echo $smtphost|cut -d\. -f 1`
		     pophost=`echo $pophost|cut -d\. -f 1`

                     $IMMONITOR_ACCESS -S $smtphost -f $sender -P $pophost -u $uid -w $secret \
                         >$HEALTHMON_TMPDIR/check_rndtrip.stdout.$$ 2>$HEALTHMON_TMPDIR/check_rndtrip.stderr.$$ 
                     RESULT=$?

                     if [ $RESULT -eq 0 ] 
                     then 

                        RETR_TIME=`grep "Total delivery time" $HEALTHMON_TMPDIR/check_rndtrip.stdout.$$ \
                                 | $AWK '{print $(NF-1)}'| cut -d\. -f 1`
			
                        printf "| %8s | %9s | %10s | %10s |%7d | %15s |\n" \
                               $_time $uid $smtphost $pophost $RETR_TIME "$ERROR" >> $LOGFILE

                        break 2
                     else
                        COLOR="yellow"
                        getErrType
                     fi

                done

            done

            if [ $RESULT -ne 0 ]
            then
                  COLOR="red"
            fi

        fi   
fi      

writeHead ${ALERTFILE}.$$

printf "| %8s | %9s | %10s | %10s |%7d | %15s |\n" \
           $_time $uid $smtphost $pophost $RETR_TIME "$ERROR" >> ${ALERTFILE}.$$

printf "+----------+-----------+------------+------------+--------+-----------------+\n" >> ${ALERTFILE}.$$

cat ${ALERTFILE}.$$ | sed -e 's/|/:/g' >> $ALERTFILE

_full_report="
&${COLOR} ${BLURB} `cat ${ALERTFILE}`"

if [ "$COLOR" = "green" ]
then

      _subject="`date` $TEST OK on $MACHINEDOTS"
      print_${report_style}_report

else

      ## non green event...
      _subject="`date` - Problems reported on $MACHINEDOTS"
      print_${report_style}_report

fi

rm -f ${ALERTFILE}* $HEALTHMON_TMPDIR/check_rndtrip.*$$

## end of script
