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

filter: Statementstmt 96.9% 64 / 66 Branchbran 94.4% 34 / 36 Conditioncond 75.0% 9 / 12 MC/DCmcdc 50.0% 4 / 8 Subroutinesub 100% 9 / 9 Podpod 100% 4 / 4 totaltotal 91.8% 124 / 135 CC 26 SCAR 28.6
CC
26
Cov
94%
check
24.1
validate
3.1
BEGIN
1.0
CRAP 17.5
« blib/lib/Monitoring/Sneck/Checks/dns_sync.pm ↑ Summary blib/lib/Monitoring/Sneck/Checks/accounts.pm »
1 package Monitoring::Sneck::Checks::resolv;
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 3 ▶use File::Slurp qw(read_file);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 Monitoring::Sneck::Checks::resolv - checks /etc/resolv.conf and its nameservers
12  
13 =head1 VERSION
14  
15 Version 0.0.1
16  
17 =cut
18  
19 3 our $VERSION = '0.0.1';
20  
21 # The keywords resolv.conf may have, across Linux and the BSDs.
22 3 our %KEYWORDS = map { $_ => 1 } ( 'nameserver', 'domain', 'search', 'sortlist', 'options', 'lookup', 'family' );
23  
24 # How many nameservers the C library uses.
25 3 our $MAX_NAMESERVERS = 3;
26  
27 =head1 SYNOPSIS
28  
29 resolv|perl:resolv
30 resolv_works|perl:resolv -q www.example.org
31  
32 =head1 DESCRIPTION
33  
34 Reads resolv.conf. Lines it does not understand, keywords with nothing after them,
35 nameservers that are not IPs, bad domain or search domains, no
36 nameservers, or more than the C library uses, 3, are WARNING. Not being able to read it is CRITICAL.
37  
38 With --query, each nameserver is asked for it through L<Net::DNS>. Any
39 answer counts, NXDOMAIN included, as the server answered. None
40 answering is CRITICAL, some not answering is WARNING.
41  
42 =head1 OPTIONS
43  
44 - -f, --file :: The file.
45 Default :: /etc/resolv.conf
46  
47 - -q, --query :: A name to ask each nameserver for.
48  
49 =cut
50  
51 7 ▶sub option_spec { return ( 'file|f=s', 'query|q=s' ) }
Subroutine option_spec: called CC=1 SCAR=0.0
Pod documented
52  
53 7 ▶sub defaults { return { file => '/etc/resolv.conf' } }
Subroutine defaults: called CC=1 SCAR=0.0
Pod documented
54  
55 7 ▶sub validate {
Subroutine validate: called CC=3 SCAR=11.4
Pod documented
56 7 my ( $self, $options ) = @_;
57  
58 return ( '--query "' . $options->{query} . '" is not a hostname' )
59 7 ▶ if defined( $options->{query} ) && !$self->is_host( $options->{query} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $options->{"query"} and not $self->is_host($options->{"query"}) 0 7 7
Condition66%2/3
defined $options->{"query"} and not $self->is_host($options->{"query"})
!ll&&!rl&&r
430
MC/DC0%0/2
defined $options->{"query"} and not $self->is_host($options->{"query"})
defined $options->{"query"} not $self->is_host($options->{"query"})
Truth table66%2/3
A and not(B)
A= defined $options->{"query"}B= not $self->is_host($options->{"query"})
ABresult
0X0
100
111
60  
61 7 return ();
62 }
63  
64 =head1 METHODS
65  
66 =head2 check
67  
68 Does the check.
69  
70 =cut
71  
72 7 ▶sub check {
Subroutine check: called CC=24 SCAR=31.8
Pod documented
73 7 my ($self) = @_;
74  
75 7 require Data::Validate::IP;
76 7 my $file = $self->opt('file');
77 7 my $content = read_file( $file, err_mode => 'quiet' );
78 7 ▶ if ( !defined($content) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $content) 1 6 7
79 1 return $self->finish( $self->CRITICAL, 'can not read ' . $file . ', ' . $! );
80 }
81  
82 6 my @nameservers;
83 my @problems;
84 6 my $line_number = 0;
85 6 foreach my $line ( split( /\n/, $content ) ) {
86 28 $line_number++;
87 28 $line =~ s/[#;].*\z//;
88 28 $line =~ s/\A\s+|\s+\z//g;
89 28 ▶ next if $line eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $line eq "" 4 24 28
90  
91 24 my ( $keyword, @values ) = split( /\s+/, $line );
92 24 ▶ if ( !$KEYWORDS{$keyword} ) {
Branch: 4 branches
BranchTrueFalseTotal
if (not $Monitoring::Sneck::Checks::resolv::KEYWORDS{$keyword}) { } 1 23 24
elsif (not defined $values[0]) { } 1 22 23
elsif ($keyword eq 'nameserver') { } 13 9 22
elsif ($keyword eq 'domain' or $keyword eq 'search') { } 5 4 9
93 1 push( @problems, 'line ' . $line_number . ' has unknown keyword "' . $keyword . '"' );
94 } elsif ( !defined( $values[0] ) ) {
95 1 push( @problems, 'line ' . $line_number . ' has nothing after ' . $keyword );
96 } elsif ( $keyword eq 'nameserver' ) {
97 13 my $address = $values[0] =~ s/%.*\z//r;
98 13 ▶ if ( Data::Validate::IP::is_ip($address) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (Data::Validate::IP::is_ip($address)) { } 12 1 13
99 12 push( @nameservers, $values[0] );
100 } else {
101 1 push( @problems, 'line ' . $line_number . ' nameserver "' . $values[0] . '" is not a IP' );
102 }
103 ▶ } elsif ( $keyword eq 'domain' || $keyword eq 'search' ) {
Condition66%2/3
$keyword eq "domain" or $keyword eq "search"
l!l&&r!l&&!r
054
MC/DC50%1/2
$keyword eq "domain" or $keyword eq "search"
$keyword eq "domain" $keyword eq "search"
Truth table66%2/3
A or B
A= $keyword eq "domain"B= $keyword eq "search"
ABresult
1X1
011
000
104 5 foreach my $domain (@values) {
105 9 ▶ next if $domain eq '.' || $self->is_host( $domain =~ s/\.\z//r );
Branch: 1 branch
BranchTrueFalseTotal
if $domain eq "." or $self->is_host($domain =~ s/\.\z//ru) 8 1 9
Condition100%3/3
$domain eq "." or $self->is_host($domain =~ s/\.\z//ru)
l!l&&r!l&&!r
441
MC/DC100%2/2
$domain eq "." or $self->is_host($domain =~ s/\.\z//ru)
$domain eq "." $self->is_host($domain =~ s/\.\z//ru)
Truth table100%3/3
A or B
A= $domain eq "."B= $self->is_host($domain =~ s/\.\z//ru)
ABresult
1X1
011
000
106 1 push( @problems, 'line ' . $line_number . ' ' . $keyword . ' "' . $domain . '" is not a domain' );
107 }
108 }
109 } ## end foreach my $line ( split( /\n/, $content ) )
110  
111 6 ▶ if ( !defined( $nameservers[0] ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $nameservers[0]) { } 1 5 6
elsif (scalar @nameservers > $Monitoring::Sneck::Checks::resolv::MAX_NAMESERVERS) { } 1 4 5
112 1 push( @problems, 'no nameservers, so the local host is used' );
113 } elsif ( scalar(@nameservers) > $MAX_NAMESERVERS ) {
114 1 push( @problems, scalar(@nameservers) . ' nameservers, but only the first ' . $MAX_NAMESERVERS . ' are used' );
115 }
116 6 ▶ my @codes = ( defined( $problems[0] ) ? $self->WARNING : $self->OK );
Branch: 1 branch
BranchTrueFalseTotal
defined $problems[0] ? : 2 4 6
117  
118 6 ▶ if ( defined( $self->opt('query') ) && defined( $nameservers[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->opt("query") and defined $nameservers[0]) 3 3 6
Condition66%2/3
defined $self->opt("query") and defined $nameservers[0]
!ll&&!rl&&r
303
MC/DC50%1/2
defined $self->opt("query") and defined $nameservers[0]
defined $self->opt("query") defined $nameservers[0]
Truth table66%2/3
A and B
A= defined $self->opt("query")B= defined $nameservers[0]
ABresult
0X0
100
111
119 3 my @silent;
120 3 foreach my $nameserver (@nameservers) {
121 6 my $resolver = $self->resolver( servers => [ $nameserver =~ s/%.*\z//r ] );
122 6 my $reply = $resolver->send( $self->opt('query'), 'A' );
123 6 ▶ push( @silent, $nameserver ) if !defined($reply);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $reply 2 4 6
124 }
125 3 ▶ if ( scalar(@silent) == scalar(@nameservers) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @silent == scalar @nameservers) { } 1 2 3
elsif (defined $silent[0]) { } 0 2 2
126 1 push( @codes, $self->CRITICAL );
127 1 push( @problems, 'no nameserver answered for ' . $self->opt('query') );
128 } elsif ( defined( $silent[0] ) ) {
129 0 push( @codes, $self->WARNING );
130 0 push( @problems, join( ', ', @silent ) . ' did not answer for ' . $self->opt('query') );
131 }
132 } ## end if ( defined( $self->opt('query') ) && defined...)
133  
134 6 ▶ my $message = $file . ', nameservers ' . ( defined( $nameservers[0] ) ? join( ', ', @nameservers ) : 'none' );
Branch: 1 branch
BranchTrueFalseTotal
defined $nameservers[0] ? : 5 1 6
135 6 ▶ $message = $message . ', ' . join( ', ', @problems ) if defined( $problems[0] );
Branch: 1 branch
BranchTrueFalseTotal
if defined $problems[0] 3 3 6
136  
137 6 return $self->finish( $self->worst(@codes), $message );
138 } ## end sub check
139  
140 3 1;
« blib/lib/Monitoring/Sneck/Checks/dns_sync.pm ↑ Summary blib/lib/Monitoring/Sneck/Checks/accounts.pm »