Monitoring RG using ping script from EC2

This is a script which runs on an Amazon Web Services EC2 instance and pings the RG to see if it is alive. If the ping fails then it sends an e-mail.

[ec2-user@ip-172-31-15-183 ~]$ cat monitor_rgw
#!/bin/bash
# http://www.cyberciti.biz/tips/simple-linux-and-unix-system-monitoring-with-ping-command-and-scripts.html
# -------------------------------------------------------------------------

# add ip / hostname separated by white space
HOSTS="xxxxxxxx.no-ip.org"

# no ping request
COUNT=1

# email report when
SUBJECT="Ping failed"
EMAILID="xxxxxxx@gmail.com"
for myHost in $HOSTS
do
count=$(ping -c $COUNT $myHost | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }')
if [ $count -eq 0 ]; then
# 100% failed
echo "Subject: $myHost is down at $(date)" | cat - text | /usr/sbin/sendmail -F ec2-user@ami-t2micro2.com -t $EMAILID
fi
done

Crontab is set to run the script every 30 minutes

[ec2-user@ip-172-31-15-183 ~]$ crontab -e
# Monitor remote host every 30 minutes using monitor_rgw
*/30 * * * * /home/ec2-user/monitor_rgw

On the RG itself, a firewall entry is inserted to allow ICMP.

root@voyage:~# iptables -I INPUT 1 -i ppp0 -p icmp
Advertisements