By Tuxette

(Transferred from the wiki by Peter)


Here is how you use it:

Code:
# monitor-website.pl -e my@email-address.com http://www.website.com

  1. The "-e" is the email address that should receive notifications.
  2. You can also use the "-s" option to look for a specific string on the web page. The string should be in quotes, but will work without them if there ar no spaces or special characters.
  3. I purposely didn't use any perl modules so that it would work on any system.
  4. It works first by using "curl -I" to see if the site returns a "200 OK" message in the page header, and if it does, it will use curl to get the entire page searching for </html> at the end unless "-s" specifies some other string.
  5. monitor_email is the email address the warning email appears to come from in the envelope.
  6. If your web page does a redirect, for example from http://www.website.com/about to http://www.website.com/about/ the script will fail as it only looks for the "200 OK" code, not the "302" redirect code. So first run "curl -I URL" first to see if a 200 OK is returned.
  7. Here is the original forums page for this script for your comments.

Code:
#!/usr/bin/perl

    use Getopt::Std;



    # Define the address the alerts should come from

    $monitor_email              = "monitor@monitor.com";

    # Define global variables
    $curl_executable            = "/usr/bin/curl";
    $mail_executable            = "/usr/sbin/sendmail";
    $OPT_STRING                 = 'hs:e:';

    # Start program
    &main();


sub main {

  # Read in the options
  getopts( "$OPT_STRING", \%opt ) or usage();

  # Set variables
  my $url_header        = "";
  my $url_body          = "";
  my $email_address     = "";
  my $url               = $ARGV[0];
  my $valid_string      = "</html>";

  # Determine options
  if ($opt{s}) { $valid_string = $opt{s};}
  usage() if $opt{h};
  usage() if !$opt{e};
  
  # Define more variables
  $email_address = $opt{e};

  # Monitor URL
  $url_header = `$curl_executable -I -s $url`;

  if ($url_header =~ /200 OK/){
        $url_body = `$curl_executable -s $url`;
        if ($url_body =~ /$valid_string/){
                #print ("$url_header\n");
        }
        else{
                &send_warning($email_address, $url);
        }
  }
  else{
        &send_warning($email_address, $url);
  }

}

sub send_warning {

  my ($email_address,$url) = @_;

  # Create timestamp
  my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
  $year = $year + 1900;
  $mon  = $mon + 1;
  my $time_stamp = $year."-".$mon."-".$mday." ".$hour.":".$min.":".$sec; 
 
  my $email_content = $url ." failure at ". $time_stamp . "\n";

  unless(open (MAIL, "| /usr/sbin/sendmail -t")) {
        print "error.\n";
        warn "Error starting sendmail: $!";
  }  
  else{
        print MAIL "From: " . $monitor_email . "\n";
        print MAIL "To: " . $email_address . "\n";
        print MAIL "Subject: Website URL Monitor Failure\n\n";
        print MAIL $email_content;
        close(MAIL) || warn "Error closing mail: $!";
        print ("$email_content");
  }
}

sub usage {

print STDERR << "EOF";

    This program monitors HTTP URLS

    usage: $0 [-h] [-s "string"]

     -h                   : This (help) message

     -e email-address     : The email address that should be alerted.

     -s                   : Unique string found on the web 
                            page that proves it is working correctly

    example: $0 -s -e email-address URL


EOF
    exit;
Code:
}