raise timeout
[mirror/dsa-nagios.git] / dsa-nagios-checks / checks / dsa-check-zone-rrsig-expiration-many
1 #!/usr/bin/perl
2
3 # Copyright (c) 2010,2012 Peter Palfrader <peter@palfrader.org>
4 #
5 # Permission is hereby granted, free of charge, to any person obtaining
6 # a copy of this software and associated documentation files (the
7 # "Software"), to deal in the Software without restriction, including
8 # without limitation the rights to use, copy, modify, merge, publish,
9 # distribute, sublicense, and/or sell copies of the Software, and to
10 # permit persons to whom the Software is furnished to do so, subject to
11 # the following conditions:
12 #
13 # The above copyright notice and this permission notice shall be
14 # included in all copies or substantial portions of the Software.
15 #
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
24 use strict;
25 use warnings;
26 use threads;
27
28 use English;
29 use Getopt::Long;
30 use FindBin qw($Bin);
31 use YAML;
32 use File::Basename;
33
34 my $CHECK = $Bin.'/dsa-check-zone-rrsig-expiration';
35
36 $SIG{__DIE__} = sub {
37         print @_;
38         exit 3;
39 };
40
41 sub convert_time {
42         my $ticks = shift;
43         my $unit = shift;
44
45         unless (defined $unit) {
46                 my $newticks;
47                 ($newticks, $unit) = $ticks =~ m/^(\d*)([smhdw]?)$/;
48                 if (!defined $newticks) {
49                         print STDERR "Warning: invalid timestring to convert '$ticks'\n";
50                         return $ticks;
51                 }
52                 $ticks = $newticks;
53         }
54
55         if ($unit eq 's' || $unit eq '') { }
56         elsif ($unit eq 'm') { $ticks *= 60; }
57         elsif ($unit eq 'h') { $ticks *= 60*60; }
58         elsif ($unit eq 'd') { $ticks *= 60*60*24; }
59         elsif ($unit eq 'w') { $ticks *= 60*60*24*7; }
60         else { print STDERR "Warning: invalid unit '$unit'\n" }
61         return $ticks;
62 }
63
64 sub check_one {
65         $SIG{'KILL'} = sub { threads->exit(); };
66
67         my $zone = shift;
68         my $check = shift;
69         my $params = shift;
70
71         open(P, '-|', ($check, '-w', $params->{'warn'}, '-c', $params->{'critical'}, $zone)) or die ("Cannot run $CHECK for $zone\n");
72         my @p = <P>;
73         close P;
74         $p[0] = $zone.': '. $p[0] if (scalar @p > 0);
75
76         my $res = $CHILD_ERROR >> 8;
77
78         return ($res, \@p);
79 }
80
81 my $USAGE = "Usage: $PROGRAM_NAME [--help] | [--timeout=<nn>] [--warn=<nn>] [--critical=<nn>] [--geozonedir=<geodir>] <indir>\n";
82 my $params = { 'timeout' => 45, 'warn' => '14d', 'critical' => '7d' };
83 Getopt::Long::config('bundling');
84 GetOptions (
85         '--help' => \$params->{'help'},
86         '--timeout=i' => \$params->{'timeout'},
87         '--warn=s' => \$params->{'warn'},
88         '--critical=s' => \$params->{'critical'},
89         '--geozonedir=s' => \$params->{'geozonedir'},
90 ) or die ($USAGE);
91 if ($params->{'help'}) {
92         print $USAGE;
93         exit(0);
94 };
95 die ($USAGE) unless (scalar @ARGV == 1);
96 my $INDIR = shift;
97
98
99 my $count =
100         { 'ok' => [],
101           'warn' => [],
102           'critical' => [],
103           'unknown' => [],
104           'unsigned' => [],
105         };
106
107
108 my @dnsseczones;
109 # load list of classic zones that will do DNSSEC
110 chdir $INDIR or die "chdir $INDIR failed? $!\n";
111 opendir INDIR, '.' or die ("Cannot opendir $INDIR\n");
112 for my $file (sort {$a cmp $b} (readdir INDIR)) {
113         next if ( -l "$file" );
114         next unless ( -f "$file" );
115         next if $file =~ /^(dsset|keyset)-/;
116
117         my $do_dnssec = 0;
118         open(F, '<', $file) or die ("Cannot open $file: $!\n");
119         for (<F>) {
120                 if (/^; wzf:\s*dnssec\s*=\s*1\s*$/) { $do_dnssec = 1; last; }
121         };
122         close F;
123
124         if ($do_dnssec) {
125                 push @dnsseczones, $file;
126         } else {
127                 push @{$count->{'unsigned'}}, $file;
128         };
129 }
130 closedir(INDIR);
131
132 # load list of geodns zones that will do DNSSEC
133 if (defined $params->{'geozonedir'}) {
134         chdir $params->{'geozonedir'} or die "chdir $params->{'geozonedir'} failed? $!\n";
135         opendir INDIR, '.' or die ("Cannot opendir $params->{'geozonedir'}\n");
136         for my $file (sort {$a cmp $b} (readdir INDIR)) {
137                 next unless $file =~ /\.zone$/;
138
139                 open (F, '<', $file) or die "Cannot open $file: $!\n";
140                 my ($zc, undef, undef) = Load(join "", (<F>));
141                 close F;
142
143                 my $zone = basename($file, '.zone');
144
145                 if ($zc->{'dnssec'}) {
146                         push @dnsseczones, $zone;
147                 } else {
148                         push @{$count->{'unsigned'}}, $zone;
149                 };
150         }
151         closedir(INDIR);
152 }
153
154 my @details;
155
156 my %threads;
157 for my $zone (sort {$a cmp $b} @dnsseczones) {
158         die "Duplicate zone $zone?\n"  if defined $threads{$zone};
159         my $thr = threads->create({'context' => 'list'},
160                                   \&check_one, $zone, $CHECK, $params);
161         $threads{$zone} = $thr;
162 }
163
164 my $begin = time;
165 while (time - $begin <= $params->{timeout}) {
166         for my $zone (sort {$a cmp $b} keys %threads) {
167                 next unless $threads{$zone}->is_joinable();
168
169                 my ($res, $det) = $threads{$zone}->join();
170
171                 push @details, @$det;
172
173                 if ($res == 0) { push @{$count->{'ok'}}, $zone; }
174                 elsif ($res == 1) { push @{$count->{'warn'}}, $zone; }
175                 elsif ($res == 2) { push @{$count->{'critical'}}, $zone; }
176                 else { push @{$count->{'unknown'}}, $zone; };
177                 delete $threads{$zone};
178         }
179         sleep(1) unless scalar keys %threads == 0;
180 }
181 for my $zone (sort {$a cmp $b} keys %threads) {
182         push @{$count->{'warn'}}, $zone;
183         push @details, "$zone: timeout during check\n";
184         $threads{$zone}->kill('KILL')->detach();
185 }
186
187 for my $k (keys %$count) {
188         @{$count->{$k}} = sort {$a cmp $b} @{$count->{$k}};
189 }
190
191 my $exit;
192 my %state_mapping = (
193         'unknown' => 255,
194         'critical' => 2,
195         'warn' => 1,
196         'ok' => 0 );
197
198 for my $state (sort {$state_mapping{$b} <=> $state_mapping{$a}} keys %state_mapping) {
199         if (scalar @{$count->{$state}}) {
200                 printf "%s: %d", uc($state), scalar @{$count->{$state}};
201                 if ($state_mapping{$state} > 0) {
202                         print ": ", join(', ', @{$count->{$state}});
203                 };
204                 print "; ";
205                 $exit = $state_mapping{$state} unless defined $exit;
206         };
207 };
208 printf "unsigned: %d", scalar @{$count->{'unsigned'}};
209 print "\n";
210 print $_ for (@details);
211 exit $exit;