[wellylug] server monitoring

jumbophut jumbophut at gmail.com
Thu Jan 13 16:45:06 NZDT 2005


On Thu, 13 Jan 2005 15:44:26 +1300, Mark Signal wrote:
> Hi all
> 
> does anyone know of a script (run as a cron job?)  that will ping a
> remote server a few times regularly and run a command if the server
> becomes unpingable  - and ideally runs another command when the server
> rejoins the land of the living.
> 
Disclaimer: no linux to test on right now.  However, something like
this might do what you want:

#!/bin/sh
pingable=1
while [ 1 -eq 1 ]; do
    numpings=5
    ping -c $numpings
    retval=$?
    if [ $retval -eq 1 ]; # if the ping receives no reply packets
        then if [ pingable -eq 1 ]; # and the lack of reply has just started
            then mail -s "Unpingable" admin at box;
            pingable=0;
        fi;
    elif [ $retval -eq 0 ]; # if the ping received replies
        then if [ pingable -eq 0 ];  # and hasn't been receiving them
            then mail -s "Pingable again" admin at box;
            pingable=1;
        fi;
    else mail -s "Unknown ping error" admin at box;
    fi;
done

To do it as a cron job, you will want to store 'pingable' in a file
somewhere and check it each time (removing the while loop).  So where
I've said pingable=0 at the top of the file, you'd say, e.g.,
pingable=$(/tmp/pingable), and where I've said pingable=x elsewhere,
you'd say echo 'x' > /tmp/pingable, etc.

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




More information about the wellylug mailing list