On 03.01.24 15:34, Christopher Schultz wrote:
Olaf,

+1

The regular expression used with grep should be improved a lot.

I would recommend at least the following:

STAT=`netstat -luptn 2>/dev/null | grep '^tcp.*:8080[^:0-9]' | awk '{print $6}'`

...or omit the UDP output by using "netstat -lptn" to begin with ;)

The improved regexp will ignore non-TCP ports and will only match on a proper port-number by requiring the presence of a : and being followed by anything other than a : (which would indicate it's an IPv6 address) or more numbers (which could be a port number like 80800 or more of an IPv6 address).

wouldn't the :8080[^:0-9] still hit on an IPV6 address /ending/ in 8080? Still an improvement, as it'd be 1 false alarm per 4294967296 IPV6 addresses ;)

netstat is a pretty crude tool to be used, here. Why not just connect to the service on port 8080 and see if it responds? The process listening on the port doesn't guarantee it's actually able to serve any requests...

Now I'm +1'ing, and can add:

As you said this, you triggered my memory: My toolbox has this gem, checking for http-status 200:

|#!/bin/bash status_code=$(curl --write-out %{http_code} --silent --output /dev/null http://localhost:8080/) if [[ "$status_code" -ne 200 ]] ; then echo "Tomcat Status: $status_code" | mail -s "Tomcat Down?" "someb...@example.com" -r "STATUS_CHECKER" else exit 0 fi |

Olaf

Reply via email to