Results 1 to 4 of 4

Thread: Simple Website Monitoring Script in Perl

Hybrid View

  1. #1
    Junior Member tuxette's Avatar
    Join Date
    Jan 2006

    Simple Website Monitoring Script in Perl

    Canela inspired me to contribute a little perl script I wrote to monitor my webserver.

    Here is how you use it:

    # -e
    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 to 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 wiki page for future modifications.

    It is not heavy duty, but should work well for a personal site. Let me know if it works OK for you!

         use Getopt::Std;
         # Define the address the alerts should come from
         $monitor_email              = "";
         # Define global variables
         $curl_executable            = "/usr/bin/curl";
         $mail_executable            = "/usr/sbin/sendmail";
         $OPT_STRING                 = 'hs:e:';
         # Start program
     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");
                     &send_warning($email_address, $url);
             &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: $!";
             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

  2. #2
    Advisor Outlaw's Avatar
    Join Date
    May 2001
    Clifton Park, NY
    Nice work, checkout the custom plugin section of the Nagios PET. Handled similarly using curl.

  3. #3
    Great Script!!!
    This is exactly what I was looking for.

    I've tried it on several ubuntu machines and get the following errors:
    ./ line 3: use: command not found
    ./ line 7: =: command not found
    ./ line 10: =: command not found
    ./ line 11: =: command not found
    ./ line 12: =: command not found
    ./ line 15: syntax error near unexpected token `&'
    ./ line 15: `     &main();'
    What I did.
    ran vim
    pasted your code
    changed the monitor email
    ran chmod a+x
    ran -e shai at http: slash slash www dot csts dot com ( Ican't post links)

    Any ideas? Thank you!

  4. #4
    I am getting error.
    Can't find string terminator "EOF" anywhere before EOF at ./ line 84.

Similar Threads

  1. Shell script for monitoring a list of IP's
    By Hariharan in forum Linux - Software, Applications & Programming
    Replies: 0
    Last Post: 09-16-2010, 12:20 PM
  2. Shell script for monitoring
    By Hariharan in forum Linux - Software, Applications & Programming
    Replies: 0
    Last Post: 09-16-2010, 05:54 AM
  3. Simple Website Monitoring Script in Perl
    By peter in forum Tutorials
    Replies: 0
    Last Post: 11-24-2008, 04:59 AM
  4. Simple firewall script
    By modules in forum Security
    Replies: 2
    Last Post: 02-22-2006, 09:39 AM
  5. Can Anyone Help With A Simple Script??
    By Gary Penner in forum Programming
    Replies: 1
    Last Post: 02-01-2005, 07:03 PM


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts