📜 ⬆️ ⬇️

Script monitoring the availability of ports on the network with notification by email (bash)

It just so happened that we live, albeit in an advanced country with all kinds of technologies, but the stability of the Internet connection for cities that are far from the capital is a rare phenomenon. Long chose various services for professional monitoring of the availability of equipment on the network. Some have only 80 port checks on the web server, others may 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 naturally the amount of equipment that needs to be monitored.

But I came across a good script Monitoring ports with a notification to the sysadmin , of course 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 changed and modified the proposed script somewhat.

- The script began to send not only a letter about the unavailability of the port, but also about its appearance on the network.
- The script sends an email to the mail and logs the time, the number of errors during the scan and the previous state of the scan.
- The script sends a letter to the mail only if there were five unsuccessful connection attempts or five successful ones after the unavailability.
- The script does not send a letter if the Internet is missing 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 /" - If necessary, specify the location of nmap
WC = "/ mnt / ext / usr / bin /" - Path to the wc application

The content of the servers file is:
 
 192.168.1.1:80
 192.168.1.2:25
 192.168.1.3:110


In the log we get the record:
 
 ------------
 29-12-2013_12: 42: 41
 192.168.1.1:80
       five
       0
 ------------
 ------------
 29-12-2013_12: 58: 19
 192.168.1.1:80
 192.168.1.2:25
      ten
       five
 ------------


I would like to add about the possibility of monitoring and sms.
Using mostly mail.ru mail, I set up a filter to receive letters so that if the subject contains the word sms , then the letter is marked with a red flag in the list of letters in the Inbox, and a copy is sent to the phone as an SMS at 38068594xxx2@sms.kyivstar .net.
In the 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 a QNAP TS-419PII NAS drive.
As a result, we get every minute monitoring of all your servers almost for nothing.

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

This is my first bash script that I reworked many times during the week. I hope for constructive criticism and your comments.

Source: https://habr.com/ru/post/208116/


All Articles