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