Monitoring Scripts

Revision 3 as of 2009-10-07 05:43:24

Clear message

Contents

Contents

  1. Nagios

If you have scripts to monitor the status of your mirror, please post them here.

Nagios

This check is suitable for Nagios installations. It will do a HEAD-request on the requested file on both your webserver and the main webserver and calculate the time-difference between the two last-modified headers.

Call this script as follows:

define command {
        command_name    check_ubuntu
        command_line    /etc/nagios-plugins/custom/check_ubuntu $HOSTNAME$ $ARG1$ $ARG2$
        }

And:

define service{
        use                             generic-service
        host_name                       <your mirrors hostname>
        service_description             Mirror status
        check_command                   check_ubuntu!<hostname of main mirror>!<file to check>!<mode, archive or cd>
}

Good files to check are:

  • ubuntu/ls-lR.gz for archive mirrors
  • .pool/MD5SUMS for releases mirrors

# Create a user agent object
use LWP::UserAgent;
use Net::DNS;
use Date::Parse;
use strict;
use lib "/usr/lib/nagios/plugins";
use utils qw($TIMEOUT %ERRORS &print_revision &support);

my ($host, $file, $mode) = @ARGV;

my $resolver = Net::DNS::Resolver->new;

my $dnsquery = $resolver->search($host);

if ($dnsquery) {
    foreach my $rr ($dnsquery->answer) {
        next unless $rr->type eq "A";
        if ($rr->address =~ /91\.189\.88/) {
                print "OK - Mirror ok (runs at Canonical)";
                exit $ERRORS{"OK"};
        }
    }
} else {
        print "CRITICAL - Cannot resolve hostname $host";
        exit $ERRORS{"CRITICAL"};
}

my $ua = LWP::UserAgent->new;
$ua->agent("UbuntuMirrorCheck/0.1 ");

my $canonicalhost = $host;
my $canonicalfile;
if ($canonicalhost =~ m/(archive|releases)\.ubuntu\.com/) {
        $canonicalhost =~ s/^..\.//;
        $canonicalfile = $file;
} else {
        if (defined($mode) && $mode =~ m/archive/) {
                $canonicalhost = 'archive.ubuntu.com';
                $canonicalfile = 'ubuntu/ls-lR.gz';
        } elsif (defined($mode) && $mode =~ m/cd/) {
                $canonicalhost = 'releases.ubuntu.com';
                $canonicalfile = '.pool/MD5SUMS';
        } else {
                print "CRITICAL - Cannot figure out the mode from hostname $host, and mode is not set";
                exit $ERRORS{"CRITICAL"};
        }
}


# Create a request
my $canonical = HTTP::Request->new(HEAD => 'http://'.$canonicalhost.'/'.$canonicalfile);
my $mirror    = HTTP::Request->new(HEAD => 'http://'.$host.'/'.$file);

# Pass request to the user agent and get a response back
my $canonical_res = $ua->request($canonical);
my $mirror_res    = $ua->request($mirror);

# Check the outcome of the response
if ($mirror_res->is_success) {
        my $canonical_time = str2time($canonical_res->header('last-modified'));
        my $mirror_time    = str2time($mirror_res->header('last-modified'));

        if (!defined($mirror_time)) {
                print "OK - Unable to determine mirror's last-modified timestamp";
                exit $ERRORS{"OK"};
        }

        if ($mirror_time > $canonical_time) {
                print "WARNING - Mirror seems to live in the future";
                exit $ERRORS{"WARNING"};
        } else {
                my $diff = $canonical_time - $mirror_time;
                my ($rest, $days, $hours, $minutes, $seconds) = (0, 0, 0, 0, 0);
                $days = int($diff/86400);
                $rest = $diff-($days*86400);
                $hours = int($rest/3600);
                $rest = $rest-($hours*3600);
                $minutes = int($rest/60);
                $rest = $rest-($minutes*60);
                $seconds = $rest;
                if ($diff > 64800) {
                        print "CRITICAL - Mirror is $days days, $hours hours, $minutes minutes and $seconds seconds behind";
                        exit $ERRORS{"CRITICAL"};
                } else {
                        print "OK - Mirror is up to date";
                        exit $ERRORS{"OK"};
                }
        }
} else {
    print "CRITICAL - Mirror is unavailable";
    exit $ERRORS{"CRITICAL"};
}