#!/usr/bin/perl ######################################################################### # monitor.cgi # # This program must be run from cron on a Unix server. If you don't # know what it is, ask # your sysadmin. It will periodically check the # sites listed in @sites, below, to see if they are up and running. If # they are down, an email will be sent to your email address, advising # you of the downtime. # # Written by Greg Raaum # Copyright (c) 2000 BizDesign, Inc. All Rights Reserved. # Selling the code for this program without prior written consent is # expressly forbidden. # # This program is NOT supported. Use at your own risk! ######################################################################### # Instructions: # # Rename this file from monitor.txt to monitor.cgi or monitor.pl, # then upload this file to your server and add it to cron, normally in # /etc/cron.hourly or /etc/cron.quarter-hourly. You must set permissions # on this file to 755 after adding this to a cron directory. # # Define the four variables below. Place this file in a cron directory or # make a cron entry for this file and you are ready to go! # ######################################################################### ########################################################################## # Configuration Section ######################################################################### # (1) ################################################################### # WHAT IS YOUR EMAIL ADDRESS? $admin_email = 'yourname@yoursite.com'; # (2) ################################################################### # WHAT IS THE PATH TO SENDMAIL ON YOUR SERVER? $sendmail = '/usr/sbin/sendmail'; # (3) ################################################################### # WHAT SITES DO YOU WANT TO TRACK? (leave a comma off of the last site!) @sites = ( 'www.yoursite1.com', 'www.yoursite2.com', 'www.yoursite3.com', 'www.yoursite4.com' ); # (4) ################################################################### # WHAT TIME ZONE ARE YOU IN? $zone = "CST"; # No further editing is necessary, but feel free to play around... ######################################################################### use IO::Socket; # use Socket module &check_sites; exit(0); sub check_sites { # -------------------------------------------------------- # Attempts to access, 1 by 1, each site listed in @sites foreach $site (@sites) { $socket = new IO::Socket::INET( PeerAddr => $site, PeerPort => 80, Proto => 'tcp', Type => SOCK_STREAM, ) or &send_email($site); print $socket "GET / HTTP/1.0\n"; print $socket "User-Agent: SiteMonitor\n\n"; close ($socket); } } sub send_email { # -------------------------------------------------------- # Sends mail to the administrator if a site is down my ($server) = @_; &get_date; open(MAIL, "|$sendmail -t"); print MAIL "To: $admin_email\n"; print MAIL "From: $admin_email\n"; print MAIL "Subject: Server Down!\n\n"; print MAIL "$date\n\n"; print MAIL "The site, $server, is down. Please check it immediately!\n"; close (MAIL); } sub get_date { # -------------------------------------------------------- # Gets the date in case we need to send email @junk = localtime(time); $date = (Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec)[$junk[4]]; $date .= "-" . $junk[3] . "-" . ($junk[5] + 1900); $date .= " " . $junk[2] . ":" . $junk[1] . " $zone"; }