#!/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;