#!/usr/bin/perl # Created by Michael Wood of http://www.michaelwood.me.uk # Released under the GPL licence http://www.gnu.org/licenses/gpl-3.0.txt ####### setup info ########## $hostname = 'myhostname'; # current server hostname $email_to = 'your@email'; # recipient of email MUST BE IN SINGLE QUOTES $from = 'http-monitor@youremail.com'; # Email FROM address MUST BE IN SINGLE QUOTES @urls = ("http://www.yoururls.com/dindex.html","http://www.yoururls2.com/dindex.html");#comma seperated list of urls # you don't really need to change these $email_subject = 'IMPORTANT: HTTP Server Failure'; # Email subject text $email_body = "Report:\n-------------\n$hostname failed to connect"; #Email body text ###### end of setup ######### 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 die "Can't fork for sendmail: $!\n"; print MAIL <<__STOP; From: $from To: $to Subject: $subject $body __STOP close(MAIL) or warn "mail didn't close nicely"; } # Test HTTP connection via a HTTP GET function sub testhttp { my ($urls) = @_; $req = new HTTP::Request 'GET',$url; # send the request $res = $ua->request($req); $datetime = gmtime; if ($res->is_success) { print "HTTP Check $url PASSED at $datetime\n"; } else { print "HTTP Check $url FAILED at $datetime\n"; $error = $res->status_line; $email_body .= " when attempting to access url $url at $datetime \nError was $error \n\nRegards http-monitor"; #this needs to be here to get the $url varible for the instance #print $email_body; # for debug or something send_email($email_to, $email_subject, $email_body, $from); } } foreach $url (@urls) #for each url specified { testhttp($url); } 0;