Advertisement
krowvin

Shell Script for Email Failed Ping

Mar 24th, 2016
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/sh
  2.  
  3. #Script is intended for sending reports if a desired host is down using ping.
  4. #Set your hosts in the HOSTS=" " section
  5. #Set the count value to the desired number of pings
  6. #BE SURE to change your email
  7.  
  8. #be sure to set the file permission so the cron job can run it
  9.  
  10. ### Parameters ###
  11. logfile="/tmp/ping_report.tmp"
  12. subject="Ping Status Report from FreeNAS"
  13. HOSTS="google.com 192.168.1.5 freenasiscrunk.com" #enter your local IPs to monitor here
  14. COUNT=4
  15.  
  16.  
  17.  
  18. ### email headers ###
  19. (
  20.     echo "To: ${email}"
  21.     echo "Subject: ${subject}"
  22.     echo "Content-Type: text/html"
  23.     echo "MIME-Version: 1.0"
  24.     echo -e "\r\n"
  25. ) > "$logfile"
  26.  
  27.  
  28.  
  29.  
  30. for myHost in $HOSTS
  31. do
  32.   count=$(ping -c $COUNT $myHost | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }')
  33.   if [ $count -eq 0 ]; then
  34.     # Failed, creating report
  35.    
  36.     echo "<pre style=\"font-size:14px\">" >> "$logfile"
  37. (
  38.    echo "Host : $myHost is down (ping failed) at $(date)"
  39. ) >> "$logfile"
  40.     sendmail -t < "$logfile"
  41.     rm "$logfile"
  42.   fi
  43. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement