Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- #
- # add ip or hostname space separated
- HOSTLIST="1.2.3.4 www.somehost.com 12.34.56.78 otherhost.org"
- # email report
- MAILTO="[email protected]"
- MAILFROM="[email protected]"
- # number of ping requests, timeout in seconds and a threshold
- COUNT=5
- TIMEOUT=2
- MAXFAIL=2
- # ip to use to check if *this* machine can ping (8.8.4.4 is google dns)
- CONTROLIP="8.8.4.4"
- #
- let "THRESHOLD = $COUNT - $MAXFAIL"
- ping -c 1 -W $TIMEOUT $CONTROLIP > /dev/null && for HOST in $HOSTLIST
- do
- count=$(ping -c $COUNT -W $TIMEOUT $HOST | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }')
- if [ $count -lt $THRESHOLD ] ; then
- # too many ping failed but control is up: if previously up->failed-once, if failed-once->down
- if [ -f .up-$HOST ]; then
- mv .up-$HOST .failed-once-$HOST
- else
- if [ -f .failed-once-$HOST ]; then
- mv .failed-once-$HOST .down-$HOST
- echo "Host : $HOST is down (ping failed) at $(date)" | mailx -r $MAILFROM -s "Alert: $HOST is down" $MAILTO
- else
- # is it down or a new host -> failed-once
- if [ ! -f .down-$HOST ]; then
- touch .failed-once-$HOST
- fi
- fi
- fi
- else
- # most ping ok, host is up: if !up -> up
- if [ ! -f .up-$HOST ]; then
- if [ -f .failed-once-$HOST ]; then
- rm .failed-once-$HOST
- else
- if [ -f .down-$HOST ]; then
- rm .down-$HOST
- echo "Host : $HOST is up again (ping ok) at $(date)" | mailx -r $MAILFROM -s "Alert: $HOST is UP again" $MAILTO
- fi
- fi
- touch .up-$HOST
- fi
- fi
- done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement