blib/lib/Monitoring/Sneck/Checks/dns_sync.pm

filter: Statementstmt 97.7% 85 / 87 Branchbran 86.9% 40 / 46 Conditioncond 90.0% 9 / 10 MC/DCmcdc 66.6% 4 / 6 Subroutinesub 100% 8 / 8 Podpod 100% 4 / 4 totaltotal 93.1% 150 / 161 CC 33 SCAR 28.9
CC
33
Cov
94%
check
25.1
validate
9.2
BEGIN
1.0
CRAP 17.9
« blib/lib/Monitoring/Sneck/Checks/cciss.pm ↑ Summary blib/lib/Monitoring/Sneck/Checks/resolv.pm »
1 package Monitoring::Sneck::Checks::dns_sync;
2  
3 3 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use parent 'Monitoring::Sneck::Checks::base';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =head1 NAME
9  
10 Monitoring::Sneck::Checks::dns_sync - checks a zone's nameservers have the same serial
11  
12 =head1 VERSION
13  
14 Version 0.0.1
15  
16 =cut
17  
18 3 our $VERSION = '0.0.1';
19  
20 =head1 SYNOPSIS
21  
22 zone_sync|perl:dns_sync -z example.org
23 zone_sync_internal|perl:dns_sync -z internal.example.org -n 192.0.2.53 -n 192.0.2.54 -c
24  
25 =head1 DESCRIPTION
26  
27 Finds a zone's nameservers from its NS records, then asks each of
28 their addresses for the zone's SOA through L<Net::DNS>, without
29 recursion. A nameserver with no address, or a address that does not
30 answer, gives a error, or is not authoritative, is CRITICAL. That last
31 is a lame delegation.
32  
33 Serials that differ are WARNING, as a secondary that has not caught up
34 yet, or CRITICAL with --critical.
35  
36 =head1 OPTIONS
37  
38 - -z, --zone :: The zone. Required.
39  
40 - -s, --server :: A server to ask for the NS records and the addresses
41 of the nameservers. May be given more than once. The system's if not
42 given.
43  
44 - -n, --nameserver :: A nameserver to check, a IP or name, in place of
45 looking up the NS records. May be given more than once.
46  
47 - -4, --ipv4 :: Only check the IPv4 addresses of nameservers found by
48 name. A IP given to --nameserver is checked either way.
49  
50 - -6, --ipv6 :: The same for IPv6.
51  
52 - -c, --critical :: Serials that differ are CRITICAL.
53  
54 =cut
55  
56 12 ▶sub option_spec { return ( 'zone|z=s', 'server|s=s@', 'nameserver|n=s@', 'ipv4|4', 'ipv6|6', 'critical|c' ) }
Subroutine option_spec: called CC=1 SCAR=0.0
Pod documented
57  
58 12 ▶sub required { return ('zone') }
Subroutine required: called CC=1 SCAR=0.0
Pod documented
59  
60 12 ▶sub validate {
Subroutine validate: called CC=9 SCAR=22.2
Pod documented
61 12 my ( $self, $options ) = @_;
62  
63 12 my @problems;
64 push( @problems, '--zone "' . $options->{zone} . '" is not a zone' )
65 12 ▶ if defined( $options->{zone} ) && !$self->is_host( $options->{zone} =~ s/\.\z//r );
Branch: 1 branch
BranchTrueFalseTotal
if defined $options->{"zone"} and not $self->is_host($options->{"zone"} =~ s/\.\z//ru) 0 12 12
Condition66%2/3
defined $options->{"zone"} and not $self->is_host($options->{"zone"} =~ s/\.\z//ru)
!ll&&!rl&&r
1110
MC/DC0%0/2
defined $options->{"zone"} and not $self->is_host($options->{"zone"} =~ s/\.\z//ru)
defined $options->{"zone"} not $self->is_host($options->{"zone"} =~ s/\.\z//ru)
Truth table66%2/3
A and not(B)
A= defined $options->{"zone"}B= not $self->is_host($options->{"zone"} =~ s/\.\z//ru)
ABresult
0X0
100
111
66 12 foreach my $name ( 'server', 'nameserver' ) {
67 24 ▶ foreach my $host ( @{ $options->{$name} // [] } ) {
Condition100%2/2
$options->{$name} // []
l!l
123
MC/DC100%1/1
$options->{$name} // []
$options->{$name}
Truth table100%2/2
A
A= $options->{$name}
Aresult
11
00
68 2 ▶ push( @problems, '--' . $name . ' "' . $host . '" is not a IP or hostname' ) if !$self->is_host($host);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_host($host) 0 2 2
69 }
70 }
71 12 ▶ push( @problems, '--ipv4 and --ipv6 can not both be given' ) if $options->{ipv4} && $options->{ipv6};
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"ipv4"} and $options->{"ipv6"} 1 11 12
Condition100%3/3
$options->{"ipv4"} and $options->{"ipv6"}
!ll&&!rl&&r
1011
MC/DC100%2/2
$options->{"ipv4"} and $options->{"ipv6"}
$options->{"ipv4"} $options->{"ipv6"}
Truth table100%3/3
A and B
A= $options->{"ipv4"}B= $options->{"ipv6"}
ABresult
0X0
100
111
72  
73 12 return @problems;
74 } ## end sub validate
75  
76 =head1 METHODS
77  
78 =head2 check
79  
80 Does the check.
81  
82 =cut
83  
84 10 ▶sub check {
Subroutine check: called CC=25 SCAR=32.2
Pod documented
85 10 my ($self) = @_;
86  
87 10 require Data::Validate::IP;
88 10 my $zone = $self->opt('zone') =~ s/\.\z//r;
89 10 my $resolver = $self->resolver( servers => $self->opt('server') );
90 10 ▶ my @names = @{ $self->opt('nameserver') // [] };
Condition100%2/2
$self->opt("nameserver") // []
l!l
19
MC/DC100%1/1
$self->opt("nameserver") // []
$self->opt("nameserver")
Truth table100%2/2
A
A= $self->opt("nameserver")
Aresult
11
00
91 10 ▶ if ( !defined( $names[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $names[0]) 9 1 10
92 9 my $reply = $resolver->send( $zone, 'NS' );
93 9 ▶ if ( !defined($reply) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $reply) 0 9 9
94 0 return $self->finish( $self->CRITICAL, 'no reply for ' . $zone . ' NS, ' . $resolver->errorstring );
95 }
96 9 @names = sort( map { lc( $_->nsdname ) =~ s/\.\z//r } grep { $_->type eq 'NS' } $reply->answer );
97 9 ▶ if ( !defined( $names[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $names[0]) 1 8 9
98 1 return $self->finish( $self->CRITICAL, $zone . ' has no NS records' );
99 }
100 } ## end if ( !defined( $names[0] ) )
101  
102 9 ▶ my @types = $self->opt('ipv4') ? ('A') : $self->opt('ipv6') ? ('AAAA') : ( 'A', 'AAAA' );
Branch: 2 branches
BranchTrueFalseTotal
$self->opt('ipv4') ? : 1 8 9
$self->opt('ipv6') ? : 0 8 8
103 9 my @codes;
104 my @problems;
105 9 my %serials;
106 9 my $answered = 0;
107 9 foreach my $name (@names) {
108 18 my @addresses;
109 18 ▶ if ( Data::Validate::IP::is_ip($name) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (Data::Validate::IP::is_ip($name)) { } 2 16 18
110 2 @addresses = ($name);
111 } else {
112 16 foreach my $type (@types) {
113 30 my $reply = $resolver->send( $name, $type );
114 30 ▶ next if !defined($reply);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $reply 0 30 30
115 push( @addresses,
116 22 ▶ map { $type eq 'AAAA' ? $_->address_short : $_->address }
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'AAAA' ? : 7 15 22
117 30 grep { $_->type eq $type } $reply->answer );
118 }
119 }
120 18 ▶ if ( !defined( $addresses[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $addresses[0]) 1 17 18
121 1 push( @codes, $self->CRITICAL );
122 1 push( @problems, $name . ' has no address' );
123 1 next;
124 }
125  
126 17 foreach my $address (@addresses) {
127 24 ▶ my $where = $name eq $address ? $name : $name . ' (' . $address . ')';
Branch: 1 branch
BranchTrueFalseTotal
$name eq $address ? : 2 22 24
128 24 my $asker = $self->resolver( servers => [$address], no_recurse => 1 );
129 24 my $reply = $asker->send( $zone, 'SOA' );
130 24 ▶ my ($soa) = defined($reply) ? grep { $_->type eq 'SOA' } $reply->answer : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $reply ? : 22 2 24
131 24 my $problem;
132 24 ▶ if ( !defined($reply) ) {
Branch: 4 branches
BranchTrueFalseTotal
if (not defined $reply) { } 2 22 24
elsif ($reply->header->rcode ne 'NOERROR') { } 1 21 22
elsif (not $reply->header->aa) { } 3 18 21
elsif (not defined $soa) { } 0 18 18
133 2 $problem = 'did not answer';
134 } elsif ( $reply->header->rcode ne 'NOERROR' ) {
135 1 $problem = 'got ' . $reply->header->rcode;
136 } elsif ( !$reply->header->aa ) {
137 3 $problem = 'is not authoritative';
138 } elsif ( !defined($soa) ) {
139 0 $problem = 'has no SOA';
140 }
141 24 ▶ if ( defined($problem) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $problem) 6 18 24
142 6 push( @codes, $self->CRITICAL );
143 6 push( @problems, $where . ' ' . $problem );
144 6 next;
145 }
146 18 $answered++;
147 18 push( @{ $serials{ $soa->serial } }, $where );
148 } ## end foreach my $address (@addresses)
149 } ## end foreach my $name (@names)
150  
151 9 my @serials = sort { $b <=> $a } keys(%serials);
152 9 ▶ if ( scalar(@serials) > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @serials > 1) 2 7 9
153 2 ▶ push( @codes, $self->opt('critical') ? $self->CRITICAL : $self->WARNING );
Branch: 1 branch
BranchTrueFalseTotal
$self->opt('critical') ? : 1 1 2
154 push( @problems,
155 2 'serials differ, ' . join( '; ', map { $_ . ' on ' . join( ', ', @{ $serials{$_} } ) } @serials ) );
156 }
157  
158 # the highest serial, as the one the others should catch up to
159 9 ▶ my $message
Branch: 1 branch
BranchTrueFalseTotal
defined $serials[0] ? : 8 1 9
160 = defined( $serials[0] )
161 ? $zone . ' serial ' . $serials[0] . ', ' . $answered . ' answered'
162 : $zone . ', none answered';
163 9 $message = $message . ', nameservers ' . join( ', ', @names );
164 9 ▶ $message = $message . ', ' . join( ', ', @problems ) if defined( $problems[0] );
Branch: 1 branch
BranchTrueFalseTotal
if defined $problems[0] 6 3 9
165  
166 9 return $self->finish( $self->worst(@codes), $message );
167 } ## end sub check
168  
169 3 1;
« blib/lib/Monitoring/Sneck/Checks/cciss.pm ↑ Summary blib/lib/Monitoring/Sneck/Checks/resolv.pm »