Listing 2 Service-monitor script
#!/bin/sh
#
# Name: /usr/local/sbin/service-monitor
# Author: Leo Liberti
# Purpose: To monitor IP activity on another host and if
# it crashes, to take its place
# Source: bash
# History: 001107 work started (derived from monitor)
# Usage: /usr/local/sbin/service-monitor A
#
# logfile directory
LOGDIR=/usr/local/lib/service-monitor/log
# interval in seconds between service activity tests
TESTINTERVAL=30
function pingservice() {
echo "GET /" | nc $1 80 | grep -qs "<HTML>"
}
# parse command line
if [ "$1" == "" ]; then
echo "$0: error: syntax: $0 <hostname|IPaddr>"
exit 1
fi
HOST=$1
# main loop
LOGF=$LOGDIR/service-monitor-$HOST
while true; do
while pingservice $HOST ; do
echo "service-monitor: $HOST: service HTTP active at `date \
"+%y%m%d%H%M%S"`" >> $LOGF
sleep $PINGINTERVAL
done
# if we're here it means that service is down - take action
echo "service-monitor: $HOST: SERVICE INACTIVE at `date \
"+%y%m%d%H%M%S"`" >>$LOGF
# try to get apache to resume service
FLAG=1
if ! rexec A ps -e | grep -v grep | grep -qs httpd ; then
rexec A apachectl graceful
sleep 1
if pingservice A ; then
FLAG=0
fi
fi
if [ FLAG==1 ] ; then
rexec A ifconfig eth0 down
apachectl graceful
ifconfig eth0:0 192.168.31.1 up
exit 0
fi
done
|