November 22

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


Copyright 2021. All rights reserved.

Posted November 22, 2017 by Timothy Conrad in category "Linux

About the Author

If I were to describe myself with one word it would be, creative. I am interested in almost everything which keeps me rather busy. Here you will find some of my technical musings. Securely email me using - PGP: 4CB8 91EB 0C0A A530 3BE9 6D76 B076 96F1 6135 0A1B