[project @ peter@palfrader.org-20081223220130-dfmsrpvbhkdgwt3t]
[mirror/dsa-nagios.git] / dsa-nagios-nrpe-config / dsa-check-mirrorsync
1 #!/usr/bin/perl -w
2
3 # nagios check for debian security sync checks
4 #
5 #  Copyright (c) 2008 Alexander Wirt <formorer@debian.org>
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 # USA
21
22 use LWP::UserAgent;
23 use Socket;
24 use strict;
25 use Date::Parse;
26 use Getopt::Long;
27 use Date::Parse;
28 use Date::Format;
29 use warnings;
30
31
32 $ENV{'PATH'} = '/bin:/sbin:/usr/bin:/usr/sbin';
33 delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};
34
35 my $VERSION = '0.0';
36 my $PROGRAM_NAME = 'check_securitymirror';
37 my $params;
38
39 $params->{'host'} = 'security.debian.org'; #which host to check
40 $params->{'path'} = 'project/trace/security-master.debian.org'; 
41
42 my $OK = 0;
43 my $WARNING = 1;
44 my $CRITICAL = 2;
45 my $UNKNOWN = 3;
46
47 if (!GetOptions (
48                 '--help'                => \$params->{'help'},
49                 '--verbose'             => \$params->{'verbose'},
50                 '--version'             => \$params->{'version'},
51                 '--host=s'              => \$params->{'host'},
52                 '--path=s'              => \$params->{'path'},
53                 )) {
54                 die ("Usage: $PROGRAM_NAME [--help|--version] [--verbose]\n");
55 };
56
57 if ($params->{'help'}) {
58          print "$PROGRAM_NAME $VERSION\n";
59          print "Usage: $PROGRAM_NAME [--help|--version] [--verbose]\n";
60          print "\n";
61          print "  --help              Print this short help.\n";
62          print "  --version           Report version number.\n";
63          print "  --verbose           Be a little verbose.\n";
64          print "  --host              hostname to check.\n";
65          print "  --path              path to tracefile.\n";
66          print "\n";
67          exit (0);
68 };
69
70 my $host = $params->{'host'};
71 my $path = $params->{'path'};
72 my @slaves;
73 my $status;
74 my @exitstatus;
75 my $exitcode = $OK;
76
77 @slaves = gethostbyname($params->{'host'})   or die "Can't resolve " . $params->{'host'} .": $!\n";
78 @slaves = map { inet_ntoa($_) } @slaves[4 .. $#slaves];
79 print "Checking the following hosts:\n" . join("\n", @slaves) . "\n" if $params->{'verbose'};
80
81 my @critical;
82
83 foreach my $slave (@slaves) {
84         my $ua = LWP::UserAgent->new;
85         $ua->proxy('http', "http://$slave");
86         print "Requesting http://$host/$path from $slave\n" if $params->{'verbose'};
87         my $response = $ua->get("http://$host/$path");
88
89
90         if ($response->is_success) {
91                 my $content = $response->content;  # or whatever
92                 my ($date, $foo, $bar) = split("\n", $content);
93                 my $synctime = str2time($date);;
94                 print "$slave last synced $synctime\n" if $params->{'verbose'};
95                 $status->{$slave}->{'synced'} = $synctime; 
96         }
97         else {
98                 push @exitstatus, "$slave broken: " . $response->status_line; 
99                 $status->{$slave}->{'error'} = $response->status_line;
100                 $exitcode = $CRITICAL;
101                 push @critical, $slave;
102         }
103 }
104
105
106 my %seen;
107 my $o_sync = scalar(grep !$seen{$_}++, map{$status->{$_}->{'synced'}} keys(%{$status}));
108 if ($o_sync > 1) {
109         $exitcode = $CRITICAL; 
110         $o_sync -= 1;
111         my @mirrors =  sort { $status->{$a}->{'synced'} <=> $status->{$b}->{'synced'}  } keys %{$status};
112         push @exitstatus, "$o_sync mirror(s) not in sync (from oldest to newest): ". 
113                 join(",", splice(@mirrors,0,$o_sync));  
114 } else {
115         print "All mirrors unique\n" if $params->{'verbose'};
116 }
117
118 if ($exitcode == $CRITICAL) {
119         print "CRITICAL: " . join(',',@exitstatus) . "\n";              
120 } elsif ($exitcode == $OK) {
121         print "OK: all mirrors up2date\n";
122 }
123
124 foreach my $mirror (keys(%{$status})) {
125         if ($status->{$mirror}->{'error'}) {
126                 print "$mirror broken: " . $status->{$mirror}->{'error'} . "\n";
127         } else {
128                 print "$mirror last synced: " . localtime($status->{$mirror}->{'synced'}) ."\n";
129         }
130 }
131
132 exit $exitcode;