#!/bin/sh
#
# $Header: /home/vikas/src/nocol/utility/RCS/notifier,v 1.6 1998/07/31 18:39:29 vikas Exp $
#
# program to notify when site is CRITICAL for an extended period of time 
# in NOCOL (using eventselect)
#
#	-vikas@navya.com, May 1994
#
umask 002

## Tweak these
##
ROOTDIR="/pbulk/work/net/nocol/work/.destdir/usr/pkg"
PIDDIR="/pbulk/work/net/nocol/work/.destdir/var/run"
OPSMAIL="root@localhost"
CRITMAIL="root@localhost"
MAIL="/usr/bin/mail"                           # location of 'mail' program

## To send email to a user after site critical for x to x+1 hours, set AFTERx
AFTER2="root@localhost"
#AFTER3="brass@your.domain"
#AFTER5="hicommand@your.domain"

EVENTSELECT="$ROOTDIR/bin/eventselect"
DATADIR="${ROOTDIR}/data"
TMPFILE="${PIDDIR}/notifier.tmp$$"

trap "echo $0 Exiting on signal ; rm -f ${TMPFILE}; exit 1"  1 2 3 15

if [ ! -f ${EVENTSELECT} ]; then
  echo "$0 ERROR: Program $EVENTSELECT not found"
  exit 1
fi

for  i in 1 2 3 4 5 6
do
  unset TOMAIL ;

  case $i in
    1)  if [ ! "X$AFTER1" = "X" ]; then  TOMAIL="$AFTER1" ; fi ;;
    2)  if [ ! "X$AFTER2" = "X" ]; then  TOMAIL="$AFTER2" ; fi ;;
    3)  if [ ! "X$AFTER3" = "X" ]; then  TOMAIL="$AFTER3" ; fi ;;
    4)  if [ ! "X$AFTER4" = "X" ]; then  TOMAIL="$AFTER4" ; fi ;;
    5)  if [ ! "X$AFTER5" = "X" ]; then  TOMAIL="$AFTER5" ; fi ;;
    6)  if [ ! "X$AFTER6" = "X" ]; then  TOMAIL="$AFTER6" ; fi ;;
    *) ;;
  esac

  if [ "X${TOMAIL}" = "X" ]; then
    continue;       # skip
  fi

  j=`expr $i + 1`
  T1=`expr 3600 \* $i`
  T2=`expr 3600 \* $j`
   
  ${EVENTSELECT} -v critical -f +${T2}  -t +${T1}  ${DATADIR}/* > ${TMPFILE}

  if test -s ${TMPFILE} ; then
    if [ "X${MAIL}" = "X/usr/ucb/mail" ]; then
      cat $TMPFILE | ${MAIL} -s "Sites down for $i - $j hours" $TOMAIL
    else
      cat $TMPFILE | ${MAIL} $TOMAIL
    fi
  fi

  rm -f ${TMPFILE}

done

exit 0

##
