How to monitor a port opened on local LAN device? Need to get notification when the opened port stops reponding

Hi,
I have an application running on Windows10 PC. Something like web server. When healthy, it responds on port 80 to incoming requests. Sometimes the app process fails and stops responding. I need to get email notification when it happens. I have set Windows Services Notification but it hardly works.
I guess I could use nmap on Omnia but have no idea how to set it up. The algorithm will be like

  • check 192.168.1.2:80 every minute
  • in case of no response send email to blah@blah.net

Furthermore, it would be awesome if I could have USER1 LED green when it works and turn red when it fails.

Eventually, could Device detection (package dev-detect) be used to achieve that?

how about something really naive (this is straight from top of my head):
just putting this into crontab:

curl -sk https://192.168.1.2:80 -m3 > /dev/null && (echo "rainbow_green_led_command_placeholder" 2>/dev/null||:) || (echo "rainbow_red_led_command_placeholder"; echo "email_script_placeholder")

-m3 is a total timeout before the curl command fails (for cases, where it connects to the opened port, but the server is not sending anything back)

Good point :slight_smile:
In this case I get always …red… because the response is curl: (56) Recv failure: Connection reset by peer in case of success. In fact it is not web server but storj service:

root@turris:~# curl 192.168.1.10:28967 -m3
curl: (56) Recv failure: Connection reset by peer
root@turris:~# curl 192.168.1.10:2896 -m3
curl: (28) Connection timed out after 3001 milliseconds
root@turris:~#

The first response is good and returns immediately, the second one is an example of bad one and is waiting for timeout.
Is it able to work with that?

Or use netcat (only checks if any software listens on port, but I think this is sufficient in your case):

/usr/bin/nc -z -w $timeout $host_ip $port

You might need to install full netcat through opkg as busybox netcat doesn‘t support enough options if I am correct…

then add curl command to a conditional and aim for an exit code. curl uses different exit codes for different states.

curl -sk https://192.168.1.2:80 -m3 > /dev/null; [ $? -eq 56 ] && (echo "rainbow_green_led_command_placeholder" 2>/dev/null||:) || (echo "rainbow_red_led_command_placeholder"; echo "email_script_placeholder")

this is getting complex :wink:

2 Likes

Yes the busybox version of Netcat does not make it. I will stick with curl for now, since it does not require installation (it is in Omnia by default).

yes it works :slight_smile:
What does 2>/dev/null||: do?

2>/dev/null hides the stderr output
and statement <command>||: always returns 0, even in the case of <command> returning non-zero r.c.
I put it there not to trigger the Service failure scenario (red led+email alert) in case your service is okay, but the green LED script is malfunctioning.

…at least I hope it does that :wink:

Finally it works. Needed to learn cron and msmtp too :slight_smile:
In case anyone in future will search for similar goal, here are the steps

  • command: */1 * * * * curl -sk 192.168.1.10:28967 -m3 > /dev/null; [ $? -eq 56 ] && (rainbow usr2 green enable 2>/dev/null||:) || (rainbow usr2 red enable; echo -e "Subject: STORJ IS DOWN\r\n\r\nstorj down" |msmtp --from=from@email.cz -t to@email.cz) needs to be entered to crontab, also available in LUCI at http://192.168.1.1/cgi-bin/luci/admin/system/crontab
  • in /etc/msmtprc add “host smtp.someprovider.cz” (need SMTP server running on standart port without authentication, usually provided by your ISP)

It checks the port 28967 on machine 192.168.1.10 every minute, keep USR2 light green, in case of port unreachable it turns USR2 to red and sends an email notification every minute until the port is up again.

Thanks much to rplevka

1 Like