I was looking for an easy way to keep an eye on the status of some of my servers. Since I didn't have the patience to search for a solution which matched my needs, I wrote this little perl script. Download it, paste it in your favorite text editor, and save it somewhere on your path as is_tcp_port_listening.pl. Make it executable (chmod 755 is_tcp_port_listening.pl), and then it can be invoked with this command:
$ ./is_tcp_port_listening.pl 'hostname' 'portnumber'
The script simply tries to connect to a server on a TCP port. If the connection can be established, it will return the value 0, which means in a UNIX System that everything is okay. If it's not possible to connect in a definable time, it will stop its attempts and return the value 1. You can set the timeout by changing the following line in the source code:
my $timeout = 3;
This for example sets the timeout to three seconds, which might be a bit short if your server and/or your internet connection is slow.
Now this might sound a bit technical as is. But if you combine this script with GeekTool, you can easily draw a small green or red icon onto your desktop, which indicates whether your server is up or not. In my example, I might want to create a 'shell' entry which invokes the command is_tcp_port_listening.pl www.my-http-server.com 80 every 60 seconds, and check 'Hide output' and 'Show icon.' The result can be seen in the screenshot at right.
It is, of course, possible to use any available service on your server which listens on a TCP port. Instead of 80, you might want to use 5900 if you are running a VNC server, or just any TCP-based service your server is running.
Et voilá, you have everything under control. No problems anymore, if someone accidentally shuts off your server or if you forgot to pay the bill of your internet service provider. Okay, even with this handy little script you may still not be able to solve the problem, but at least you know when you are in trouble.
Mac OS X Hints
http://hints.macworld.com/article.php?story=20060221135557761