Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Check server status remotely with a perl script Network
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.
    •    
  • Currently 3.63 / 5
  You rated: 1 / 5 (8 votes cast)
 
[31,023 views]  

Check server status remotely with a perl script | 15 comments | Create New Account
Click here to return to the 'Check server status remotely with a perl script' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Check server status remotely with a perl script
Authored by: thype on Feb 28, '06 07:24:42AM

www.bb4.org

Open source, the BBTF version, and monitors everything under the sun, and cross platform compatible.



[ Reply to This | # ]
Check server status remotely with a perl script
Authored by: pete_v on Feb 28, '06 01:22:38PM

why reinvent the wheel?

> nc $host $port

- see 'man nc' for more details

/Pete



[ Reply to This | # ]
Check server status remotely with a perl script
Authored by: mistersquid on Mar 01, '06 05:03:33AM

Pete, I appreciate your pointing to a standard UNIX utility to do this work, but your post reeks of programmer's laziness. Not only do you refer everyone to the "man" page for nc, but you provide no further details.

I'm moderately experienced issuing commands at the CLI, though I don't program in the shell. Like many, I program in PERL (or some other non-shell scripting language) and then execute from the command line. The beauty is I don't have to figure out the idiosyncrasies of every UNIX utility out there.

Would you mind explaining to those of us who do not program at the command line how one might use the functionality of nc to take some kind of action once a connection establishes or fails to establish?

From what I can tell, nc is interactive and the only way to do this as an automated task is to have a non-shell interface querying using nc.



[ Reply to This | # ]
Check server status remotely with a perl script
Authored by: foilpan on Mar 01, '06 11:00:42AM

i didn't interpret the previous post with any sort of hostility or "programmer's laziness," as you did.

since i'm not that familiar with netcat's usage, it took a minute or two of quick man page reading to see what it does. that's not so bad.

for instance, i just issued a <code>nc -vz 192.168.1.1 1-80</code> to scan the open ports on our main server here in the office (that's not the internal IP, but you get the idea). it works like a charm, showing which ports are open. If you try with a host that's down, it will report as such. neat-o.

i've used nmap to test for hosts' status, too.



[ Reply to This | # ]
Check server status remotely with a perl script
Authored by: rschwarz on Mar 01, '06 12:17:01PM
Thank you foilpan for your additional information on the usage of nc. Although I looked up the man page, it was not instantly clear to me, that this can be done so easily.

You can also set the timeout with the -w option. This is useful to me, since I don't want to wait a minute or so until netcat aborts.

[ Reply to This | # ]
left out stuff
Authored by: macubergeek on Mar 01, '06 03:42:47PM

you failed to mention that after you do nc whatever.com 80 you have to do
get /
to determine if it's live or not, or to at least pull the index.htm page.



[ Reply to This | # ]
Check server status remotely with a perl script
Authored by: gaoshan on Mar 05, '06 11:27:26PM
Doing nc and adding an extra -v or 2 for verbosity...i.e.
nc -vvvz 192.168.1.1 1-80
results in some pretty decent info. You get something like the following:
(UNKNOWN) [192.168.1.1] 80 (http) open
(UNKNOWN) [192.168.1.1] 79 (finger) : Connection refused
(UNKNOWN) [192.168.1.1] 78 (vettcp) : Connection refused
(UNKNOWN) [192.168.1.1] 77 (?) : Connection refused


[ Reply to This | # ]
Check server status remotely with a perl script
Authored by: xsa on Mar 01, '06 03:07:03AM
monit is another good utility for managing and monitoring, processes, files, directories and devices on a UNIX system and iss pretty easy to setup: http://www.tildeslash.com/monit/.

[ Reply to This | # ]
monit site is nonreachable
Authored by: macubergeek on Mar 01, '06 03:32:27PM

Ironicly the monit site wasn't reachable when I tried it ;-)



[ Reply to This | # ]
monit site is nonreachable
Authored by: xsa on Mar 01, '06 11:55:37PM

Yeah, life sucks.



[ Reply to This | # ]
Check server status remotely with a perl script
Authored by: gaoshan on Mar 05, '06 11:49:33PM
When configuring monit make sure you use:
./configure --without-resource
or make will throw an error.

[ Reply to This | # ]
Use hping2
Authored by: macubergeek on Mar 01, '06 03:29:28PM

sudo hping2 www.apple.com -c 1 -p 80

hping is a swiss army knife of a program. You can also use it to craft packets and to test firewalls. The above will also check to see if port 80 is reachable on host www.apple.com (for example).
You can use -c somebiggernumber to test latency as well. A little shell script wrapped around this can parse, slice and dice the output any way you want.



[ Reply to This | # ]
Use hping2
Authored by: gaoshan on Mar 05, '06 11:15:56PM

hping2 isn't on my machine (10.4.5). Do you have the server version of OS X? Regardless, it's available at Sourceforge and DarwinPorts even has hping3 available.



[ Reply to This | # ]
Check server status remotely with a perl script
Authored by: merlyn on Mar 04, '06 08:04:22AM

A Perl article with an author of "rschwarz". That's gonna get confusing. At least you spell it PERL, which every Perl hacker knows is not the proper spelling!



[ Reply to This | # ]
Check server status remotely with a perl script
Authored by: merlyn on Mar 04, '06 08:11:59AM

Oops. So sorry. It was mistersquid that spelled it as PERL. My apologies.



[ Reply to This | # ]