3 # downloaded from http://dns.measurement-factory.com/tools/nagios-plugins/check_zone_rrsig_expiration.html
4 # on 2010-02-07 by Peter Palfrader
6 # $Id: check_zone_rrsig_expiration,v 1.7 2008/11/25 01:36:36 wessels Exp $
8 # check_zone_rrsig_expiration
10 # nagios plugin to check expiration times of RRSIG records. Reminds
11 # you if its time to re-sign your zone.
13 # Copyright (c) 2008, The Measurement Factory, Inc. All rights reserved.
15 # Redistribution and use in source and binary forms, with or without
16 # modification, are permitted provided that the following conditions
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.
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.
44 # command_name check-zone-rrsig
45 # command_line /usr/local/libexec/nagios-local/check_zone_rrsig -Z $HOSTADDRESS$
49 # name dns-rrsig-service
50 # check_command check-zone-rrsig
56 # host_name zone.example.com
57 # alias ZONE example.com
61 # use dns-rrsig-service
62 # host_name zone.example.com
69 use Net::DNS::Resolver;
70 use Time::HiRes qw ( gettimeofday tv_interval);
72 use List::Util qw ( shuffle );
75 getopts('Z:dt:', \%opts);
76 usage() unless $opts{Z};
103 $start = [gettimeofday()];
106 $stop = [gettimeofday()];
111 my $res = Net::DNS::Resolver->new;
113 print STDERR "\nRECURSE\n" if $opts{d};
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');
123 critical("No response to seed query") unless $pkt;
124 critical($pkt->header->rcode . " from " . $pkt->answerfrom)
125 unless ($pkt->header->rcode eq 'NOERROR');
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);
142 foreach my $ns (keys %$data) {
143 next if $data->{$ns}->{done};
144 print STDERR "\nQUERY $ns\n" if $opts{d};
146 my $pkt = send_query($zone, 'RRSIG', $ns);
147 add_nslist_to_data($pkt);
148 $data->{$ns}->{queries}->{RRSIG} = $pkt;
150 print STDERR "done with $ns\n" if $opts{d};
151 $data->{$ns}->{done} = 1;
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;
180 warning("No nameservers found. Is '$zone' a zone?") if ($nscount < 1);
181 warning("No RRSIGs found") unless %MAX_EXP_BY_TYPE;
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};
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")
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")
202 success("No RRSIGs expiring in the next $WARN_DAYS days");
205 sub sigrr_exp_epoch {
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);
215 sub add_nslist_to_data {
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;
230 output('WARNING', shift);
235 output('CRITICAL', shift);
242 $stop = [gettimeofday()] unless $stop;
243 my $latency = tv_interval($start, $stop);
244 printf "ZONE %s: %s; (%.2fs) |time=%.6fs;;;0.000000\n",
252 print STDERR "usage: $0 -Z zone\n";
260 my $res = Net::DNS::Resolver->new;
261 $res->nameserver($server) if $server;
262 $res->udp_timeout($opts{t});
264 $res->udppacketsize(4096);
265 my $pkt = $res->send($qname, $qtype);
268 $res->tcp_timeout($opts{t});
269 $pkt = $res->send($qname, $qtype);
276 return () unless $pkt;
277 return () unless $pkt->authority;
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));