S wrote:
>
> >From the naive:
> What is the most efficacious method of pinging a web server URL for its
>status
> regarding the viability of a specific page ?
>
> appreciatively,
> scott
Alternatively you could do something like below... note that this is a
cut down version but most of what you want is there...
cheers
Sean
use strict;
# not sure how many of these are really needed here...
use Carp;
use Fcntl qw(:DEFAULT :flock);
use MIME::Base64;
use POSIX qw(uname);
use Socket;
use Mail::Mailer;
use LWP::UserAgent;
use HTTP::Request;
use HTTP::Response;
#######################################################
#
# sub fpget($URL) - fetch URL or else complain...
#
sub fpget($$) {
my $URL= shift;
my $redirect_url = shift;
my $URL_timeout = 30;
my $URL_retries = 3;
my $URL_email_list = "<fred\@silly.net>, <john\@silly.net>";
# create the user agent object
my $ua = new LWP::UserAgent;
$ua->agent("daft_agent/1.0 " . $ua->agent);
$ua->timeout($URL_timeout); # seconds
# if a proxy is needed then it would go something like...
# $ua->proxy('http', 'http://proxy.silly.net:8080/');
# $ua->no_proxy('silly.net');
# create the request
my $req = HTTP::Request->new(GET => $URL);
$req->header('Connection' => 'Close');
my $res;
my $for_res;
for (my $i=0; $i < $URL_retries; $i++) {
$res = $ua->request($req);
if ($res->is_success) {
$for_res = $res->content;
last;
} else {
warn "The HTTP GET($URL) failed!\n\n";
}
}
unless ($res->is_success) {
warn "URL failed " . $URL_retries .
" times, mailing $URL_email_list now.";
# as per recipe 17.8 from the Ram book. get the
# fully qualified name of this machine
# my $hostname = hostname();
my $hostname = (uname)[1];
my $address = gethostbyname($hostname)
or warn "Couldn't resolve $hostname: $!";
$hostname = gethostbyaddr($address, AF_INET)
or warn "Couldn't re-resolve $hostname : $!";
# send a mail to the appropriate people warning them about
# this problem....
my $mailer = Mail::Mailer->new("sendmail");
$mailer->open({ From => "wibble\@silly.net",
To => $URL_email_list,
Subject => "URL GET Failure",})
or die "Can't open: $!\n";
print $mailer "At ". scalar(gmtime) . " GMT, URL fetch
failed\n";
print $mailer "from $hostname after " . $URL_retries . "
attempts\n";
print $mailer "trying to fetch using url:\n";
print $mailer "$URL\n";
return "Temporarily unavailable";
}
# here we mung the page for our own purposes...
return $for_res;
}