dsa-check-zone-rrsig-expiration-many: also allow checking of geozones
[mirror/dsa-nagios.git] / dsa-nagios-checks / checks / dsa-check-zone-rrsig-expiration-many
1 #!/usr/bin/perl
2
3 # Copyright (c) 2010 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 English;
27 use Getopt::Long;
28 use FindBin qw($Bin);
29 use YAML;
30 use File::Basename;
31
32 my $CHECK = $Bin.'/dsa-check-zone-rrsig-expiration';
33
34 $SIG{__DIE__} = sub {
35         print @_;
36         exit 3;
37 };
38
39 sub convert_time {
40         my $ticks = shift;
41         my $unit = shift;
42
43         unless (defined $unit) {
44                 my $newticks;
45                 ($newticks, $unit) = $ticks =~ m/^(\d*)([smhdw]?)$/;
46                 if (!defined $newticks) {
47                         print STDERR "Warning: invalid timestring to convert '$ticks'\n";
48                         return $ticks;
49                 }
50                 $ticks = $newticks;
51         }
52
53         if ($unit eq 's' || $unit eq '') { }
54         elsif ($unit eq 'm') { $ticks *= 60; }
55         elsif ($unit eq 'h') { $ticks *= 60*60; }
56         elsif ($unit eq 'd') { $ticks *= 60*60*24; }
57         elsif ($unit eq 'w') { $ticks *= 60*60*24*7; }
58         else { print STDERR "Warning: invalid unit '$unit'\n" }
59         return $ticks;
60 }
61
62 my $USAGE = "Usage: $PROGRAM_NAME [--help] | [--warn=<nn>] [--critical=<nn>] [--geozonedir=<geodir>] <indir>\n";
63 my $params = { 'warn' => '14d', 'critical' => '7d' };
64 Getopt::Long::config('bundling');
65 GetOptions (
66         '--help' => \$params->{'help'},
67         '--warn=s' => \$params->{'warn'},
68         '--critical=s' => \$params->{'critical'},
69         '--geozonedir=s' => \$params->{'geozonedir'},
70 ) or die ($USAGE);
71 if ($params->{'help'}) {
72         print $USAGE;
73         exit(0);
74 };
75 die ($USAGE) unless (scalar @ARGV == 1);
76 my $INDIR = shift;
77
78
79 my $count =
80         { 'ok' => [],
81           'warn' => [],
82           'critical' => [],
83           'unknown' => [],
84           'unsigned' => [],
85         };
86
87
88 my @dnsseczones;
89 # load list of classic zones that will do DNSSEC
90 chdir $INDIR or die "chdir $INDIR failed? $!\n";
91 opendir INDIR, '.' or die ("Cannot opendir $INDIR\n");
92 for my $file (sort {$a cmp $b} (readdir INDIR)) {
93         next if ( -l "$file" );
94         next unless ( -f "$file" );
95         next if $file =~ /^(dsset|keyset)-/;
96
97         my $do_dnssec = 0;
98         open(F, '<', $file) or die ("Cannot open $file: $!\n");
99         for (<F>) {
100                 if (/^; wzf:\s*dnssec\s*=\s*1\s*$/) { $do_dnssec = 1; last; }
101         };
102         close F;
103
104         if ($do_dnssec) {
105                 push @dnsseczones, $file;
106         } else {
107                 push @{$count->{'unsigned'}}, $file;
108         };
109 }
110 closedir(INDIR);
111
112 # load list of geodns zones that will do DNSSEC
113 if (defined $params->{'geozonedir'}) {
114         chdir $params->{'geozonedir'} or die "chdir $params->{'geozonedir'} failed? $!\n";
115         opendir INDIR, '.' or die ("Cannot opendir $params->{'geozonedir'}\n");
116         for my $file (sort {$a cmp $b} (readdir INDIR)) {
117                 next unless $file =~ /\.zone$/;
118
119                 open (F, '<', $file) or die "Cannot open $file: $!\n";
120                 my ($zc, undef, undef) = Load(join "", (<F>));
121                 close F;
122
123                 my $zone = basename($file, '.zone');
124
125                 if ($zc->{'dnssec'}) {
126                         push @dnsseczones, $zone;
127                 } else {
128                         push @{$count->{'unsigned'}}, $zone;
129                 };
130         }
131         closedir(INDIR);
132 }
133
134
135 my @details;
136
137 for my $zone (sort {$a cmp $b} @dnsseczones) {
138
139         open(P, '-|', ($CHECK, '-w', $params->{'warn'}, '-c', $params->{'critical'}, $zone)) or die ("Cannot run $CHECK for $zone\n");
140         my @p = <P>;
141         close P;
142         $p[0] = $zone.': '. $p[0] if (scalar @p > 0);
143         push @details, @p;
144
145         my $res = $CHILD_ERROR >> 8;
146         if ($res == 0) { push @{$count->{'ok'}}, $zone; }
147         elsif ($res == 1) { push @{$count->{'warn'}}, $zone; }
148         elsif ($res == 2) { push @{$count->{'critical'}}, $zone; }
149         else { push @{$count->{'unknown'}}, $zone; };
150 };
151
152 my $exit;
153 my %state_mapping = (
154         'unknown' => 255,
155         'critical' => 2,
156         'warn' => 1,
157         'ok' => 0 );
158
159 for my $state (sort {$state_mapping{$b} <=> $state_mapping{$a}} keys %state_mapping) {
160         if (scalar @{$count->{$state}}) {
161                 printf "%s: %d", uc($state), scalar @{$count->{$state}};
162                 if ($state_mapping{$state} > 0) {
163                         print ": ", join(', ', @{$count->{$state}});
164                 };
165                 print "; ";
166                 $exit = $state_mapping{$state} unless defined $exit;
167         };
168 };
169 printf "unsigned: %d", scalar @{$count->{'unsigned'}};
170 print "\n";
171 print $_ for (@details);
172 exit $exit;