Monitoring Scripts

Differences between revisions 1 and 3 (spanning 2 versions)
Revision 1 as of 2009-09-21 10:27:46
Size: 3128
Editor: fw1
Comment:
Revision 3 as of 2009-10-07 05:43:24
Size: 3909
Editor: gw
Comment:
Deletions are marked like this. Additions are marked like this.
Line 14: Line 14:
        command_line /etc/nagios-plugins/custom/check_ubuntu $HOSTNAME$ $ARG1$         command_line /etc/nagios-plugins/custom/check_ubuntu $HOSTNAME$ $ARG1$ $ARG2$
Line 22: Line 22:
        host_name .*\.archive\.ubuntu\.com         host_name <your mirrors hostname>
Line 24: Line 24:
        check_command check_ubuntu!<hostname of main mirror>!<file to check>         check_command check_ubuntu!<hostname of main mirror>!<file to check>!<mode, archive or cd>
Line 43: Line 43:
my ($yourhost, $mainhost, $file) = @ARGV; 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"};
}
Line 48: Line 65:
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"};
 }
}

Line 49: Line 85:
my $canonical = HTTP::Request->new(HEAD => 'http://'.$mainhost.'/'.$file);
my $mirror = HTTP::Request->new(HEAD => 'http://'.$yourhost.'/'.$file);
my $canonical = HTTP::Request->new(HEAD => 'http://'.$canonicalhost.'/'.$canonicalfile);
my $mirror = HTTP::Request->new(HEAD => 'http://'.$host.'/'.$file);
Line 58: Line 94:
        my $canonical_time = str2time($canonical_res->header('last-modified'));  my $canonical_time = str2time($canonical_res->header('last-modified'));
Line 61: Line 97:
        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 > 43200) {
                        print "CRITICAL - Mirror is $days days, $hours hours, $minutes minutes and $seconds seconds behind";
                        exit $ERRORS{"CRITICAL"};
                } else {
                        print "OK - Mirror is $days days, $hours hours, $minutes minutes and $seconds seconds behind";
                        
exit $ERRORS{"OK"};
                }
        }
 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"};
  }
 }

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"};
}

Mirrors/Monitoring Scripts (last edited 2022-07-25 23:59:19 by tcuthbert)