Listing 4: mon_host
#!/bin/sh
#
# mon_host
#
# This script uses "ping" to determine whether each of the hosts
# listed in "mon_host.dat" responds. If a host fails to respond, it
# is recorded in the status file and generates a "CRITICAL" alarm.
#
#########################
INSTALL_DIR="/tools" # Installation directory
INFILE="mon_host.dat" # Input data file
PINGCMD="/usr/sbin/ping" # OS-specific "ping" command
PATH=$INSTALL_DIR:$PATH # Search path, beginning with
# $INSTALL_DIR
STATFILE="$INSTALL_DIR/mon_stat" # Status file
#
# This function removes an entry from the $STATFILE.
#
clear_statfile()
{
# If called without an argument, quietly exit
if [ $# = 0 ]; then
exit
# Assume that the argument is a hostname and remove that line from
# the $STATFILE
else
echo 'g?^HOSTDOWN '$1'$?d\nwq' | ex $STATFILE > /dev/null 2>&1
fi
}
# If the input data file exists, continue; otherwise exit with a
# message
if [ -f "$INFILE" ]; then
# Ignore lines in the input file beginning with "#". From each
# other line, read the HOST name
grep -v "^#" "$INFILE" | while read HOST; do
# If we can ping $HOST, make sure it's not in the $STATFILE
if `$PINGCMD $HOST 5 > /dev/null 2>&1`; then
clear_statfile $HOST
# If we can't, and it's already in the $STATFILE, do nothing.
elif grep '^HOSTDOWN '$HOST'$' $STATFILE > /dev/null 2>&1; then
:
# Otherwise, generate an alarm
else
mon_error CRITICAL "Host '$HOST' is not responding"
echo "HOSTDOWN $HOST" >> $STATFILE
fi
done
else
echo "Error: No input file '$INFILE' for tool '$0'"
fi
# End of File
|