Listing 7: notification.ksh--Notifies users about events/conditions
#!/bin/ksh
#===================================================
# "@(#)notification.ksh"
#
# This script notifies the user(s) about the
# conditions and events. Currently only two
# notification styles are supported : paging
# and mail. Mail and page contacts can be
# configured separately in the configuration
# files. If the message is a state, the message
# file is retained and will be deleted when the
# end of condition message is received. In case
# of an event, the messages file is deleted after
# the user(s) are notified of the event.
#
# Parameters : None
#
# Calling mechanism :
# Should be called from cron after every unit of
# time equal to the reporting time. For example
# if you want notifications every 15 minutes,
# then the cron entry should be
# 0,15,30,45 * * * * /usr/local/bin/notification
#
# Author :
# Ravindra Nemlekar and Jeffrey Soto
#
#==================================================
BINDIR=/usr/local/admin/sysmon
BASEDIR=/usr/local/admin/sysmon
integer ok_value=-1 no_ok_required=-2
umask 0
cd ${BASEDIR}/hosts
# Search for a Condition file. If found, read
# the generic config file, the condition specific
# config file and the condition specific config
# file for that host. Each notification style can
# be turned on or off.
for file in $(find . -type f -name "Condition.*" \
-print)
do
host=$(dirname $file)
condition_type=$(basename $file)
count_file=$host/count.$condition_type
ok_file=$host/End.$condition_type
host_log_file=$host/log.Condition
integer count=0
# Is it the first iteration?
if [ ! -f $count_file ] ;then
echo "0" > $count_file
else
# Just increment the count.
count=$(cat $count_file)
count=count+1
echo "$count" > $count_file
fi
if [ -f $ok_file ] ; then
count=ok_value
fi
# Default is a stateful condition not an event
EVENT_FLAG=0
# Send an Okay message also
OK_FLAG=1
# Read this for each host so that each
# gets the initialized parameters
# Read the global config file, then local
# config files would override the parameters
# which are to be reset for each. Each
# parameter is initialized to some default
# value so that even if no config files are
# present, the script does not misbehave.
if [ -f $BASEDIR/config.generic ] ; then
# read the global file first
. $BASEDIR/config.generic
fi
if [ -f $BASEDIR/config.$condition_type/\
config.generic ] ; then
# read the condition specific global file
. $BASEDIR/config.$condition_type/\
config.generic
fi
if [ -f $host/config.$condition_type ] ; then
# read the condition specific file for
# the problem on that host.
. $host/config.$condition_type
fi
# Send a mail by default
MAIL_FLAG=${MAIL_FLAG:-1}
# Mail "sa" by default
mail_contacts=${MAIL_CONTACTS:-sa}
if [ "$MAIL_FLAG" -eq 1 ] ; then
if (( count == 0 )) ; then
cat $file | /usr/ucb/Mail -s \
"Error on $(basename $host)" $mail_contacts
elif (( count == ok_value )) ; then
if [ "$OK_FLAG" = 1 ] ; then
cat $ok_file | /usr/ucb/Mail -s \
"$(basename $host) is ok" $mail_contacts
fi
fi
fi
# Number of beeps to be sent for a
# particular condition.
BEEP_COUNT=${BEEP_COUNT:-0}
# Beep "sa" by default
beep_contacts=${BEEP_CONTACTS:-sa}
# Don't beep by default
do_beep=False
if (( count < $BEEP_COUNT )) ; then
do_beep=True
fi
if [ "$OK_FLAG" = 0 -a $count -eq $ok_value ]
then
do_beep=False
echo $(date) $(cat $ok_file) >> $host_log_file
fi
# Do the actual beeping
if [ "$do_beep" = "True" ] ; then
if [ -f $ok_file ] ; then
msg=$(cat $ok_file)
else
msg=$(cat $file)
fi
if (( count == 0 || count == $ok_value ))
then
echo $(date) $msg >> $host_log_file
fi
for name in $beep_contacts
do
/usr/local/bin/beep $name $msg
done
fi
# Execute a script when the condition is
# encountered first. This enables automated
# response to the condition.
if (( count == 0 )) ; then
if [ "$SCRIPT" ] ; then
# don't wait for the script to complete
$SCRIPT &
fi
fi
# Remove the condition and Okay files, so
# that they are not sent again.
if (( count == $ok_value || $EVENT_FLAG == 1 ))
then
/bin/rm -f $ok_file $count_file $file
fi
done
# End of File
|