Here's a perl script that I use to just determine if my lines and routers
are up.  You can easily modify it to suit your needs.  A simple system
call would restart the webserver:
system("/etc/init.d/apache stop && /etc/init.d/apache start");

I run this in /etc/cron.d every minute and dump the results in a log upon
which I run logcheck.

I think it uses a default telnet timeout of 10 seconds.  look at man
Net::Telnet to specify a timeout.  It pings some hosts, and telnets to
port 80 on some webservers.  Upon connecting to a webserver it issues,
"GET / HTTP/1.0" and expects the first line to be "HTTP/1.[01] 200 OK".
If not it prints an error.  Note that some icmp is lost, being an
unreliable protocol.  So for ping tests I try about 15 pings and if I
get at least 5 then I figure my line is up.  Eventually I'd like to have
this dialup to an isp and email-page me if my line is really down.
Right now it just emails me.

Cory


line_monitor.pl

#!/usr/bin/perl -w
#Used to monitor t1 facilities as well as local webserver

use strict;
use Net::Ping;
use Net::Telnet;

sub ping ($);
sub port_test($$);

my $hostname = `hostname`; chomp($hostname);
my $progname = `basename $0`; chomp($progname);
my $date = `date +"%b %e %T"`; chomp($date);

my %ping_hosts = (  "123.456.789.00",   "otherside_of_t1_router",      
                    "123.456.789.00",   "next_hop_router",      
                    "64.58.76.227",     "www.yahoo.com",
                    "128.223.142.13",   "www.uoregon.edu"
                    );

# Use ping_hosts to determine if line is up
my $linetest=5;
for my $i (1..3) {
  foreach(keys %ping_hosts) {
    $linetest-- if &ping($_);       # If can get 5 pings, line is probably up
  }
}
if($linetest>0) {
    system("traceroute www.yahoo.com -n -w 2 -m 10 2>&1"); 
        # traceroute to yahoo    
    print "$date $hostname $progname: $ping_hosts{$linetest} ICMP Failed - Line 
Down\n";
}


# Test webservers for connectivity
#my $retry = 0;
my %www_hosts = (   "192.168.0.20",        "local_webserver",
                    "128.223.142.13",   "www.uoregon.edu"
                    );

foreach(sort(keys %www_hosts)) {
    my $r = &port_test($_, 80, "GET / HTTP/1.0\n\n");
    unless ($r && $r=~m{HTTP/1.[01] 200 OK}) { 
        $r="" unless $r; 
        chomp($r); 
        print "$date $hostname $progname: $www_hosts{$_}:$_ WWW failed
$r\n";
    }
}

exit(0);


#####################
# Functions 
#####################

sub ping ($) {
    my $host = $_[0];           # Host to ping

    my $p = Net::Ping->new("icmp") or die "Can't create new ping object:
$!\n";

    if ($p->ping ($host)) { return 1; }
    else { return 0; }
    $p->close();
}

sub port_test($$) {
    my ($host, $port, $put) = @_;
    
    my $t = new Net::Telnet(Host => $host, Port => $port, 
            Errmode => "return", Telnetmode => 0);
    return 0 unless $t;

    $t->put($put) if $put;
    
    my $line = $t->getline();
    $t->close();
    return $line;
}









On Fri, Dec 13, 2002 at 12:12:20PM -0800, Tim Howe wrote:
> I have a heavily used web server that has been deciding to simply stop serving pages 
>for up to 20 minutes at a time...  I have already tried all manner of Apache and OS 
>tweeks to stop this but nothing seems to work.  Restarting the server puts everything 
>back on track.  What I would like to do, until I find a real fix, is have a Perl 
>program try to connect to that machine on port 80, and if it fails to get a page 
>within, say, 5 seconds, to restart the server.
> I have found a bunch of modules that will ping and or connect to web servers, but 
>none of them seem to have a good way to time the response.  Any suggestions?
> 
> TimH
> _______________________________________________
> Eug-LUG mailing list
> [EMAIL PROTECTED]
> http://mailman.efn.org/cgi-bin/listinfo/eug-lug
> 
_______________________________________________
Eug-LUG mailing list
[EMAIL PROTECTED]
http://mailman.efn.org/cgi-bin/listinfo/eug-lug

Reply via email to