Monitor Server Status and Notify when up/down

#!/bin/bash
## Global variables
TO=admin@example.com
CC=admin@example.com
FROM="Your Server Remote Monitor <automation@example.com>"
user=gmailuser
pass=gmailuserpass
SMTP=smtp.gmail.com:587
nitstatus=/tmp/nit

###### Check line up or down 
ping -c3 example.com ; [ $? == 0 ] && checkstatus=up || status is checkstatus=down ; clear ; echo status is  $checkstatus |tail -1

if [[  ! -f $nitstatus ]];
then
echo up > $nitstatus
fi
oldstatus=`cat /tmp/nit`
echo old status is $oldstatus
echo $checkstatus
if [[ $checkstatus == "up" ]]
then
newstatus=up
elif [[ $checkstatus == "down" ]]
then
newstatus=down
fi
###### Email notification ###

echo new status is $newstatus

if
[[ $oldstatus != $newstatus ]]
then

echo "Your Server server is $newstatus"
echo $newstatus > $nitstatus
                  if [[ $newstatus == "down" ]]
                  then
                  echo sending email for down
                  /scripts/sendEmail.pl -f "$FROM" -t "$TO"  -s  "$SMTP" -xu "$user" -xp "$pass"  -u "Your Server Server is Down" -m "
Your Server server is down on `date +%T`"
                  elif [[ $newstatus == "up" ]]
                  then 
                  echo sending email for up
                  /scripts/sendEmail.pl -f "$FROM" -t "$TO"  -s  "$SMTP" -xu "$user" -xp "$pass"  -u "Your Server Server is Up" -m "
Your Server server is up on `date +%T`" 
                  fi

else
echo no change
fi

Leave a Reply

Your email address will not be published. Required fields are marked *

*

* Copy This Password *

* Type Or Paste Password Here *

4,842 Spam Comments Blocked so far by Spam Free Wordpress

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>