[wellylug] server monitoring
Mark Signal
mark at remote-assist.co.nz
Fri Jan 14 08:36:28 NZDT 2005
oops
looks like the script formatting didnt travel very well...
Mark Signal wrote:
> Hi
> many thanks for this - I learnt quite a bit by playing with your first
> version. - Had all the core logic mixed with enough errors to make me
> think. (whats a few missing "$" between friends :)
>
> this is what I had ended up with :
> ..and yes I had to manually create pingable file
>
> email=mark at webcoda.com
>
> while [ 1 -eq 1 ]; do
> ping -c10 -w 20 $1 > /dev/null 2>&1
> retval=$?
> if [ $retval -eq 1 ]; then # if the ping receives no reply packets
> pingable=$(cat /root/pingable)
> if [ $pingable -eq 1 ]; # and the lack of reply has just started
> then
> echo dead | mail -s "Host has gone down" $email;
> echo Host has gone down;
> echo '0' > /root/pingable;
> fi
> elif [ $retval -eq 0 ]; then # if the ping received replies
> pingable=$(cat /root/pingable)
> if [ $pingable -eq 0 ]; # and hasn't been receiving them
> then
> echo alive | mail -s "Host is back up" $email;
> echo Host is back up;
> echo '1' > /root/pingable;
> fi
> fi
> sleep 60
>
> done
>
> I will now play with this one untill I understand it.
> Ultimately I will modify it so it first checks for local internet
> access and exits if none. I was also thinking that it may make sense
> to have the script sleep for 5 minutes after it detects a faluire and
> try again and then do its stuff on the second faluire - I'll have to
> get head around this new one first.
>
> many thanks for your efforts.
>
> cheers
>
> Mark
>
> jumbophut wrote:
>
>> 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
>>
>>
>>
>
>
--
No virus found in this outgoing message.
Checked by AVG Anti-Virus.
Version: 7.0.300 / Virus Database: 265.6.11 - Release Date: 12/01/2005
More information about the wellylug
mailing list