#!/bin/bash

## Script to test server downtime
##
## INSTALL
## * Use crontab -e to enter the following so script will run every 5 minutes
## 0 5 * * * /export/home/bthunter/ping.sh
##
## * chmod 755 /export/home/bthunter/ping.sh
## * chmod 644 /export/home/bthunter/ping_log-08172017_135900
## * $DATETIME is date and time of logfile is setup automatically in script DATE And TIME
## * Copy all servers to be tested in $HOME/ping-servers-list
## * $HOME is your default user's home directory ie: /export/home/bthunter
##

PAGER="brian@sutechy.com,4014104325@nexttel.com"
DATETIME="`date +'%m%d%y_%H%M%S'`"
LOGFILE="$HOME/ping_log-$DATETIME"

clear

for svr in `cat /etc/hosts | awk '{ print $2 }'`
do
   if [ $svr != 'localhost.localdomain' ]
   then
      PING=`ping -c1 $svr | grep ^64 | awk '{ print $1 }'`
      if [ $PING != "64" ]
      then
         printf "ATTENTION: [$svr] IS DOWN PAGED $PAGER @ $DATETIME" >>$LOGFILE
         printf "ATTENTION: [$svr]" | mailx -s "ALERT [$svr] SERVER DOWN" $PAGER
      fi
   fi
done
exit 0