Advertisement
Guest User

Untitled

a guest
Nov 12th, 2011
784
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.52 KB | None | 0 0
  1. #!/bin/bash
  2. #
  3. # add ip or hostname space separated
  4. HOSTLIST="1.2.3.4 www.somehost.com 12.34.56.78 otherhost.org"
  5.  
  6. # email report
  7.  
  8.  
  9. # number of ping requests,  timeout in seconds and a threshold
  10. COUNT=5
  11. TIMEOUT=2
  12. MAXFAIL=2
  13.  
  14. # ip to use to check if *this* machine can ping (8.8.4.4 is google dns)
  15. CONTROLIP="8.8.4.4"
  16.  
  17. #
  18. let "THRESHOLD = $COUNT - $MAXFAIL"
  19.  
  20. ping -c 1 -W $TIMEOUT $CONTROLIP > /dev/null && for HOST in $HOSTLIST
  21. do
  22.   count=$(ping -c $COUNT -W $TIMEOUT $HOST | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }')
  23.   if [ $count -lt $THRESHOLD ] ; then
  24.     # too many ping failed but control is up: if previously up->failed-once, if failed-once->down
  25.    
  26.     if [ -f .up-$HOST ];    then
  27.             mv  .up-$HOST .failed-once-$HOST
  28.         else
  29.             if [ -f .failed-once-$HOST ]; then
  30.                     mv  .failed-once-$HOST .down-$HOST
  31.                     echo "Host : $HOST is down (ping failed) at $(date)" | mailx -r $MAILFROM -s "Alert: $HOST is down" $MAILTO
  32.                 else
  33.                 # is it down or a new host -> failed-once
  34.                     if [ ! -f .down-$HOST ];    then
  35.                             touch .failed-once-$HOST
  36.                     fi
  37.             fi
  38.     fi
  39.    
  40.   else
  41.     # most ping ok, host is up: if !up -> up
  42.    
  43.     if [ ! -f .up-$HOST ]; then
  44.             if [ -f .failed-once-$HOST ]; then
  45.                 rm  .failed-once-$HOST
  46.             else
  47.                 if [ -f .down-$HOST ]; then
  48.                     rm  .down-$HOST
  49.                     echo "Host : $HOST is up again (ping ok) at $(date)" | mailx -r $MAILFROM -s "Alert: $HOST is UP again" $MAILTO
  50.                 fi
  51.             fi
  52.     touch .up-$HOST
  53.     fi
  54.  
  55.   fi
  56. done
  57.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement