Script for monitoring port availability on the network with email notification (bash)

It just so happened that we live, although in a country with all kinds of technologies, but the stability of the Internet connection for cities that are far from the capital is a rare occurrence. For a long time I chose all kinds of services for professional monitoring of the availability of equipment on the network. Some have a check of only 80 port of the web server, while others can check the keyword on the page, send GET or POST requests. I found the service better than others, there is an opportunity to check the availability of ssh, but the payment depends on the number of requests for a certain period of time and of course the amount of equipment that needs to be monitored.

But stumbled upon a good script Port Monitoring with a system administrator notification, naturally on a habr But as I wrote above, the stability of our Internet gave some inaccuracy in determining the availability of certain servers on the global network, so I slightly changed and finalized the proposed script.

- The script began to send not only a letter about the inaccessibility of the port, but also its appearance on the network.
- The script sends a letter to the mail and writes the time, the number of errors during the scan and the previous status of the scan to the log.
- The script sends a letter to the mail only if there were five unsuccessful connection attempts or five successful ones after unavailability.
- The script does not send a letter if the Internet has disappeared at the location of the script
- The script can be run every minute


#!/bin/bash
CATALOG="/share/Web/"
NMAPCAT="/share/MD0_DATA/.qpkg/Optware/bin/"
WC="/mnt/ext/usr/bin/"
echo "Subject: Monitoring hosts sms" > "$CATALOG"mail
echo "From: send_from@mail.ru" >> "$CATALOG"mail
echo "To: send_to@mail.ru" >> "$CATALOG"mail
echo "CC: copy_to@gmail.com" >> "$CATALOG"mail
echo "" >> "$CATALOG"mail
for i in 1 2 3 4 5
do
for a in $(<"$CATALOG"servers); do "$NMAPCAT"nmap `echo $a | sed -e 's/:/ -p /'` | grep -q "/tcp *open " || echo $a; done >> "$CATALOG"serverlist_n;
sleep 5;
done
"$WC"sort -u "$CATALOG"serverlist_n > "$CATALOG"serverlist
if  [[ $(cat "$CATALOG"serverlist_n | "$WC"wc -w) -ge 5 ]]
then 
      cat "$CATALOG"serverlist >> "$CATALOG"mail
	  echo "" >> "$CATALOG"mail
else
      echo "All OK" >> "$CATALOG"mail
	  echo "" >> "$CATALOG"mail
fi
if  [[ $(cat "$CATALOG"servers | "$WC"wc -w) -eq  $(cat "$CATALOG"serverlist | "$WC"wc -w) ]]
then 
      cat /dev/null > "$CATALOG"serverlist_n
      exit 1 
fi	  
a=`expr $(cat "$CATALOG"serverlist_n | "$WC"wc -w) % 5`
b=`expr $(cat "$CATALOG"serverlist_old | "$WC"wc -w) % 5`
if [[ $(cat "$CATALOG"serverlist_old | "$WC"wc -w) -ne $(cat "$CATALOG"serverlist_n | "$WC"wc -w) && $a -eq 0 && $b -eq 0 ]]
then
cat "$CATALOG"serverlist_n | "$WC"wc -w >> "$CATALOG"mail
cat "$CATALOG"serverlist_old | "$WC"wc -w >> "$CATALOG"mail
cat "$CATALOG"mail | sendmail -t
echo "------------" >> "$CATALOG"log
DATENOW=$(date +%d-%m-%Y_%T)
echo $DATENOW >> "$CATALOG"log
cat "$CATALOG"serverlist >> "$CATALOG"log
cat "$CATALOG"serverlist_n | "$WC"wc -w >> "$CATALOG"log
cat "$CATALOG"serverlist_old | "$WC"wc -w >> "$CATALOG"log
echo "------------" >> "$CATALOG"log
fi
if [[ $a -eq 0 ]]
then
cat "$CATALOG"serverlist_n > "$CATALOG"serverlist_old
fi
cat /dev/null > "$CATALOG"serverlist_n
exit
fi


CATALOG = "/ share / Web /" - Specify the directory where this script is located
NMAPCAT = "/ share / MD0_DATA / .qpkg / Optware / bin /" - Specify nmap
WC = "/ mnt / ext / usr / bin / if necessary " - The path to the wc application

The contents of the servers file are:
 
192.168.1.1:80
192.168.1.2:25
192.168.1.3:110


In the log we get the entries:
 
------------
12/29/2013_12: 42: 41
192.168.1.1:80
      5
      0
------------
------------
12/29/2013_12: 58: 19
192.168.1.1:80
192.168.1.2:25
     10
      5
------------


I would like to add about the possibility of monitoring on sms.
Using mainly mail.ru mail, I configured the filter to receive letters so that if the subject contains the word sms , then such a message is marked with a red flag in the list of letters in the Inbox folder, and the copy is sent to the phone as an SMS at 38068594xxx2@sms.kyivstar .net.
In Kyivstar tariff (Beeline UA) it is possible to receive 150 sms and 50 mms monthly for 7 kopecks a day.

The monitoring script can be run via cron on a router, satellite receiver running Linux or on a NAS drive. In this case, the script is running on the QNAP TS-419PII NAS.
As a result, we get every minute monitoring of all your servers for nothing.

In the digression, I want to write, if the “Receive SMS / MMS from e-mail” service is already connected on the Kyivstar network, you can also create a filter by the word mms in the subject of the message and receive messages up to 1 Mb in total without the sender's fee for mms.

This is my first bash script that I redid many times over the course of a week. I hope for constructive criticism and your comments.

Also popular now: