Linux: Monitor multiple servers using ping and email on failure
Steps to monitor a website using curl:
1. Create the main script file
2. Create a site list file
3. Create a cron job
1. main scripts
Working directory /home/username/scripts
vi /home/username/scripts/pingmysites
#!/bin/bash
SITESFILE=/home/usernamescripts/servers.txt #list the sites you want to monitor in this file
EMAILS=”[email protected]” #list of email addresses to receive alerts (comma separated)
while read site; do
if [ ! -z “${site}” ]; then
echo $site
checker=$(/bin/ping -c4 $site)
if echo $checker | grep “0 received” > /dev/null
then
echo “The Server on ${site} is down!”
MESSAGE=”This is an alert from MonitorServer that the ping test to the Server ${site} has failed to respond.”
for EMAIL in $(echo $EMAILS | tr “,” ” “); do
SUBJECT=”The ping test to theĀ Server $site (ICMP) Failed”
echo “$MESSAGE” | mail -s “$SUBJECT” $EMAIL
echo $SUBJECT
echo “Alert sent to $EMAIL”
done
fi
else
echo “The Server on ${site} is up!”
fi
done < $SITESFILE
2. Site list file
Working directory /home/username/scripts
vi /home/username/scripts/servers.txt
192.168.1.5
192.168.1.7
3. Cron job
crontab -e
* * * * * /home/username/scripts/./pingmysites
By: Timothy Conrad