Remote HTTP response alive tester and monitor thing version2

Just modified my perl Remote HTTP response alive tester and monitor thing.

  • Makes a HTTP GET request to a list of URLS or just one
  • Sends an email if a URL can not be accessed with the error details in the email
  • Outputs status of URL to terminal
  • 6 lines to configure it :D
  • Doesn’t use any weird Perl modules :D

It now has DNS capabilities which means you’ll need the perl dns module to use it. Ubuntu and Debian systems will need to install the package libnet-dns-perl (it’s probably the same named packge on other distro’s too)

  • Uses libnet dns to tell you what the given url is resolving as :D

I added this because it’s useful to see which actual server is being queried, ie if one goes down and the dns is changed over to another server it will show this and make sure that the same url is accessible.

If you run it as a cron job it will output this to the stdout so you could log it if you wanted

michael@fornax:~/scripts$ perl ./alivecheck-w-DNS.pl
Tue Mar 11 22:41:43 2008 HTTP Check http://www.url.org.uk/index.php PASSED
Tue Mar 11 22:41:43 2008 HTTP Check http://www1.url.org.uk/index.php PASSED
Tue Mar 11 22:41:43 2008 HTTP Check http://www2.url.org.uk/index.php PASSED
or if there is a fail:

michael@fornax:~/scripts$ perl ./alivecheck-w-DNS.pl
Tue Mar 11 22:52:50 2008 HTTP Check http://www.url.org.uk/index.php PASSED
Tue Mar 11 22:52:50 2008 HTTP Check http://www1.url.org.uk/index.php PASSED
Tue Mar 11 22:52:50 2008 HTTP Check http://www2.url.org.uk/index.php DNS Check www2.url.org.uk resolved as xxx.xxx.xxx.xxx FAILED

Email output looks this:

Report:
————-
Your.Server.something failed an http connect
FAILED Tue Mar 11 19:33:01 2008 HTTP Check http://www.url.org.uk/index.php DNS Check www.url.org.uk resolved as xx.xxx.xxx.xxx
FAILED Tue Mar 11 19:36:02 2008 HTTP Check http://www1.url.org.uk/index.php DNS Check www1.url.org.uk resolved as xx.xxx.xxx.xxx

regards, http monitor

Obviously it will only email you if the check fails.

Fill in the configuration at the top of the perl script, you’ll also need to count the number of characters after the domain, by default it is set to “-10″ which means it removes “/index.php” from the url to be able to parse it in the resolve domain bit. Change the “-10″ to how ever many characters proceed the domain part of the url.

If anyone has some nice regex or something to automagically work out the proceeding characters that would be a nice addition 12/03/08 THANKS Seeker`/Chris, also the $hostname variable could be populated by its self (although in the case of shared hosting it’s not so practical so it needs to be able to be over written)

Download it here (copy and paste not recommened)

#!/usr/bin/perl

####### setup info ##########

$hostname = ‘your.server.com’; # current server hostname
$email_to = ‘you@yourdomain.com’; # recipient of email MUST BE IN SINGLE QUOTES
$email_subject = ‘IMPORTANT: HTTP Server Failure’; # Email subject text
$email_body = “Report:\n————-\n$hostname failed an http connect \n”;
$from = ‘http-monitor@yourdomain.com’; # Email FROM address MUST BE IN SINGLE QUOTES
@urls = (”http://www.wwfweff.org.uk/ind1ex.php”,”http://www1.ursdvcvl.org.uk/index.php”,”http://www2.ur43trgl.org.uk/index.php”);#your list of urls

###### end of setup #########

use Net::DNS;
use LWP::UserAgent;
$ua = new LWP::UserAgent;

# Send Email function

sub send_email($$$$)
{
my ($to, $subject, $body, $from) = @_;

$body =~ s/\\//g;
$to =~ s/@/\@/g;
open (MAIL, “|/usr/sbin/sendmail -oi -t”) or warn “Can’t fork for sendmail: $!\n”;
print MAIL <<__STOP;
From: $from
To: $to
Subject: $subject

$body

regards, http monitor

__STOP
close(MAIL) or warn "mail didn't close nicely";

}

# Test HTTP connection via a HTTP GET function

sub testservice
{

my ($urls) = @_;

$req = new HTTP::Request 'GET',$url;

# send the request
$res = $ua->request($req);
$datetime = gmtime;

if ($res->is_success)
{

print “$datetime HTTP Check $url PASSED \n”;
$email_body .= “PASSED $datetime HTTP Check $url “;
resolvedomain($url);

} else {

$error = $res->status_line;

$email_body .= “FAILED $datetime HTTP Check $url “;
resolvedomain($url);
print “$datetime HTTP Check $url DNS as $ip FAILED \n”;

$fails = 1;

}

}

sub resolvedomain
{
my ($urls) = @_;

#remove http:// and remove /index.php etc from url to get the domain name

$offset = index($url, “/”, 7);
if($offset != -1) {
$domain = substr($url, 7, $offset - 7);
} else {
$domain = substr($url, 7);
}

my $dnsreq = Net::DNS::Resolver->new;

my $query = $dnsreq->search($domain);

if ($query) {
foreach my $rr ($query->answer) {
next unless $rr->type eq “A”;
$dnsoutput = “DNS Check $domain resolved as “;
$ip = $rr->address;
$dnsoutput .=Â $ip;
}
} else {
$dnsoutput = warn “query failed: “, $dnsreq->errorstring, “\n”;
}
$email_body .= $dnsoutput;
$email_body .= “\n”;
}

foreach $url (@urls)Â Â Â #for each url specified
{
testservice($url);
}

if ($fails == 1) {
#print ($email_body);
send_email($email_to, $email_subject, $email_body, $from);
}

0;

Leave a Reply