Cover V10, I04
Article
Listing 1
Listing 2

apr2001.tar


Listing 1 Monitor script

#!/bin/sh
#
# Name:         /usr/local/sbin/monitor
# Author:       Leo Liberti
# Purpose:      To monitor IP activity on another host and if
#               it crashes, to take its place
# Source:       bash
# History:      000821  work started
# Usage:        /usr/local/sbin/monitor A [yes|no]
#
# install directory of monitor
BINDIR=/usr/local/lib/monitor/bin
# logfile directory
LOGDIR=/usr/local/lib/monitor/log
# this controls whether monitor exits after a ping fails or reenters
# main loop
EXITAFTERFAILURE=yes
# interval in seconds between pings
PINGINTERVAL=30

function pinghost() {
  ping -c 1 $1 > /dev/null 2>&1
}
# parse command line
if [ "$1" == "" ]; then
        echo "$0: error: syntax: $0 <hostname|IPaddr> [yes|no]"
        exit 1
fi
HOST=$1
if [ "$2" != "" ]; then
        EXITAFTERFAILURE=$2
fi
# main loop
LOGF=$LOGDIR/monitor-$HOST
while true; do
        while pinghost $HOST
        do
          echo "monitor: $HOST: alive at `date "+%y%m%d%H%M%S"`" >> $LOGF
          sleep $PINGINTERVAL
        done

        # if we're here it means that host has not responded - take action
        echo "monitor: $HOST: NOT RESPONDING at `date "+%y%m%d%H%M%S"`" \ 
          >>$LOGF
        # execute appropriate action script
        if [ -x $BINDIR/$HOST-noping ]; then
          echo "monitor: $HOST: spawning script $HOST-noping" >> $LOGF
          $BINDIR/$HOST-noping &
        fi
        if [ EXITAFTERFAILURE==yes ]; then
          echo "monitor: $HOST: exiting monitor"
          exit 1
        else
          echo "monitor: $HOST: continuing the monitoring"
        fi
done
exit 0


------------------------------------------------------------------------


#!/bin/sh
# the ``take action'' part of the scheme
apachectl graceful
ifconfig eth0:0 192.168.31.1 up