Software, your way.
How To Get Good Custom Software
(Download)
(PDF)
burger menu icon
WillMaster

WillMaster > LibrarySnooping (Information Retrieval)

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

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.

  1. The URL of the web page to request.

  2. The email address for the alert email.

  3. The location of sendmail.

Installation

When the edits have been completed, install the script.

  1. Upload the script with FTP as plain text (not as binary) into a directory that can run Perl CGI scripts.

  2. Give the uploaded script 755 permissions.

  3. 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 http://jkie9se333eddfs.com/index.html. Verify it does not exist by trying it in your browser.

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

Was this article helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method

All information in WillMaster Library articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC