dsa-check-zone-rrsig-expiration* updates
[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 # Copyright (c) 2013 Peter Palfrader <peter@palfrader.org>
47 #  - add -r option to override initial refs.
48
49
50 # usage
51 #
52 # define command {
53 #   command_name    check-zone-rrsig
54 #   command_line    /usr/local/libexec/nagios-local/check_zone_rrsig -Z $HOSTADDRESS$
55 # }
56
57 # define service {
58 #   name                dns-rrsig-service
59 #   check_command       check-zone-rrsig
60 #   ...
61 # }
62
63 # define host {
64 #   use dns-zone
65 #   host_name zone.example.com
66 #   alias ZONE example.com
67 # }
68
69 # define service {
70 #   use dns-rrsig-service
71 #   host_name zone.example.com
72 # }
73
74 use warnings;
75 use strict;
76
77 use Getopt::Std;
78 use Net::DNS::Resolver;
79 use Time::HiRes qw ( gettimeofday tv_interval);
80 use Time::Local;
81 use List::Util qw ( shuffle );
82
83 sub convert_time {
84         my $in = shift;
85         my ($ticks, $unit) = ($in =~ /^(\d+)([smhdw]?)$/);
86
87         if ($unit eq 's' || $unit eq '') { }
88         elsif ($unit eq 'm') { $ticks *= 60; }
89         elsif ($unit eq 'h') { $ticks *= 60*60; }
90         elsif ($unit eq 'd') { $ticks *= 60*60*24; }
91         elsif ($unit eq 'w') { $ticks *= 60*60*24*7; }
92         else { die "Invalid unit '$unit' in '$in'\n" }
93         return $ticks;
94 }
95
96 my %opts = (t=>30, s=>1024);
97 getopts('hdt:c:w:s:r:', \%opts);
98 usage() unless scalar @ARGV == 1;
99 usage() if $opts{h};
100 my $zone = $ARGV[0];
101
102 my $data;
103 my $start;
104 my $stop;
105 my $CRIT = 3 * 3600*24;
106 my $WARN = 7 * 3600*24;
107
108 $CRIT = convert_time($opts{c}) if defined $opts{c};
109 $WARN = convert_time($opts{w}) if defined $opts{w};
110
111 my @refs = qw (
112 a.root-servers.net
113 b.root-servers.net
114 c.root-servers.net
115 d.root-servers.net
116 e.root-servers.net
117 f.root-servers.net
118 g.root-servers.net
119 h.root-servers.net
120 i.root-servers.net
121 j.root-servers.net
122 k.root-servers.net
123 l.root-servers.net
124 m.root-servers.net
125 );
126 @refs = split(/\s*,\s*/, $opts{r}) if (defined $opts{r});
127
128 $start = [gettimeofday()];
129 do_recursion();
130 do_queries();
131 $stop = [gettimeofday()];
132 do_analyze();
133
134 sub do_recursion {
135         my $done = 0;
136         my $res = Net::DNS::Resolver->new;
137         do {
138                 print STDERR "\nRECURSE\n" if $opts{d};
139                 my $pkt;
140                 foreach my $ns (shuffle @refs) {
141                         print STDERR "sending query for $zone RRSIG to $ns\n" if $opts{d};
142                         $res->nameserver($ns);
143                         $res->udp_timeout($opts{t});
144                         $res->udppacketsize($opts{s});
145                         $pkt = $res->send($zone, 'RRSIG');
146                         last if $pkt;
147                 }
148                 critical("No response to seed query") unless $pkt;
149                 critical($pkt->header->rcode . " from " . $pkt->answerfrom)
150                         unless ($pkt->header->rcode eq 'NOERROR');
151                 @refs = ();
152                 foreach my $rr ($pkt->authority) {
153                         print STDERR $rr->string, "\n" if $opts{d};
154                         push (@refs, $rr->nsdname);
155                         next unless lc($rr->name) eq lc($zone);
156                         add_nslist_to_data($pkt);
157                         $done = 1;
158                 }
159         } while (! $done);
160 }
161
162
163 sub do_queries {
164         my $n;
165         do {
166                 $n = 0;
167                 foreach my $ns (keys %$data) {
168                         next if $data->{$ns}->{done};
169                         print STDERR "\nQUERY $ns\n" if $opts{d};
170
171                         my $pkt = send_query($zone, 'RRSIG', $ns);
172                         add_nslist_to_data($pkt);
173                         $data->{$ns}->{queries}->{RRSIG} = $pkt;
174
175                         print STDERR "done with $ns\n" if $opts{d};
176                         $data->{$ns}->{done} = 1;
177                         $n++;
178                 }
179         } while ($n);
180 }
181
182 sub do_analyze {
183         my $nscount = 0;
184         my $NOW = time;
185         my %MAX_EXP_BY_TYPE;
186         foreach my $ns (keys %$data) {
187                 print STDERR "\nANALYZE $ns\n" if $opts{d};
188                 my $pkt = $data->{$ns}->{queries}->{RRSIG};
189                 critical("No response from $ns") unless $pkt;
190                 print STDERR $pkt->string if $opts{d};
191                 critical($pkt->header->rcode . " from $ns")
192                         unless ($pkt->header->rcode eq 'NOERROR');
193                 critical("$ns is lame") unless $pkt->header->ancount;
194                 foreach my $rr ($pkt->answer) {
195                         next unless $rr->type eq 'RRSIG';
196                         my $exp = sigrr_exp_epoch($rr);
197                         my $T = $rr->typecovered;
198                         if (!defined($MAX_EXP_BY_TYPE{$T}->{exp}) || $exp > $MAX_EXP_BY_TYPE{$T}->{exp}) {
199                                 $MAX_EXP_BY_TYPE{$T}->{exp} = $exp;
200                                 $MAX_EXP_BY_TYPE{$T}->{ns} = $ns;
201                         }
202                 }
203                 $nscount++;
204         }
205         warning("No nameservers found.  Is '$zone' a zone?") if ($nscount < 1);
206         warning("No RRSIGs found") unless %MAX_EXP_BY_TYPE;
207         my $min_exp = undef;
208         my $min_ns = undef;
209         my $min_type = undef;
210         foreach my $T (keys %MAX_EXP_BY_TYPE) {
211                 printf STDERR ("%s RRSIG expires in %.1f days\n", $T, ($MAX_EXP_BY_TYPE{$T}->{exp}-$NOW)/86400) if $opts{d};
212                 if (!defined($min_exp) || $MAX_EXP_BY_TYPE{$T}->{exp} < $min_exp) {
213                         $min_exp = $MAX_EXP_BY_TYPE{$T}->{exp};
214                         $min_ns = $MAX_EXP_BY_TYPE{$T}->{ns};
215                         $min_type = $T;
216                 }
217         }
218         critical("$min_ns has expired RRSIGs") if ($min_exp < $NOW);
219         if ($min_exp - $NOW < ($CRIT)) {
220                 my $ND = sprintf "%3.1f days", ($min_exp-$NOW)/86400;
221                 critical("$min_type RRSIG expires in $ND at $min_ns")
222         }
223         if ($min_exp - $NOW < ($WARN)) {
224                 my $ND = sprintf "%3.1f days", ($min_exp-$NOW)/86400;
225                 warning("$min_type RRSIG expires in $ND at $min_ns")
226         }
227         success(sprintf("No RRSIGs at zone apex expiring in the next %3.1f days", $WARN/86400));
228 }
229
230 sub sigrr_exp_epoch {
231         my $rr = shift;
232         die unless $rr->type eq 'RRSIG';
233         my $exp = $rr->sigexpiration;
234         die "bad exp time '$exp'"
235                 unless $exp =~ /^(\d\d\d\d)(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)$/;
236         my $exp_epoch = timegm($6,$5,$4,$3,$2-1,$1);
237         return $exp_epoch;
238 }
239
240 sub add_nslist_to_data {
241         my $pkt = shift;
242         foreach my $ns (get_nslist($pkt)) {
243                 next if defined $data->{$ns}->{done};
244                 print STDERR "adding NS $ns\n" if $opts{d};
245                 $data->{$ns}->{done} |= 0;
246         }
247 }
248
249 sub success {
250         output('OK', shift);
251         exit(0);
252 }
253
254 sub warning {
255         output('WARNING', shift);
256         exit(1);
257 }
258
259 sub critical {
260         output('CRITICAL', shift);
261         exit(2);
262 }
263
264 sub output {
265         my $state = shift;
266         my $msg = shift;
267         $stop = [gettimeofday()] unless $stop;
268         my $latency = tv_interval($start, $stop);
269         printf "ZONE %s: %s; (%.2fs) |time=%.6fs;;;0.000000\n",
270                 $state,
271                 $msg,
272                 $latency,
273                 $latency;
274 }
275
276 sub usage {
277         print STDERR "usage: $0 [-d] [-w=<warn>] [-c=<crit>] [-t=<timeout>] <zone>\n";
278         exit 3;
279 }
280
281 sub send_query {
282         my $qname = shift;
283         my $qtype = shift;
284         my $server = shift;
285         my $res = Net::DNS::Resolver->new;
286         $res->nameserver($server) if $server;
287         $res->udp_timeout($opts{t});
288         $res->retry(2);
289         $res->udppacketsize($opts{s});
290         my $pkt = $res->send($qname, $qtype);
291         unless ($pkt) {
292                 $res->usevc(1);
293                 $res->tcp_timeout($opts{t});
294                 $pkt = $res->send($qname, $qtype);
295         }
296         return $pkt;
297 }
298
299 sub get_nslist {
300         my $pkt = shift;
301         return () unless $pkt;
302         return () unless $pkt->authority;
303         my @nslist;
304         foreach my $rr ($pkt->authority) {
305                 next unless ($rr->type eq 'NS');
306                 next unless ($rr->name eq $zone);
307                 push(@nslist, lc($rr->nsdname));
308         }
309         return @nslist;
310 }