chimni has asked for the wisdom of the Perl Monks concerning the following question:


Hi Monks,
I am trying to write a script to monitor the status of webservers in a subnet.
#! /usr/bin/perl use strict; use warnings; use Parallel::ForkManager; my $pm = new Parallel::ForkManager(20); foreach my $ip (0..255) { my $ip_add = sprintf("202.1.150.%s",$ip); #print $ip_add; my $pid = $pm->start and next; my @val = `nmap -sT $ip_add | grep http`; #print @val; if (@val) { foreach (@val) { my $test = (split(/\s+/,$_))[1]; if ($test =~ m/open/) { print "Web Server at $ip_add is runnin +g.\n"; last; } } } else { print "Web Server at $ip_add is Down.\n"; } $pm->finish; } $pm->wait_all_children; exit 0;

The problems i am facing are.
1)How to identify between webserver down(when it should not be) and no webserver on the machine

2)How to handle a situation where the webserver is running on some port other than default.
3)are there any tools or script resources i can use
Thanks,
chimni

Replies are listed 'Best First'.
Re: Monitor webserver status on all machines in a subnet
by Abigail-II (Bishop) on May 27, 2004 at 11:21 UTC
    How to identify between webserver down(when it should not be) and no webserver on the machine
    The only way to do that is to let the script have some knowledge which machines should run a webserver, and which ones shouldn't.
    How to handle a situation where the webserver is running on some port other than default.
    About the only way to do so is to check all possible ports to see if 1) something is listening, and 2) if that something talks HTTP.
    are there any tools or script resources i can use
    I see you are using nmap. nmap is great to determine what kinds of ports are listened on, and can determine lots of information related to IP, TCP and UDP. It does however not have lots of capabilities for application layers - just some stuff for RFC and proxy FTP. The fact that something is listening on port 80 doesn't mean the webserver is healthy. Unless you actually do a request and get a reponse, you'll know it. You might want to do a 'HEAD http://$ip/' and read the first 3 bytes of the response - although you might get an HTTP error code due to virtual hosting.

    Abigail

Re: Monitor webserver status in a subnet
by borisz (Canon) on May 27, 2004 at 10:53 UTC
    are there any tools or script resources i can use
    nmap does this all for you. And to use it from perl look at Nmap.
    Boris
Re: Monitor webserver status on all machines in a subnet
by cLive ;-) (Prior) on May 27, 2004 at 11:18 UTC
    Nagios is also very good - if you want a graphical monitoring system.

    cLive ;-)

Re: Monitor webserver status on all machines in a subnet
by Chady (Priest) on May 27, 2004 at 13:00 UTC

    Just as Abigail has pointed out, you have to issue a request to the webserver to see if it is really running, and not just something listening on the port. ex:

    require LWP::UserAgent; my $ua = LWP::UserAgent->new(timeout => 10, agent => 'subnet monitor' ); my $request = HTTP::Request->new('HEAD', "http://perlmonks.org/"); my $contents = $ua->request($request); print $contents->{_rc} == 200 ? "Webserver Up" : "Webserver Down";

    Update: As for if the server is running on a different port, you might want to experiment with Net::Telnet

    Possibly something like this:

    sub check { my ($host, $port) = @_; my $telnet = new Net::Telnet (errmode=> 'return'); my $return = $telnet->open(Host => $host, Port=> $port); unless ($return) { print "Nothing running on port $port.\n"; return; } my $res = $telnet->getline; $telnet->print("HEAD / HTTP/1.0\n\n"); $res = $telnet->getline; if ($res =~ /200 OK/) { print "Webserver Up\n"; } else { print "Webserver Down\n"; } $telnet->close(); }

    He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.

    Chady | http://chady.net/