Perl Script for Monitoring a Server


Square Dot Square Dot Square Dot Little red trilobyte thingy Square Dot Square Dot Square Dot

This script, when called from a cron job, will alert you to a server that's not available. You'll need to install both HTTP::Lite and Mail::Sendmail for the script to run as-is.

#! /usr/bin/perl

use strict;
use HTTP::Lite;
use Mail::Sendmail;
use POSIX qw(strftime);

my $http = new HTTP::Lite;

# Customize this line for your web site:
my $url = qq(http://www.domain.com/smallpage.html);

my $startTime=time;

my $success=1;
my $message;

# Change it to whatever you want:
$http->add_req_header('User-Agent', 'Server Tattler');

my $req = $http->request($url) or $success=0;

if ($success)
{
	# Nothing, just go gently into that good night.
}
else
{
	my $elapsedTime = time - $startTime;
	
	my $sentTime = strftime("%A, %B %e %Y - %T", localtime($startTime));
	
	# You can dispense with Mail::sendmail and just use Perl's  built-in "print"
	# for these messages if you want. If you set up your crontab properly,
	# anything you "print" will get sent to you.
	
	$message =  qq(No server response after $elapsedTime second(s).\n);
	$message .= qq(Attempting to reach url: $url\n);
	$message .= qq(Began attempt at: $sentTime\n);
	
	my %mail = ( To => 'Your Name ',
				From => 'Server Tattler ',
				Subject => 'Possible Server Failure',
				Message => $message
				);
	sendmail(%mail) or print qq(Died at output to me $Mail::Sendmail::error\n);
}

Make the target page, here called smallpage.html, a really small page. On my server, it's a page that just sends back the IP address of the computer that doing the monitoring. It's just a single Apache include:

<--#echo var="REMOTE_ADDR"-->

Charlie Minow
February 9, 2020 - 5:09 PM MST

Dot Dot Dot Rose Thing Dot Dot Dot
Cool doo wah