Server Monitor
Here, you'll find a script to monitor a server.
The script is installed on one server to monitor another server.
Every so often, perhaps every 10 minutes, the script will request a document from the server it is monitoring. If there is no response from the server, something is wrong and an alert email is sent.
To prevent a local condition from affecting both servers at once, the server with the script and the server being monitored should be located in widely different geographical areas.
If you have only one server, make an agreement with another for reciprocal monitoring.
The Script
Copy the script source code and paste it into a plain text processor. Save it as ServerMonitor.cgi or other .cgi file name that makes sense to you.
#!/usr/bin/perl # Server Monitor with Alert Email # Version 1.0 # December 27, 2009 # Will Bontrager # https://www.willmaster.com # Copyright 2009 Bontrager Connection, LLC # Three places to customize: # Note: Single quotation marks (apostrophes) are used in the # customization area instead of double quotation marks. # Place 1 -- # Specify the URL of a web page on the server to monitor. my $MonitorURL = 'http://example.com/index.html'; # Place 2 -- # Specify the email address for the alert email. my $AlertEmailAddress = 'name@example.com'; # Place 3 -- # Specify the location of sendmail. my $sendmailLocation = '/usr/sbin/sendmail'; # # # # # # # # # # # # # # # # # # # # # # # No other customizations required. # # # # # # # # # # # # # # # # # # # # # # # use strict; use LWP::UserAgent; use HTTP::Request::Common; my $Content = ''; my $AlertEmailSubject = "[Server Monitor Alert]"; $AlertEmailAddress =~ s/^\s*(.*?)\s*$/$1/s; $MonitorURL =~ s/^\s*(.*?)\s*$/$1/s; $sendmailLocation =~ s/^\s*(.*?)\s*$/$1/s; $sendmailLocation .= ' -t' unless $sendmailLocation =~ / -t/; sub GetMonitoredPage { my ($url,$content) = @_; my ($success,$status,$code) = (); my $ua = LWP::UserAgent->new; my $r = $ua->request(GET $url); $success = $r->is_success if $r->is_success; $$content = $r->content if $r->content; $status = $r->status_line if $r->status_line; $code = $r->code if $r->code; return ($success,$status,$code); } # sub GetMonitoredPage sub MakeDateTimeString { my ($sec,$min,$hour,$mday,$mon,$yr) = @_; $mon = qw(Jan Feb Mar Apr May Jun Jul Augt Sept Oct Nov Dec)[$mon]; $sec = "0$sec" if $sec < 10; $min = "0$min" if $min < 10; $hour = "0$hour" if $hour < 10; $yr += 1900; return "$mday $mon $yr $hour:$min:$sec"; } # sub MakeDateTimeString my ($success,$status,$code) = GetMonitoredPage($MonitorURL,\$Content); goto BOTTOM if $success; goto BOTTOM unless $status =~ /timeout|refuse|unavailable|connect|gateway/i; my ($sec,$min,$hour,$mday,$mon,$yr,$wday,$yday,$isdst) = localtime; my $servertime = MakeDateTimeString($sec,$min,$hour,$mday,$mon,$yr); ($sec,$min,$hour,$mday,$mon,$yr,$wday,$yday,$isdst) = gmtime; my $gmtime = MakeDateTimeString($sec,$min,$hour,$mday,$mon,$yr); open MAIL,"|$sendmailLocation"; print MAIL <<END; From: $AlertEmailAddress To: $AlertEmailAddress Subject: $AlertEmailSubject $status Time: Monitoring server - $servertime GMT - $gmtime Server at: $MonitorURL Responded with status line: $status Explanation or more information, if available: $Content END BOTTOM: print "Content-type:text/plain\n\n" if $ENV{REQUEST_METHOD} =~ /GET/i; exit; #finis
Three places in the script require customization, both clearly marked.
-
The URL of the web page to request.
-
The email address for the alert email.
-
The location of sendmail.
Installation
When the edits have been completed, install the script.
-
Upload the script with FTP as plain text (not as binary) into a directory that can run Perl CGI scripts.
-
Give the uploaded script 755 permissions.
-
Set up cron to run the script ever 10 minutes or other desired frequency.
Hopefully, your hosting company provides a cron scheduling interface for you. If it does not, here are two documents that might help.
Testing
To test the installation, specify a URL in the script to a domain that does not exist - something like
Testing is complete when an alert email is received at the frequency of the cron schedule.
Change the URL in the script back to the URL for live monitoring.
Load the script into the browser to verify no Internal Server Error was introduced when the URL was changed.
The script is now monitoring the other server at the frequency specified with the cron schedule. An alert email is sent whenever the monitored server does not respond.
Will Bontrager