No longer install nrpe_dsa.cfg
[mirror/dsa-nagios.git] / dsa-nagios-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 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 "\n";
49         exit ($exit);
50 };
51
52
53 $ENV{'PATH'} = '/bin:/sbin:/usr/bin:/usr/sbin';
54 delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};
55
56 my $params;
57
58 $params->{'host'} = 'security.debian.org'; #which host to check
59 $params->{'path'} = 'project/trace/security-master.debian.org'; 
60
61 my $OK = 0;
62 my $WARNING = 1;
63 my $CRITICAL = 2;
64 my $UNKNOWN = 3;
65
66 if (!GetOptions (
67                 '--help'                => \$params->{'help'},
68                 '--verbose'             => \$params->{'verbose'},
69                 '--version'             => \$params->{'version'},
70                 '--host=s'              => \$params->{'host'},
71                 '--path=s'              => \$params->{'path'},
72                 )) {
73                 usage(*STDERR,1)
74 };
75 usage(*STDOUT,0) if ($params->{'help'});
76 usage(*STDERR,1) if (scalar @ARGV);
77
78 my $host = $params->{'host'};
79 my $path = $params->{'path'};
80 my @slaves;
81 my $status;
82 my @exitstatus;
83 my $exitcode = $OK;
84
85 @slaves = gethostbyname($params->{'host'})   or die "Can't resolve " . $params->{'host'} .": $!\n";
86 @slaves = map { inet_ntoa($_) } @slaves[4 .. $#slaves];
87 print "Checking the following hosts:\n" . join("\n", @slaves) . "\n" if $params->{'verbose'};
88
89 my @critical;
90
91 foreach my $slave (@slaves) {
92         my $ua = LWP::UserAgent->new;
93         $ua->proxy('http', "http://$slave");
94         print "Requesting http://$host/$path from $slave\n" if $params->{'verbose'};
95         my $response = $ua->get("http://$host/$path");
96
97
98         if ($response->is_success) {
99                 my $content = $response->content;  # or whatever
100                 my ($date, $foo, $bar) = split("\n", $content);
101                 my $synctime = str2time($date);;
102                 print "$slave last synced $synctime\n" if $params->{'verbose'};
103                 $status->{$slave}->{'synced'} = $synctime; 
104         }
105         else {
106                 push @exitstatus, "$slave broken: " . $response->status_line; 
107                 $status->{$slave}->{'error'} = $response->status_line;
108                 $exitcode = $CRITICAL;
109                 push @critical, $slave;
110         }
111 }
112
113
114 my %seen;
115 my $o_sync = scalar(grep !$seen{$_}++, map{$status->{$_}->{'synced'}} keys(%{$status}));
116 if ($o_sync > 1) {
117         $exitcode = $CRITICAL; 
118         $o_sync -= 1;
119         my @mirrors =  sort { $status->{$a}->{'synced'} <=> $status->{$b}->{'synced'}  } keys %{$status};
120         push @exitstatus, "$o_sync mirror(s) not in sync (from oldest to newest): ". 
121                 join(",", splice(@mirrors,0,$o_sync));  
122 } else {
123         print "All mirrors unique\n" if $params->{'verbose'};
124 }
125
126 if ($exitcode == $CRITICAL) {
127         print "CRITICAL: " . join(',',@exitstatus) . "\n";              
128 } elsif ($exitcode == $OK) {
129         print "OK: all mirrors up2date\n";
130 }
131
132 foreach my $mirror (keys(%{$status})) {
133         if ($status->{$mirror}->{'error'}) {
134                 print "$mirror broken: " . $status->{$mirror}->{'error'} . "\n";
135         } else {
136                 print "$mirror last synced: " . localtime($status->{$mirror}->{'synced'}) ."\n";
137         }
138 }
139
140 exit $exitcode;