Um, in general it is not possible for a failed system to report that
it has failed, because it has already failed. :-/

What I do is to run a periodic check on a different host.  My script
is far from complete, but it often catches problems that I need to
know about.  I have cron run it every five minutes.  By running the
check on a different host, I avoid the chicken/egg problem of trying
to get a failing machine to monitor itself.

I've attached a copy of my script.  You'll need to configure it for
your services and your notification requirements, and I have no doubt
that it can be improved.

-- 
Mark H. Wood, Lead System Programmer   [EMAIL PROTECTED]
Typically when a software vendor says that a product is "intuitive" he
means the exact opposite.

#! /usr/bin/perl -w
# ping-servers -- check web servers for life and wellness
# Copyright 2005 Indiana University - Purdue University Indianapolis
# Mark H. Wood, IUPUI University Library, 26-Sep-2005

use BerkeleyDB;
use LWP::UserAgent;
use Mail::Sendmail;
use Date::Format;

# === Configure here ===

my @urls = (
	    'https://idea.iupui.edu',
	    'https://archives.iupui.edu',
	    'https://policyarchive.iupui.edu',
	    'https://folio.iupui.edu',
	    'https://ithink.ulib.iupui.edu',
	    'https://johncock.ulib.iupui.edu',
	    'http://journals.iupui.edu',
	    #'http://mhw.ulib.iupui.edu/goof',
	    'http://mhw.ulib.iupui.edu/'
	    );
my $fromAddress = '[EMAIL PROTECTED]';
my $emailAddress = '[EMAIL PROTECTED]';
my $pageAddress = '[EMAIL PROTECTED]';

# === End configuration data ===

my $body;
my $failures = 0;
my $debug = 0;

# Prepare a Web context

my $ua = LWP::UserAgent->new;
$ua->timeout(30);

# Open up the database

tie(%state, 'BerkeleyDB::Btree',
    -Filename => 'ping-servers.db',
    -Flags => DB_CREATE,
    -Mode => 0600)
    || die 'Failed to open database';

open LOG,'>>ping-servers.log' if $debug;

# Scan monitored servers

foreach $url (@urls)
{
    my $code = 0;
    my $failure = 0;
    my $now = time;
    my($then, $when, $oldcode, $oldstate);

    # Fetch server's previous state if known
    if ($oldstate = $state{$url})
    {
	($oldcode, $then, $when) = split(/:/,$oldstate);
    } else # initialize a new server
    {
	$oldcode = $code;
	$then = $now;
	$when = $now;
    };

    next if ($when > $now);

    # Poll the server
    my $response = $ua->head($url);

    if (!$response)
    {
	$failure = 1;
	$message = "Could not fetch $url\n";
	$when = $now + (15 * 60);
    }
    else
    {
	$code = $response->code;
	$message = "HEAD $url returned $code\n";
	if (!$response->is_success)
	{
	    $failure = 1;
	    $when = $now + (15 * 60);
	}
    }

    # Notify if status has changed
    print LOG time2str('%Y/%m/%d %X',$now),' ',$message if $debug;
    if ($code != $oldcode)
    {
	# send mail
	$body .= $message;
	if ($failure)
	{
	    # send page
	    my %mail = ( To => $pageAddress,
			 From => $fromAddress,
			 Message => $message );
	    print "paging\n" if $debug;
	    $failures++;
	}
    }

    # Record latest status
    $state{$url} = "$code:$now:$when";
}

# Send mail if there is anything new to say

if ($body)
{
    my $subject;
    if ($failures)
	{ $subject = 'ping-servers:  failure'; }
    else
	{ $subject = 'ping-servers:  success'; }

    my %mail = ( To => $emailAddress,
		 From => $fromAddress,
		 Message => $body,
		 Subject => $subject );
    sendmail(%mail) or warn $Mail::Sendmail::error;
}

# Clean up and exit

untie %state;
close LOG if $debug;

Attachment: pgpQFdlvbdPWT.pgp
Description: PGP signature

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
DSpace-tech mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/dspace-tech

Reply via email to