Mar.26

Linux Shell Script For Monitoring System network with ping command

Here is a simple script to monitor server IP status with lock. Script will send only 1 email when IP is not reachable.If it came back, it will send another mail

 

#!/bin/bash
NOTIFYEMAIL=ben.george@example.com
SENDEREMAIL=smcadmin@example.com
SERVER=172.16.200.51
PAUSE=2
FAILED=0
DEBUG=0
COUNT=1

while true
do
CS=$(ping -c $COUNT $SERVER | grep ‘received’ | awk -F’,’ ‘{ print $2 }’ | awk ‘{ print $1 }’)
if [ $DEBUG -eq 1 ]
then
echo “STATUS = $CS”
echo “FAILED = $FAILED”
if [ $CS -ne 0 ]
then
echo “$SERVER is up”

elif [ $CS -eq 0 ]
then
echo “$SERVER is down”
fi
fi
if [ $CS -ne 0 ] && [ $FAILED -eq 0 ]
then
FAILED=1
if [ $DEBUG -eq 1 ]
then
echo “$SERVER is back up”
fi
if [ $DEBUG = 0 ]
then
echo “$SERVER is came up at $(date)” | /bin/mailx -s “$SERVER is came up at ” -r “$SENDEREMAIL” “$NOTIFYEMAIL”
fi
elif [ $CS -eq 0 ] && [ $FAILED -eq 1 ]
then
FAILED=0
if [ $DEBUG -eq 1 ]
then
echo “$SERVER failed”
fi
if [ $DEBUG = 0 ]
then
echo “$SERVER went down $(date)” | /bin/mailx -s “$SERVER went down ” -r “$SENDEREMAIL” “$NOTIFYEMAIL”
fi
fi
sleep $PAUSE
done

Share this Story:
  • facebook
  • twitter
  • gplus

About bentech4u

Leave a comment