2e8100f5e6b9fb2d29b4572d8bd5b327dc147aef
[mirror/dsa-nagios.git] / dsa-nagios-checks / checks / 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 #  Copyright (c) 2009, 2010 Peter Palfrader <peter@palfrader.org>
7 #
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 # General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 # USA
22
23 use LWP::UserAgent;
24 use Socket;
25 use strict;
26 use Date::Parse;
27 use Getopt::Long;
28 use Date::Parse;
29 use Date::Format;
30 use File::Basename;
31 use English;
32 use warnings;
33
34
35 sub usage($$) {
36         my ($fh, $exit) = @_;
37         my $basename = basename($PROGRAM_NAME);
38         my $VERSION = '0.1';
39
40         print $fh "$basename $VERSION\n";
41         print $fh "Usage: $basename [--help|--version] [--verbose]\n";
42         print $fh "\n";
43         print $fh "  --help              Print this short help.\n";
44         print $fh "  --version           Report version number.\n";
45         print $fh "  --verbose           Be a little verbose.\n";
46         print $fh "  --host              hostname to check.\n";
47         print $fh "  --path              path to tracefile.\n";
48         print $fh "  --allow-skew=<foo>:<bar> if the newest timestamp is newer than <foo>secs\n";
49         print $fh "                      then the mirror sync is still ok, assuming the oldest\n";
50         print $fh "                      trace file is no older than <bar>\n";
51         print $fh "\n";
52         exit ($exit);
53 };
54
55 # Work around LWP not being able to verify service certs directly
56 my $ca_dir = '/etc/ssl/ca-debian';
57 $ENV{'PERL_LWP_SSL_CA_PATH'} = $ca_dir if -d $ca_dir;
58
59 $ENV{'PATH'} = '/bin:/sbin:/usr/bin:/usr/sbin';
60 delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};
61
62 my $params;
63
64 $params->{'host'} = 'security.debian.org'; #which host to check
65 $params->{'path'} = 'project/trace/security-master.debian.org'; 
66
67 my $OK = 0;
68 my $WARNING = 1;
69 my $CRITICAL = 2;
70 my $UNKNOWN = 3;
71
72 if (!GetOptions (
73                 '--help'                => \$params->{'help'},
74                 '--verbose'             => \$params->{'verbose'},
75                 '--version'             => \$params->{'version'},
76                 '--host=s'              => \$params->{'host'},
77                 '--path=s'              => \$params->{'path'},
78                 '--allow-skew=s'        => \$params->{'skew'},
79                 )) {
80                 usage(*STDERR,1)
81 };
82 usage(*STDOUT,0) if ($params->{'help'});
83 usage(*STDERR,1) if (scalar @ARGV);
84
85 if (defined $params->{'skew'}) {
86         if (not $params->{'skew'} =~ /^([0-9]+):([0-9]+)$/) {
87                 print STDERR "Invalid allow-skew format\n";
88                 usage(*STDERR,1);
89         };
90         $params->{'skew-new'} = $1;
91         $params->{'skew-old'} = $2;
92 };
93
94 my $host = $params->{'host'};
95 my $path = $params->{'path'};
96 my @slaves;
97 my $status;
98 my @exitstatus;
99 my $exitcode = $OK;
100
101 @slaves = gethostbyname($params->{'host'})   or die "Can't resolve " . $params->{'host'} .": $!\n";
102 @slaves = map { inet_ntoa($_) } @slaves[4 .. $#slaves];
103 print "Checking the following hosts:\n" . join("\n", @slaves) . "\n" if $params->{'verbose'};
104
105 my @critical;
106
107 foreach my $slave (@slaves) {
108         my $ua = LWP::UserAgent->new;
109         $ua->proxy('http', "http://$slave");
110         print "Requesting http://$host/$path from $slave\n" if $params->{'verbose'};
111         my $response = $ua->get("http://$host/$path");
112
113
114         if ($response->is_success) {
115                 my $content = $response->content;  # or whatever
116                 my ($date, $foo, $bar) = split("\n", $content);
117                 my $synctime = str2time($date);;
118                 if (! defined $synctime) {
119                         $synctime = 0;
120                         $exitcode = $UNKNOWN;
121                         push @exitstatus, "Cannot parse tracefile on $slave";
122                 };
123                 print "$slave last synced $synctime\n" if $params->{'verbose'};
124                 $status->{$slave}->{'synced'} = $synctime;
125         }
126         else {
127                 push @exitstatus, "$slave broken: " . $response->status_line; 
128                 $status->{$slave}->{'error'} = $response->status_line;
129                 $status->{$slave}->{'synced'} = 0;
130                 $exitcode = $CRITICAL;
131                 push @critical, $slave;
132         }
133 }
134
135
136 my %seen;
137 my $o_sync = scalar(grep !$seen{$_}++, map{$status->{$_}->{'synced'}} keys(%{$status}));
138 if ($o_sync > 1) {
139         my @mirrors =  sort { $status->{$a}->{'synced'} <=> $status->{$b}->{'synced'}  } keys %{$status};
140         my @not_most_recent = grep { $status->{$_}->{'synced'} <=> $status->{$mirrors[-1]}->{'synced'} } @mirrors;
141         $o_sync = scalar @not_most_recent;
142
143         my $newest = time - $status->{$mirrors[-1]}->{'synced'};
144         my $oldest = time - $status->{$mirrors[0]}->{'synced'};
145         my $skew_ok = ( defined $params->{'skew-new'} &&
146                         defined $params->{'skew-old'} &&
147                         $newest <= $params->{'skew-new'} &&
148                         $oldest <= $params->{'skew-old'});
149
150         my $msg;
151         if ($skew_ok) {
152                 $exitcode = $OK;
153                 $msg = "$o_sync mirror(s) not in sync (from oldest to newest), but still within acceptable skew: ";
154         } else {
155                 $exitcode = $CRITICAL;
156                 $msg = "$o_sync mirror(s) not in sync (from oldest to newest): ";
157         };
158         push @exitstatus, $msg . join(", ", @not_most_recent);
159 } else {
160         print "All mirrors unique\n" if $params->{'verbose'};
161 }
162
163 if ($exitcode == $CRITICAL) {
164         print "CRITICAL: " . join(',',@exitstatus) . "\n";
165 } elsif ($exitcode == $OK) {
166         if (scalar @exitstatus > 0) {
167                 print "OK: " . join(',',@exitstatus) . "\n";
168         } else {
169                 print "OK: all mirrors up2date\n";
170         }
171 } else {
172         print join(',',@exitstatus) . "\n";
173 }
174
175 foreach my $mirror (keys(%{$status})) {
176         if ($status->{$mirror}->{'error'}) {
177                 print "$mirror broken: " . $status->{$mirror}->{'error'} . "\n";
178         } else {
179                 print "$mirror last synced: " . localtime($status->{$mirror}->{'synced'}) ."\n";
180         }
181 }
182
183 exit $exitcode;