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