📜 ⬆️ ⬇️

Primitive port monitoring with SMS notification

image

The issue of remote monitoring of the company's services for me was always very acute. And what I just did not try. Nagios, Zenoss, Zabbix, and so on. Replaced each other, striking their capabilities and features. They promised to scan, embrace and watch day and night, and in case of which, notify immediately. But unfortunately, I never had enough time to set up these combines, and monitoring was needed here and now. So one day I sat down and wrote the following script.

The principle of operation is simple. The script is launched after a certain period of time and telnet is connected to the resources from the list. After three "non-responses" SMS is sent.

Working conditions:
Platform - Ubuntu 11.04 ( most likely will work in other versions of Linux ).
Notification service - smstools ( if not needed - sudo apt-get install smstools ).
Connected via USB cable GSM phone.
Resource File Type - host port 0
for example
ya.ru 80 0
mail.ru 110 0
A computer with a script should look in the Internet through another provider.
')
My script runs every three minutes from crontab.
* / 3 * * * * '/ opt / portmonitor / telneter'

Below is the code itself. As always, do not judge strictly, we are not programmers.

 #!/bin/bash path=/opt/portmonitor/ logfile=/var/log/portmon fulltext=(`cat $path/ipbase.txt`) check=(`cat $path/count.txt`) rm $path/count.txt a=0 b=`expr $a + 1` e=`expr $b + 1` while [ ${fulltext[$a]} ] do send=(${fulltext[$a]}":"${fulltext[$b]}"-"'not answer 3 time') elem=("${fulltext[$a]}"\ "${fulltext[$b]}") ################subroutines#################################### bad (){ if [ "${check[$e]}" -lt "2" ];then zap[$i]=$elem ((check[$e]++)) #echo 'schetchik +:'${check[$e]} echo "${zap[$i]}"\ "${check[$e]}">>$path/count.txt ((i++)) a=`expr $a + 3` b=`expr $b + 3` e=`expr $e + 3` elif [ "${check[$e]}" -eq "2" ];then zap[$i]=$elem ((check[$e]++)) echo "${zap[$i]}"\ "${check[$e]}">>$path/count.txt gsmsendsms -d /dev/ttyACM0 +79xxxxxxxxx "$send" gsmsendsms -d /dev/ttyACM0 +79xxxxxxxxx "$send" gsmsendsms -d /dev/ttyACM0 +79xxxxxxxxx "$send" echo "`date` $send" >>$logfile ((i++)) a=`expr $a + 3` b=`expr $b + 3` e=`expr $e + 3` else zap[$i]=$elem echo "${zap[$i]}"\ "${check[$e]}">>$path/count.txt ((i++)) a=`expr $a + 3` b=`expr $b + 3` e=`expr $e + 3` fi } good () { zap[$i]=$elem check[$e]=0 echo "${zap[$i]}"\ "${check[$e]}">>$path/count.txt ((i++)) a=`expr $a + 3` b=`expr $b + 3` e=`expr $e + 3` } #################endsubroutines#################################### nc -w2 -z ${fulltext[$a]} ${fulltext[$b]} && good || bad done 
#!/bin/bash path=/opt/portmonitor/ logfile=/var/log/portmon fulltext=(`cat $path/ipbase.txt`) check=(`cat $path/count.txt`) rm $path/count.txt a=0 b=`expr $a + 1` e=`expr $b + 1` while [ ${fulltext[$a]} ] do send=(${fulltext[$a]}":"${fulltext[$b]}"-"'not answer 3 time') elem=("${fulltext[$a]}"\ "${fulltext[$b]}") ################subroutines#################################### bad (){ if [ "${check[$e]}" -lt "2" ];then zap[$i]=$elem ((check[$e]++)) #echo 'schetchik +:'${check[$e]} echo "${zap[$i]}"\ "${check[$e]}">>$path/count.txt ((i++)) a=`expr $a + 3` b=`expr $b + 3` e=`expr $e + 3` elif [ "${check[$e]}" -eq "2" ];then zap[$i]=$elem ((check[$e]++)) echo "${zap[$i]}"\ "${check[$e]}">>$path/count.txt gsmsendsms -d /dev/ttyACM0 +79xxxxxxxxx "$send" gsmsendsms -d /dev/ttyACM0 +79xxxxxxxxx "$send" gsmsendsms -d /dev/ttyACM0 +79xxxxxxxxx "$send" echo "`date` $send" >>$logfile ((i++)) a=`expr $a + 3` b=`expr $b + 3` e=`expr $e + 3` else zap[$i]=$elem echo "${zap[$i]}"\ "${check[$e]}">>$path/count.txt ((i++)) a=`expr $a + 3` b=`expr $b + 3` e=`expr $e + 3` fi } good () { zap[$i]=$elem check[$e]=0 echo "${zap[$i]}"\ "${check[$e]}">>$path/count.txt ((i++)) a=`expr $a + 3` b=`expr $b + 3` e=`expr $e + 3` } #################endsubroutines#################################### nc -w2 -z ${fulltext[$a]} ${fulltext[$b]} && good || bad done

If someone lacks interactivity, you can display a kind of monitor:

Watch cat /opt/portmonitor/count.txt

Get about this statistic
image

Only a few say that these ports are not available from resources.

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


All Articles