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