From: Jason Ya hoo <[EMAIL PROTECTED]>

> Do you have any way or perl code to test Web Server is
> still alive instead of using Internet Browser to
> browse Web Server page every period. Thank you for
> your help.

I use something like this:

....
use LWP::UserAgent;
my $ua = new LWP::UserAgent;
$ua->timeout(20);

my %last_result;
foreach my $url (@urls) {
        $last_result{$url} = 'OK';
}

....
        # the following code gets run every N minutes
        foreach my $url (@urls) {
                my $baseurl = $url;
                Log "\tRequesting $url";
                $url =~ s{(http://[^/:]+)/}{$1:80/};
                        # the line above is there only because of some problems with
                        # LWP in scripts compiled by PerlApp. You probably do not need 
it
                        # there.
                $url .= '?time='.time();
                        # I assume that the page will ignore the parameter.
                        # this is just to make sure I do not get the page from a
                        # cache/proxy

                my $request = HTTP::Request->new(GET => $url);
                my $response = $ua->request($request);

                if ($response->is_success) {
                        my $content = $response->content();

                        LogNT "\t\t$content";

                        if ($last_result{$baseurl} ne $content) {
                                $last_result{$baseurl} = $content;
                                LogNT "\t\tmailing!";

                                ref $sender->MailMsg({subject => "[JOBVIPeR ASP] " . 
Win32::NodeName(),
                                        msg => "$baseurl => $content\n at 
".localtime(),
                                        to => $mailto,
                                }) or Log "Error in Mail::Sender : 
$Mail::Sender::Error";
                        }

                } else {
                        my ($code,$message) = ( $response->code(), 
$response->message());
                        chomp($message);
                        LogNT "\t\t$code : $message";
                        if ($last_result{$baseurl} != $code) {
                                $last_result{$baseurl} = $code;
                                LogNT "\t\tmailing!";

                                if ($code == 500 and $message eq 'read timeout') {
                                        ref $sender->MailMsg({subject => "[JOBVIPeR 
ASP] HANGS@" . 
Win32::NodeName(),
                                                msg => "$baseurl at ".localtime(),
                                                to => $mailto,
                                        }) or Log "Error in Mail::Sender : 
$Mail::Sender::Error";
                                } else {
                                        ref $sender->MailMsg({subject => "[JOBVIPeR 
ASP] ERROR@" . 
Win32::NodeName(),
                                                msg => "$baseurl => $code : $message\n 
at ".localtime(),
                                                to => $mailto,
                                        }) or Log "Error in Mail::Sender : 
$Mail::Sender::Error";
                                }
                        }
                }
        }
....


It's part of a bigger service that among all things watches the web 
servers and reports if any of them hangs or goes down.

Log() writes the message to a log file and appends timestamp,
LogNT() only writes to log file.


Hope this gives you an idea, Jenda
=========== [EMAIL PROTECTED] == http://Jenda.Krynicky.cz ==========
There is a reason for living. There must be. I've seen it somewhere.
It's just that in the mess on my table ... and in my brain
I can't find it.
                                        --- me


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to