remove | in detail output
[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 $extra = shift;
70         my $params = shift;
71
72         my @cmd = ($check, '-w', $params->{'warn'}, '-c', $params->{'critical'});
73         push(@cmd, '-r', $extra->{'initial_refs'}) if exists $extra->{'initial_refs'};
74         push(@cmd, '-d') if $params->{'debug'};
75         push(@cmd, $zone);
76         open(P, '-|', @cmd) or die ("Cannot run $CHECK for $zone\n");
77         my @p = <P>;
78         close P;
79         $p[0] = $zone.': '. $p[0] if (scalar @p > 0);
80
81         my $res = $CHILD_ERROR >> 8;
82
83         return ($res, \@p);
84 }
85
86 my $USAGE = "Usage: $PROGRAM_NAME [--help] | [--debug] [--timeout=<nn>] [--warn=<nn>] [--critical=<nn>] [--geozonedir=<geodir>] <indir>\n";
87 my $params = { 'timeout' => 45, 'warn' => '14d', 'critical' => '7d' };
88 Getopt::Long::config('bundling');
89 GetOptions (
90         '--help' => \$params->{'help'},
91         '--timeout=i' => \$params->{'timeout'},
92         '--warn=s' => \$params->{'warn'},
93         '--debug' => \$params->{'debug'},
94         '--critical=s' => \$params->{'critical'},
95         '--geozonedir=s' => \$params->{'geozonedir'},
96 ) or die ($USAGE);
97 if ($params->{'help'}) {
98         print $USAGE;
99         exit(0);
100 };
101 die ($USAGE) unless (scalar @ARGV == 1);
102 my $INDIR = shift;
103
104
105 my $count =
106         { 'ok' => [],
107           'warn' => [],
108           'critical' => [],
109           'unknown' => [],
110           'unsigned' => [],
111         };
112
113
114 my %dnsseczones;
115 # load list of classic zones that will do DNSSEC
116 chdir $INDIR or die "chdir $INDIR failed? $!\n";
117 opendir INDIR, '.' or die ("Cannot opendir $INDIR\n");
118 for my $file (sort {$a cmp $b} (readdir INDIR)) {
119         next if ( -l "$file" );
120         next unless ( -f "$file" );
121
122         my $do_dnssec = 1;
123         my $delegated = 1;
124         my $initial_refs = undef;
125         open(F, '<', $file) or die ("Cannot open $file: $!\n");
126         for (<F>) {
127                 if (/^; wzf:\s*dnssec\s*=\s*0\s*$/) { $do_dnssec = 0; }
128                 if (/^; delegated\s*=\s*no\s*$/) { $delegated = 0; }
129                 if (/^; check-initial-refs\s*=\s*(.*?)\s*$/) { $initial_refs = $1; }
130         };
131         close F;
132
133         if ($do_dnssec && $delegated) {
134                 die "Duplicate zone $file?\n" if exists $dnsseczones{$file};
135                 $dnsseczones{$file} = {};
136                 $dnsseczones{$file}->{'initial_refs'} = $initial_refs if defined $initial_refs;
137         } else {
138                 push @{$count->{'unsigned'}}, $file;
139         };
140 }
141 closedir(INDIR);
142
143 # load list of geodns zones that will do DNSSEC
144 if (defined $params->{'geozonedir'}) {
145         chdir $params->{'geozonedir'} or die "chdir $params->{'geozonedir'} failed? $!\n";
146         opendir INDIR, '.' or die ("Cannot opendir $params->{'geozonedir'}\n");
147         for my $file (sort {$a cmp $b} (readdir INDIR)) {
148                 next unless $file =~ /\.zone$/;
149
150                 my $zone = basename($file, '.zone');
151                 die "Duplicate zone $zone?\n" if exists $dnsseczones{$zone};
152                 $dnsseczones{$zone} = {};
153         }
154         closedir(INDIR);
155 }
156
157 my @details;
158
159 my %threads;
160 for my $zone (sort {$a cmp $b} keys %dnsseczones) {
161         die "Duplicate zone $zone?\n"  if defined $threads{$zone};
162         my $thr = threads->create({'context' => 'list'},
163                                   \&check_one, $zone, $CHECK, $dnsseczones{$zone}, $params);
164         $threads{$zone} = $thr;
165 }
166
167 my $begin = time;
168 while (time - $begin <= $params->{timeout}) {
169         for my $zone (sort {$a cmp $b} keys %threads) {
170                 next unless $threads{$zone}->is_joinable();
171
172                 my ($res, $det) = $threads{$zone}->join();
173
174                 push @details, @$det;
175
176                 if ($res == 0) { push @{$count->{'ok'}}, $zone; }
177                 elsif ($res == 1) { push @{$count->{'warn'}}, $zone; }
178                 elsif ($res == 2) { push @{$count->{'critical'}}, $zone; }
179                 else { push @{$count->{'unknown'}}, $zone; };
180                 delete $threads{$zone};
181         }
182         last if scalar keys %threads == 0;
183         print STDERR (scalar keys %threads), " threads left: ", join(" ", keys %threads), "\n" if $params->{'debug'};
184         sleep 1;
185 }
186 for my $zone (sort {$a cmp $b} keys %threads) {
187         push @{$count->{'warn'}}, $zone;
188         push @details, "$zone: timeout during check\n";
189         $threads{$zone}->kill('KILL')->detach();
190 }
191
192 for my $k (keys %$count) {
193         @{$count->{$k}} = sort {$a cmp $b} @{$count->{$k}};
194 }
195
196 my $exit;
197 my %state_mapping = (
198         'unknown' => 255,
199         'critical' => 2,
200         'warn' => 1,
201         'ok' => 0 );
202
203 for my $state (sort {$state_mapping{$b} <=> $state_mapping{$a}} keys %state_mapping) {
204         if (scalar @{$count->{$state}}) {
205                 printf "%s: %d", uc($state), scalar @{$count->{$state}};
206                 if ($state_mapping{$state} > 0) {
207                         print ": ", join(', ', @{$count->{$state}});
208                 };
209                 print "; ";
210                 $exit = $state_mapping{$state} unless defined $exit;
211         };
212 };
213 printf "unsigned: %d", scalar @{$count->{'unsigned'}};
214 print "\n";
215 for (@details) {
216         s/\|/;/g;
217         print $_;
218 }
219 exit ((defined $exit) ? $exit : 0);