dsa-check-zone-rrsig-expiration: configurable packet size, and change default size
[mirror/dsa-nagios.git] / dsa-nagios-checks / checks / dsa-check-zone-rrsig-expiration
1 #!/usr/bin/perl
2
3 # downloaded from http://dns.measurement-factory.com/tools/nagios-plugins/check_zone_rrsig_expiration.html
4 # on 2010-02-07 by Peter Palfrader
5
6 # $Id: check_zone_rrsig_expiration,v 1.7 2008/11/25 01:36:36 wessels Exp $
7 #
8 # check_zone_rrsig_expiration
9 #
10 # nagios plugin to check expiration times of RRSIG records.  Reminds
11 # you if its time to re-sign your zone.
12
13 # Copyright (c) 2008, The Measurement Factory, Inc. All rights reserved.
14
15 # Redistribution and use in source and binary forms, with or without
16 # modification, are permitted provided that the following conditions
17 # are met:
18
19 # Redistributions of source code must retain the above copyright
20 # notice, this list of conditions and the following disclaimer.
21 # Redistributions in binary form must reproduce the above copyright
22 # notice, this list of conditions and the following disclaimer in the
23 # documentation and/or other materials provided with the distribution.
24 # Neither the name of The Measurement Factory nor the names of its
25 # contributors may be used to endorse or promote products derived
26 # from this software without specific prior written permission.
27
28 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
31 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
32 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
33 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
34 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
35 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
36 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
38 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39 # POSSIBILITY OF SUCH DAMAGE.
40
41 # Copyright (c) 2010 Peter Palfrader <peter@palfrader.org>
42 # - various fixes and cleanups
43 # - do more than one zone
44 # Copyright (c) 2012 Peter Palfrader <peter@palfrader.org>
45 #  - add -s option to configure udp packet size.  default changed from 4k to 1k
46
47
48 # usage
49 #
50 # define command {
51 #   command_name    check-zone-rrsig
52 #   command_line    /usr/local/libexec/nagios-local/check_zone_rrsig -Z $HOSTADDRESS$
53 # }
54
55 # define service {
56 #   name                dns-rrsig-service
57 #   check_command       check-zone-rrsig
58 #   ...
59 # }
60
61 # define host {
62 #   use dns-zone
63 #   host_name zone.example.com
64 #   alias ZONE example.com
65 # }
66
67 # define service {
68 #   use dns-rrsig-service
69 #   host_name zone.example.com
70 # }
71
72 use warnings;
73 use strict;
74
75 use Getopt::Std;
76 use Net::DNS::Resolver;
77 use Time::HiRes qw ( gettimeofday tv_interval);
78 use Time::Local;
79 use List::Util qw ( shuffle );
80
81 sub convert_time {
82         my $in = shift;
83         my ($ticks, $unit) = ($in =~ /^(\d+)([smhdw]?)$/);
84
85         if ($unit eq 's' || $unit eq '') { }
86         elsif ($unit eq 'm') { $ticks *= 60; }
87         elsif ($unit eq 'h') { $ticks *= 60*60; }
88         elsif ($unit eq 'd') { $ticks *= 60*60*24; }
89         elsif ($unit eq 'w') { $ticks *= 60*60*24*7; }
90         else { die "Invalid unit '$unit' in '$in'\n" }
91         return $ticks;
92 }
93
94 my %opts = (t=>30, s=>1024);
95 getopts('hdt:c:w:s:', \%opts);
96 usage() unless scalar @ARGV == 1;
97 usage() if $opts{h};
98 my $zone = $ARGV[0];
99
100 my $data;
101 my $start;
102 my $stop;
103 my $CRIT = 3 * 3600*24;
104 my $WARN = 7 * 3600*24;
105
106 $CRIT = convert_time($opts{c}) if defined $opts{c};
107 $WARN = convert_time($opts{w}) if defined $opts{w};
108
109 my @refs = qw (
110 a.root-servers.net
111 b.root-servers.net
112 c.root-servers.net
113 d.root-servers.net
114 e.root-servers.net
115 f.root-servers.net
116 g.root-servers.net
117 h.root-servers.net
118 i.root-servers.net
119 j.root-servers.net
120 k.root-servers.net
121 l.root-servers.net
122 m.root-servers.net
123 );
124
125 $start = [gettimeofday()];
126 do_recursion();
127 do_queries();
128 $stop = [gettimeofday()];
129 do_analyze();
130
131 sub do_recursion {
132         my $done = 0;
133         my $res = Net::DNS::Resolver->new;
134         do {
135                 print STDERR "\nRECURSE\n" if $opts{d};
136                 my $pkt;
137                 foreach my $ns (shuffle @refs) {
138                         print STDERR "sending query for $zone RRSIG to $ns\n" if $opts{d};
139                         $res->nameserver($ns);
140                         $res->udp_timeout($opts{t});
141                         $res->udppacketsize($opts{s});
142                         $pkt = $res->send($zone, 'RRSIG');
143                         last if $pkt;
144                 }
145                 critical("No response to seed query") unless $pkt;
146                 critical($pkt->header->rcode . " from " . $pkt->answerfrom)
147                         unless ($pkt->header->rcode eq 'NOERROR');
148                 @refs = ();
149                 foreach my $rr ($pkt->authority) {
150                         print STDERR $rr->string, "\n" if $opts{d};
151                         push (@refs, $rr->nsdname);
152                         next unless lc($rr->name) eq lc($zone);
153                         add_nslist_to_data($pkt);
154                         $done = 1;
155                 }
156         } while (! $done);
157 }
158
159
160 sub do_queries {
161         my $n;
162         do {
163                 $n = 0;
164                 foreach my $ns (keys %$data) {
165                         next if $data->{$ns}->{done};
166                         print STDERR "\nQUERY $ns\n" if $opts{d};
167
168                         my $pkt = send_query($zone, 'RRSIG', $ns);
169                         add_nslist_to_data($pkt);
170                         $data->{$ns}->{queries}->{RRSIG} = $pkt;
171
172                         print STDERR "done with $ns\n" if $opts{d};
173                         $data->{$ns}->{done} = 1;
174                         $n++;
175                 }
176         } while ($n);
177 }
178
179 sub do_analyze {
180         my $nscount = 0;
181         my $NOW = time;
182         my %MAX_EXP_BY_TYPE;
183         foreach my $ns (keys %$data) {
184                 print STDERR "\nANALYZE $ns\n" if $opts{d};
185                 my $pkt = $data->{$ns}->{queries}->{RRSIG};
186                 critical("No response from $ns") unless $pkt;
187                 print STDERR $pkt->string if $opts{d};
188                 critical($pkt->header->rcode . " from $ns")
189                         unless ($pkt->header->rcode eq 'NOERROR');
190                 critical("$ns is lame") unless $pkt->header->ancount;
191                 foreach my $rr ($pkt->answer) {
192                         next unless $rr->type eq 'RRSIG';
193                         my $exp = sigrr_exp_epoch($rr);
194                         my $T = $rr->typecovered;
195                         if (!defined($MAX_EXP_BY_TYPE{$T}->{exp}) || $exp > $MAX_EXP_BY_TYPE{$T}->{exp}) {
196                                 $MAX_EXP_BY_TYPE{$T}->{exp} = $exp;
197                                 $MAX_EXP_BY_TYPE{$T}->{ns} = $ns;
198                         }
199                 }
200                 $nscount++;
201         }
202         warning("No nameservers found.  Is '$zone' a zone?") if ($nscount < 1);
203         warning("No RRSIGs found") unless %MAX_EXP_BY_TYPE;
204         my $min_exp = undef;
205         my $min_ns = undef;
206         my $min_type = undef;
207         foreach my $T (keys %MAX_EXP_BY_TYPE) {
208                 printf STDERR ("%s RRSIG expires in %.1f days\n", $T, ($MAX_EXP_BY_TYPE{$T}->{exp}-$NOW)/86400) if $opts{d};
209                 if (!defined($min_exp) || $MAX_EXP_BY_TYPE{$T}->{exp} < $min_exp) {
210                         $min_exp = $MAX_EXP_BY_TYPE{$T}->{exp};
211                         $min_ns = $MAX_EXP_BY_TYPE{$T}->{ns};
212                         $min_type = $T;
213                 }
214         }
215         critical("$min_ns has expired RRSIGs") if ($min_exp < $NOW);
216         if ($min_exp - $NOW < ($CRIT)) {
217                 my $ND = sprintf "%3.1f days", ($min_exp-$NOW)/86400;
218                 critical("$min_type RRSIG expires in $ND at $min_ns")
219         }
220         if ($min_exp - $NOW < ($WARN)) {
221                 my $ND = sprintf "%3.1f days", ($min_exp-$NOW)/86400;
222                 warning("$min_type RRSIG expires in $ND at $min_ns")
223         }
224         success(sprintf("No RRSIGs expiring in the next %3.1f days", $WARN/86400));
225 }
226
227 sub sigrr_exp_epoch {
228         my $rr = shift;
229         die unless $rr->type eq 'RRSIG';
230         my $exp = $rr->sigexpiration;
231         die "bad exp time '$exp'"
232                 unless $exp =~ /^(\d\d\d\d)(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)$/;
233         my $exp_epoch = timegm($6,$5,$4,$3,$2-1,$1);
234         return $exp_epoch;
235 }
236
237 sub add_nslist_to_data {
238         my $pkt = shift;
239         foreach my $ns (get_nslist($pkt)) {
240                 next if defined $data->{$ns}->{done};
241                 print STDERR "adding NS $ns\n" if $opts{d};
242                 $data->{$ns}->{done} |= 0;
243         }
244 }
245
246 sub success {
247         output('OK', shift);
248         exit(0);
249 }
250
251 sub warning {
252         output('WARNING', shift);
253         exit(1);
254 }
255
256 sub critical {
257         output('CRITICAL', shift);
258         exit(2);
259 }
260
261 sub output {
262         my $state = shift;
263         my $msg = shift;
264         $stop = [gettimeofday()] unless $stop;
265         my $latency = tv_interval($start, $stop);
266         printf "ZONE %s: %s; (%.2fs) |time=%.6fs;;;0.000000\n",
267                 $state,
268                 $msg,
269                 $latency,
270                 $latency;
271 }
272
273 sub usage {
274         print STDERR "usage: $0 [-d] [-w=<warn>] [-c=<crit>] [-t=<timeout>] <zone>\n";
275         exit 3;
276 }
277
278 sub send_query {
279         my $qname = shift;
280         my $qtype = shift;
281         my $server = shift;
282         my $res = Net::DNS::Resolver->new;
283         $res->nameserver($server) if $server;
284         $res->udp_timeout($opts{t});
285         $res->retry(2);
286         $res->udppacketsize($opts{s});
287         my $pkt = $res->send($qname, $qtype);
288         unless ($pkt) {
289                 $res->usevc(1);
290                 $res->tcp_timeout($opts{t});
291                 $pkt = $res->send($qname, $qtype);
292         }
293         return $pkt;
294 }
295
296 sub get_nslist {
297         my $pkt = shift;
298         return () unless $pkt;
299         return () unless $pkt->authority;
300         my @nslist;
301         foreach my $rr ($pkt->authority) {
302                 next unless ($rr->type eq 'NS');
303                 next unless ($rr->name eq $zone);
304                 push(@nslist, lc($rr->nsdname));
305         }
306         return @nslist;
307 }