« blib/lib/Monitoring/Sneck/Checks/debsecan.pm
↑ Summary
blib/lib/Monitoring/Sneck/Checks/checksums.pm »
| 1 | package Monitoring::Sneck::Checks::dns; | ||||||||||||||||||||||||||||
| 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 Time::HiRes (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | =head1 NAME | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | Monitoring::Sneck::Checks::dns - checks a DNS lookup | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | =head1 VERSION | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | Version 0.0.1 | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | =cut | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 3 | our $VERSION = '0.0.1'; | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | dns_www|perl:dns -H www.example.org -a 192.0.2.10 | ||||||||||||||||||||||||||||
| 24 | dns_auth|perl:dns -H example.org -q SOA -s 192.0.2.53 -A -w 0.5 -c 1 | ||||||||||||||||||||||||||||
| 25 | dns_mx|perl:dns -H example.org -q MX -a '10 mail.example.org' | ||||||||||||||||||||||||||||
| 26 | dmarc|perl:dns -H _dmarc.example.org -q TXT -r '^"v=DMARC1; p=(quarantine|reject)' | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | Looks up a name through L<Net::DNS>. No reply, a error such as NXDOMAIN, | ||||||||||||||||||||||||||||
| 31 | or no records of the type is CRITICAL, as is a expected answer missing. | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | =head1 OPTIONS | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | - -H, --hostname :: The name to look up. Required. | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | - -q, --type :: The record type. ANY takes whatever records come | ||||||||||||||||||||||||||||
| 38 | back, each said with its type, though many servers answer ANY with | ||||||||||||||||||||||||||||
| 39 | only a HINFO record, as RFC 8482 lets them. | ||||||||||||||||||||||||||||
| 40 | Default :: A | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | - -s, --server :: A server to ask. May be given more than once. The | ||||||||||||||||||||||||||||
| 43 | system's if not given. | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | - -a, --expect :: A answer that must be there, as the record data, such | ||||||||||||||||||||||||||||
| 46 | as 192.0.2.10 or "10 mail.example.org". May be given more than once. | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | - -r, --regex :: A regex some answer must match, as the record data, | ||||||||||||||||||||||||||||
| 49 | such as '^"v=spf1 ' for a TXT record. May be given more than once, | ||||||||||||||||||||||||||||
| 50 | each needing a match. | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | - -A, --authoritative :: The answer must be authoritative. | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | - --tcp :: Ask over TCP. | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | - -w, --warning :: Range of seconds for the lookup. | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | - -c, --critical :: Range of seconds for the lookup. | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | =cut | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 17 | ▶ | sub option_spec { | ||||||||||||||||||||||||||
|
Subroutine
option_spec: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 63 | return ( | ||||||||||||||||||||||||||||
| 64 | 17 | 'hostname|H=s', 'type|q=s', 'server|s=s@', 'expect|a=s@', 'regex|r=s@', | |||||||||||||||||||||||||||
| 65 | 'authoritative|A', 'tcp', 'warning|w=s', 'critical|c=s' | ||||||||||||||||||||||||||||
| 66 | ); | ||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 17 | ▶ | sub defaults { return { type => 'A' } } | ||||||||||||||||||||||||||
|
Subroutine
defaults: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 17 | ▶ | sub required { return ('hostname') } | ||||||||||||||||||||||||||
|
Subroutine
required: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 17 | ▶ | sub ranges { return ( 'warning', 'critical' ) } | ||||||||||||||||||||||||||
|
Subroutine
ranges: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 17 | ▶ | sub validate { | ||||||||||||||||||||||||||
|
Subroutine
validate: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||||||||||||
| 76 | 17 | my ( $self, $options ) = @_; | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 17 | my @problems; | |||||||||||||||||||||||||||
| 79 | push( @problems, '--type "' . $options->{type} . '" is not a record type' ) | ||||||||||||||||||||||||||||
| 80 | 17 | ▶ | if $options->{type} !~ /\A[A-Za-z0-9]+\z/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 17 | ▶ | foreach my $server ( @{ $options->{server} // [] } ) { | ||||||||||||||||||||||||||
|
Condition100%2/2
$options->{"server"} // []
MC/DC100%1/1
$options->{"server"} // []
$options->{"server"}
Truth table100%2/2
A
A= $options->{"server"}
| |||||||||||||||||||||||||||||
| 82 | 16 | ▶ | push( @problems, '--server "' . $server . '" is not a IP or hostname' ) if !$self->is_host($server); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | 17 | ▶ | foreach my $regex ( @{ $options->{regex} // [] } ) { | ||||||||||||||||||||||||||
|
Condition100%2/2
$options->{"regex"} // []
MC/DC100%1/1
$options->{"regex"} // []
$options->{"regex"}
Truth table100%2/2
A
A= $options->{"regex"}
| |||||||||||||||||||||||||||||
| 85 | 3 | ▶ | push( @problems, '--regex "' . $regex . '" is not a valid regex' ) if !eval { qr/$regex/; 1 }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 17 | return @problems; | |||||||||||||||||||||||||||
| 89 | } ## end sub validate | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | =head1 METHODS | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | =head2 check | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | Does the check. | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | =cut | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 14 | ▶ | sub check { | ||||||||||||||||||||||||||
|
Subroutine
check: called CC=17 SCAR=28.3
Pod
documented
| |||||||||||||||||||||||||||||
| 100 | 14 | my ($self) = @_; | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 14 | my $resolver = $self->resolver( servers => $self->opt('server'), tcp => $self->opt('tcp') ); | |||||||||||||||||||||||||||
| 103 | 14 | my $name = $self->opt('hostname'); | |||||||||||||||||||||||||||
| 104 | 14 | my $type = uc( $self->opt('type') ); | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 14 | my $start = Time::HiRes::time; | |||||||||||||||||||||||||||
| 107 | 14 | my $reply = $resolver->send( $name, $type ); | |||||||||||||||||||||||||||
| 108 | 14 | my $seconds = sprintf( '%.3f', Time::HiRes::time - $start ); | |||||||||||||||||||||||||||
| 109 | 14 | ▶ | if ( !defined($reply) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | 1 | return $self->finish( $self->CRITICAL, 'no reply for ' . $name . ' ' . $type . ', ' . $resolver->errorstring ); | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | $self->perfdata( | ||||||||||||||||||||||||||||
| 114 | 13 | label => 'time', | |||||||||||||||||||||||||||
| 115 | value => $seconds, | ||||||||||||||||||||||||||||
| 116 | uom => 's', | ||||||||||||||||||||||||||||
| 117 | warning => $self->opt('warning'), | ||||||||||||||||||||||||||||
| 118 | critical => $self->opt('critical'), | ||||||||||||||||||||||||||||
| 119 | min => 0 | ||||||||||||||||||||||||||||
| 120 | ); | ||||||||||||||||||||||||||||
| 121 | 13 | ▶ | my $from = ' from ' . ( $reply->answerfrom // 'unknown' ); | ||||||||||||||||||||||||||
|
Condition50%1/2
$reply->answerfrom // "unknown"
MC/DC0%0/1
$reply->answerfrom // "unknown"
$reply->answerfrom
Truth table50%1/2
A
A= $reply->answerfrom
| |||||||||||||||||||||||||||||
| 122 | 13 | my $rcode = $reply->header->rcode; | |||||||||||||||||||||||||||
| 123 | 13 | ▶ | if ( $rcode ne 'NOERROR' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | 2 | return $self->finish( $self->CRITICAL, $name . ' ' . $type . ' got ' . $rcode . $from ); | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | # ANY takes every record, so those are said with their types. Net::DNS wraps long ones, such as | ||||||||||||||||||||||||||||
| 128 | # DNSKEY and RRSIG, over lines, which the status line can not have, so runs of space are squashed | ||||||||||||||||||||||||||||
| 129 | 11 | ▶ | my @records = grep { $type eq 'ANY' || $_->type eq $type } $reply->answer; | ||||||||||||||||||||||||||
|
Condition100%3/3
$type eq 'ANY' || $_->type eq $type
MC/DC100%2/2
$type eq 'ANY' || $_->type eq $type
$type eq 'ANY'
$_->type eq $type
Truth table100%3/3
A || B
A= $type eq 'ANY'B= $_->type eq $type
| |||||||||||||||||||||||||||||
| 130 | 11 | my @answers = map { $_->rdstring =~ s/\s+/ /gr } @records; | |||||||||||||||||||||||||||
| 131 | 11 | ▶ | my @said = $type eq 'ANY' ? map { $_->type . ' ' . ( $_->rdstring =~ s/\s+/ /gr ) } @records : @answers; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | 11 | ▶ | if ( !defined( $answers[0] ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | 1 | return $self->finish( $self->CRITICAL, $name . ' has no ' . $type . ' records' . $from ); | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 10 | my @codes = ( $self->threshold_status( $seconds, $self->opt('warning'), $self->opt('critical') ) ); | |||||||||||||||||||||||||||
| 137 | 10 | my @problems; | |||||||||||||||||||||||||||
| 138 | 10 | ▶ | push( @problems, 'slow' ) if $codes[0] != $self->OK; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | 10 | my %have = map { _normal($_) => 1 } @answers; | |||||||||||||||||||||||||||
| 140 | 10 | ▶ | foreach my $expected ( @{ $self->opt('expect') // [] } ) { | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->opt("expect") // []
MC/DC100%1/1
$self->opt("expect") // []
$self->opt("expect")
Truth table100%2/2
A
A= $self->opt("expect")
| |||||||||||||||||||||||||||||
| 141 | 5 | ▶ | next if $have{ _normal($expected) }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | 1 | push( @codes, $self->CRITICAL ); | |||||||||||||||||||||||||||
| 143 | 1 | push( @problems, 'missing ' . $expected ); | |||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | 10 | ▶ | foreach my $regex ( @{ $self->opt('regex') // [] } ) { | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->opt("regex") // []
MC/DC100%1/1
$self->opt("regex") // []
$self->opt("regex")
Truth table100%2/2
A
A= $self->opt("regex")
| |||||||||||||||||||||||||||||
| 146 | 2 | ▶ | next if grep { $_ =~ /$regex/ } @answers; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | 1 | push( @codes, $self->CRITICAL ); | |||||||||||||||||||||||||||
| 148 | 1 | push( @problems, 'nothing matching /' . $regex . '/' ); | |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | 10 | ▶ | if ( $self->opt('authoritative') && !$reply->header->aa ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->opt("authoritative") and not $reply->header->aa
MC/DC100%2/2
$self->opt("authoritative") and not $reply->header->aa
$self->opt("authoritative")
not $reply->header->aa
Truth table100%3/3
A and not(B)
A= $self->opt("authoritative")B= not $reply->header->aa
| |||||||||||||||||||||||||||||
| 151 | 1 | push( @codes, $self->CRITICAL ); | |||||||||||||||||||||||||||
| 152 | 1 | push( @problems, 'not authoritative' ); | |||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 10 | my $message = $name . ' ' . $type . ' ' . join( ', ', @said ) . ' in ' . $seconds . ' seconds' . $from; | |||||||||||||||||||||||||||
| 156 | 10 | ▶ | $message = $message . ', ' . join( ', ', @problems ) if defined( $problems[0] ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 10 | return $self->finish( $self->worst(@codes), $message ); | |||||||||||||||||||||||||||
| 159 | } ## end sub check | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | # Normalizes record data so answers can be compared with what is | ||||||||||||||||||||||||||||
| 162 | # expected. Lower cases it, squashes runs of space, and drops trailing | ||||||||||||||||||||||||||||
| 163 | # dots on names, as Net::DNS ends names in them. | ||||||||||||||||||||||||||||
| 164 | # | ||||||||||||||||||||||||||||
| 165 | # Args... | ||||||||||||||||||||||||||||
| 166 | # | ||||||||||||||||||||||||||||
| 167 | # - data :: The record data, such as '10 mail.example.org.'. | ||||||||||||||||||||||||||||
| 168 | # | ||||||||||||||||||||||||||||
| 169 | # Returns the normalized text. | ||||||||||||||||||||||||||||
| 170 | # | ||||||||||||||||||||||||||||
| 171 | # Example... | ||||||||||||||||||||||||||||
| 172 | # | ||||||||||||||||||||||||||||
| 173 | # _normal('10 Mail.Example.org.'); | ||||||||||||||||||||||||||||
| 174 | # # '10 mail.example.org' | ||||||||||||||||||||||||||||
| 175 | 21 | ▶ | sub _normal { | ||||||||||||||||||||||||||
|
Subroutine
_normal: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 176 | 21 | my ($data) = @_; | |||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | 21 | $data = lc($data); | |||||||||||||||||||||||||||
| 179 | 21 | $data =~ s/\s+/ /g; | |||||||||||||||||||||||||||
| 180 | 21 | $data =~ s/\.(\s|\z)/$1/g; | |||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 21 | return $data; | |||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 3 | 1; | |||||||||||||||||||||||||||