Warning: Function ereg() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 4

Warning: Function split() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 19

Warning: Function ereg() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 4

Warning: Function split() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 19

Warning: Function ereg() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 4

Warning: Function split() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 19
Simple Website Monitoring Script in Perl
Results 1 to 3 of 3

Thread: Simple Website Monitoring Script in Perl

Hybrid View

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

    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:

    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 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!

    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;
     
     }

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

    To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.


    To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.


    To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

  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:
    Code:
    ./checksite.pl: line 3: use: command not found
    ./checksite.pl: line 7: =: command not found
    ./checksite.pl: line 10: =: command not found
    ./checksite.pl: line 11: =: command not found
    ./checksite.pl: line 12: =: command not found
    ./checksite.pl: line 15: syntax error near unexpected token `&'
    ./checksite.pl: line 15: `     &main();'
    What I did.
    ran vim checksite.pl
    pasted your code
    changed the monitor email
    ran chmod a+x checksite.pl
    ran checksite.pl -e shai at csts.com http: slash slash www dot csts dot com ( Ican't post links)

    Any ideas? Thank you!

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

Bookmarks

Posting Permissions

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