My script is using ping to find out if a computer is available or not. If the state of a computer has changed an email notification is send and new log entry is added.
In the script, there are 3 variables you may want to change.
STATLIST - specify a file where hosts names and states are stored. Example of the STATLIST file:
myhost.site.com N
11.0.0.1 N
-here you can add your hosts and specify the initial state as "N" (Not available)
LOG - specify your log file location. The log file will be created if it does not exist.
EMAILID - your email address
Script:
#!/bin/bash if [ "$1" == "-h" ] || [ "$1" == "--help" ] || [ "$1" == "help" ]; then echo "Script for server availability monitoring based on ping reply with email notifications" echo -e "KrisKo 2011\n" echo "monitor.stat file formating: "<hostname> <state>" (without quotes); e.g. myweb.com N" echo "States: \"A\" stands for available, \"N\" for Not Available." echo -e "\nUSAGE:" echo "-h, --help, help Display this help" echo "-v Verbose, print info to standart input" exit 0 fi STATLIST="/home/krisko/bin/monitor.stat" LOG="/var/log/monitor.log" #add ip / hostname separated by white space HOSTS="`cat $STATLIST | awk '{ print $1 }'`" #ping request count COUNT=3 #email report when SUBJECT="HOST Alert" EMAILID="mymail@gmail.com" for myHost in $HOSTS; do count=`ping -c $COUNT $myHost | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }'` STATE=`cat $STATLIST | grep -w $myHost | cut -d " " -f 2` #if ping failed and the state was Available - notify if [ $count -eq 0 ] && [ "`echo $STATE`" == "A" ]; then if [ "$1" == "-v" ]; then echo "Host: $myHost is down (ping failed) at $(date +'%b %d %T')" fi #send mail echo "Host: $myHost is down (ping failed) at $(date +'%b %d %T')" | mail -s "$SUBJECT DOWN for $myHost" $EMAILID sleep 2 #write to the log file echo "$(date +'%b %d %T') $myHost changed state to DOWN." >> $LOG #change hosts state sed -i '/\b'$myHost'\b/s/.$/N/' $STATLIST #else if ping is successful and the state was NOT Available - notify elif [ $count -gt 0 ] && [ "`echo $STATE`" == "N" ]; then if [ "$1" == "-v" ]; then echo "Host: $myHost is up at $(date +'%b %d %T')" fi #send mail echo "Host: $myHost is up at $(date +'%b %d %T')" | mail -s "$SUBJECT UP for $myHost" $EMAILID sleep 2 P #write to the log file echo "$(date +'%b %d %T') $myHost changed state to UP." >> $LOG #change hosts state sed -i '/\b'$myHost'\b/s/.$/A/' $STATLIST #else if -v parameter is specified, show host state info elif [ "$1" == "-v" ]; then echo -n State Unchanged for host: $myHost is if [ "`echo $STATE`" == "A" ]; then echo " UP." elif [ "`echo $STATE`" == "N" ]; then echo " DOWN." else echo " UNKNOWN." fi fi done exit 0 ################## # ChangeLog/TODO # ################## # 1.0.0 2011-01-01 Genesis # 1.1.0 2014-06-07 Added -w opt to grep, \b to sed (bug fix: several matches were possible from monitor.stat file) ##################
No comments:
Post a Comment