3 # nagios check for debian security sync checks
5 # Copyright (c) 2008 Alexander Wirt <formorer@debian.org>
6 # Copyright (c) 2009, 2010 Peter Palfrader <peter@palfrader.org>
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.
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.
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
37 my $basename = basename($PROGRAM_NAME);
40 print $fh "$basename $VERSION\n";
41 print $fh "Usage: $basename [--help|--version] [--verbose]\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";
55 # Work around LWP not being able to verify service certs directly
56 $ENV{'HTTPS_CA_DIR'} = '/etc/ssl/ca-debian';
58 $ENV{'PATH'} = '/bin:/sbin:/usr/bin:/usr/sbin';
59 delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};
63 $params->{'host'} = 'security.debian.org'; #which host to check
64 $params->{'path'} = 'project/trace/security-master.debian.org';
72 '--help' => \$params->{'help'},
73 '--verbose' => \$params->{'verbose'},
74 '--version' => \$params->{'version'},
75 '--host=s' => \$params->{'host'},
76 '--path=s' => \$params->{'path'},
77 '--allow-skew=s' => \$params->{'skew'},
81 usage(*STDOUT,0) if ($params->{'help'});
82 usage(*STDERR,1) if (scalar @ARGV);
84 if (defined $params->{'skew'}) {
85 if (not $params->{'skew'} =~ /^([0-9]+):([0-9]+)$/) {
86 print STDERR "Invalid allow-skew format\n";
89 $params->{'skew-new'} = $1;
90 $params->{'skew-old'} = $2;
93 my $host = $params->{'host'};
94 my $path = $params->{'path'};
100 @slaves = gethostbyname($params->{'host'}) or die "Can't resolve " . $params->{'host'} .": $!\n";
101 @slaves = map { inet_ntoa($_) } @slaves[4 .. $#slaves];
102 print "Checking the following hosts:\n" . join("\n", @slaves) . "\n" if $params->{'verbose'};
106 foreach my $slave (@slaves) {
107 my $ua = LWP::UserAgent->new;
108 $ua->proxy('http', "http://$slave");
109 print "Requesting http://$host/$path from $slave\n" if $params->{'verbose'};
110 my $response = $ua->get("http://$host/$path");
113 if ($response->is_success) {
114 my $content = $response->content; # or whatever
115 my ($date, $foo, $bar) = split("\n", $content);
116 my $synctime = str2time($date);;
117 if (! defined $synctime) {
119 $exitcode = $UNKNOWN;
120 push @exitstatus, "Cannot parse tracefile on $slave";
122 print "$slave last synced $synctime\n" if $params->{'verbose'};
123 $status->{$slave}->{'synced'} = $synctime;
126 push @exitstatus, "$slave broken: " . $response->status_line;
127 $status->{$slave}->{'error'} = $response->status_line;
128 $status->{$slave}->{'synced'} = 0;
129 $exitcode = $CRITICAL;
130 push @critical, $slave;
136 my $o_sync = scalar(grep !$seen{$_}++, map{$status->{$_}->{'synced'}} keys(%{$status}));
138 my @mirrors = sort { $status->{$a}->{'synced'} <=> $status->{$b}->{'synced'} } keys %{$status};
139 my @not_most_recent = grep { $status->{$_}->{'synced'} <=> $status->{$mirrors[-1]}->{'synced'} } @mirrors;
140 $o_sync = scalar @not_most_recent;
142 my $newest = time - $status->{$mirrors[-1]}->{'synced'};
143 my $oldest = time - $status->{$mirrors[0]}->{'synced'};
144 my $skew_ok = ( defined $params->{'skew-new'} &&
145 defined $params->{'skew-old'} &&
146 $newest <= $params->{'skew-new'} &&
147 $oldest <= $params->{'skew-old'});
152 $msg = "$o_sync mirror(s) not in sync (from oldest to newest), but still within acceptable skew: ";
154 $exitcode = $CRITICAL;
155 $msg = "$o_sync mirror(s) not in sync (from oldest to newest): ";
157 push @exitstatus, $msg . join(", ", @not_most_recent);
159 print "All mirrors unique\n" if $params->{'verbose'};
162 if ($exitcode == $CRITICAL) {
163 print "CRITICAL: " . join(',',@exitstatus) . "\n";
164 } elsif ($exitcode == $OK) {
165 if (scalar @exitstatus > 0) {
166 print "OK: " . join(',',@exitstatus) . "\n";
168 print "OK: all mirrors up2date\n";
171 print join(',',@exitstatus) . "\n";
174 foreach my $mirror (keys(%{$status})) {
175 if ($status->{$mirror}->{'error'}) {
176 print "$mirror broken: " . $status->{$mirror}->{'error'} . "\n";
178 print "$mirror last synced: " . localtime($status->{$mirror}->{'synced'}) ."\n";