[wellylug] server monitoring

jumbophut jumbophut at gmail.com
Thu Jan 13 22:09:45 NZDT 2005


On Thu, 13 Jan 2005 17:09:12 +1300, Mark Signal wrote:
> this is just what I wanted
> 
Hopefully the following is just what you wanted, since it might
actually work!  The perils of writing code without testing :-)  I
still recommend you test thoroughly on your machine.

Two files.  Note you can copy and paste but will need to replace the
^D chars.  They are actually entered using Ctrl-V then Ctrl-D (works
in vi anyway, don't know about other browsers).

The first just runs in a endless loop and you have to use Ctrl-C to
cancel it.  It pings host and sends any useful info to email.  It does
numping pings each time, and waits delay seconds before repeating the
cycle, during which time you can cancel.

#!/bin/sh
# Pings domain.or.ip
host='domain.or.ip'
email='admin at email.address'
numpings=3
delay=5
state=0
pingable=0
notpingable=1
pingerror=2
while [ 1 -eq 1 ]; do
     ping -c $numpings $host
     retval=$?
     case $retval in
     $pingable)
         if [ $state -ne $pingable ]; then
             echo "^D" | mail -s "Pingable again" $email
             # your commands here
         fi;;
     $notpingable)
         if [ $state -ne $notpingable ]; then
             echo "^D" | mail -s "No longer pingable" $email
             # your commands here
         fi;;
     *)
         if [ $state -ne $pingerror ]; then
             echo "^D" | mail -s "Unknown ping problem" $email
             # your commands here
         fi;;
     esac
     state=$retval
     printf "Sleeping (hit Ctrl-C here to abort...)\n"
     sleep 5
done;

The second is suitable for running from a cron file.  It only runs
once, not in a loop.  You need to give the same info as in the first,
but also the name of a state file to write the last state to.  It will
create the file if it doesn't already exist.

#!/bin/sh
#Pings host
host='domain.or.ip'
email='admin at email.address'
numpings=3
statefile='/tmp/state'
pingable=0
notpingable=1
pingerror=2

# Get state from file
if [ -e $statefile ]; then
    if [ -w $statefile ]; then
        state=$(cat $statefile | tr '\n' ' ' | sed -e 's/ //')
    else
        printf "State file $statefile is not writable\n"
        printf "Exiting now.  Nothing done. \n"
        exit 1
    fi;
else
    printf "State file $statefile does not exist.  Creating now.\n"
    touch $statefile
    if [ $? -ne 0 ]; then
        printf "Could not create file.\n"
        printf "Exiting now.  Nothing done. \n"
        exit 2;
    fi;
fi;

case $state in
"0") state=0;;         # needs conversion to number
"1") state=1;;         # don't know why
"2") state=2;;         # just works if this is done
*)   state=$pingable;; # if file was empty or corrupt, make state pingable
esac

ping -c $numpings $host
retval=$?
case $retval in
$pingable)
    if [ $state -ne $pingable ]; then
        subject="Pingable again"
        # your commands here
    fi;;
$notpingable)
    if [ $state -ne $notpingable ]; then
        subject="No longer pingable"
        # your commands here
    fi;;
*)
    if [ $state -ne $pingerror ]; then
        subject="Unknown ping problem"
        # your commands here
    fi;;
esac
if [ "$subject" != "" ]; then
    echo "^D" | mail -s "$subject" $email # note to get ^D, use Ctrl-V, Ctrl-D
    subject=
fi;
echo $retval > $statefile

-- 
Tony (echo 'spend!,pocket awide' | sed 'y/acdeikospntw!, /l at omcgtjuba.phi/')




More information about the wellylug mailing list