« blib/lib/Monitoring/Sneck/Checks/reboot_needed.pm
↑ Summary
blib/lib/Monitoring/Sneck/Checks/http_json.pm »
| 1 | package Monitoring::Sneck::Checks::rbl; | ||||||||||||||||||||||||||||
| 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::rbl - checks if a host is on DNS block lists | ||||||||||||||||||||||||||||
| 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 | rbl_mail|perl:rbl -H 192.0.2.25 -s zen.spamhaus.org -s bl.spamcop.net | ||||||||||||||||||||||||||||
| 23 | rbl_mx|perl:rbl -H mail.example.org -s zen.spamhaus.org -w 0 -c 1 | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | Looks each address of a host up on each block list given, through | ||||||||||||||||||||||||||||
| 28 | L<Net::DNS>, with L<Net::IP> for the reversed address. A hostname is | ||||||||||||||||||||||||||||
| 29 | looked up for its A and AAAA records first. | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | A listing is a A record for it. The number of listings is checked | ||||||||||||||||||||||||||||
| 32 | against the ranges, so by default any is CRITICAL. A lookup that gets | ||||||||||||||||||||||||||||
| 33 | no usable reply, such as none at all or SERVFAIL, is UNKNOWN, unless | ||||||||||||||||||||||||||||
| 34 | the listings are worse. | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | =head1 OPTIONS | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | - -H, --hostname :: The IP or host. Required. | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | - -s, --rbl :: A block list zone, such as zen.spamhaus.org. May be given | ||||||||||||||||||||||||||||
| 41 | more than once. Required. | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | - --server :: A DNS server to ask. May be given more than once. The | ||||||||||||||||||||||||||||
| 44 | system's if not given. | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | - -w, --warning :: Range for the number of listings. | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | - -c, --critical :: Range for the number of listings. | ||||||||||||||||||||||||||||
| 49 | Default :: 0 | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | =cut | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 7 | ▶ | sub option_spec { return ( 'hostname|H=s', 'rbl|s=s@', 'server=s@', 'warning|w=s', 'critical|c=s' ) } | ||||||||||||||||||||||||||
|
Subroutine
option_spec: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 7 | ▶ | sub defaults { return { critical => '0' } } | ||||||||||||||||||||||||||
|
Subroutine
defaults: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 7 | ▶ | sub required { return ( 'hostname', 'rbl' ) } | ||||||||||||||||||||||||||
|
Subroutine
required: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 7 | ▶ | sub ranges { return ( 'warning', 'critical' ) } | ||||||||||||||||||||||||||
|
Subroutine
ranges: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 7 | ▶ | sub validate { | ||||||||||||||||||||||||||
|
Subroutine
validate: called CC=6 SCAR=18.8
Pod
documented
| |||||||||||||||||||||||||||||
| 62 | 7 | my ( $self, $options ) = @_; | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 7 | my @problems; | |||||||||||||||||||||||||||
| 65 | push( @problems, '--hostname "' . $options->{hostname} . '" is not a IP or hostname' ) | ||||||||||||||||||||||||||||
| 66 | 7 | ▶ | if defined( $options->{hostname} ) && !$self->is_host( $options->{hostname} ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $options->{"hostname"} and not $self->is_host($options->{"hostname"})
MC/DC0%0/2
defined $options->{"hostname"} and not $self->is_host($options->{"hostname"})
defined $options->{"hostname"}
not $self->is_host($options->{"hostname"})
Truth table33%1/3
A and not(B)
A= defined $options->{"hostname"}B= not $self->is_host($options->{"hostname"})
| |||||||||||||||||||||||||||||
| 67 | 7 | ▶ | foreach my $zone ( @{ $options->{rbl} // [] } ) { | ||||||||||||||||||||||||||
|
Condition100%2/2
$options->{"rbl"} // []
MC/DC100%1/1
$options->{"rbl"} // []
$options->{"rbl"}
Truth table100%2/2
A
A= $options->{"rbl"}
| |||||||||||||||||||||||||||||
| 68 | 12 | ▶ | push( @problems, '--rbl "' . $zone . '" is not a zone' ) if !$self->is_host($zone); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 7 | return @problems; | |||||||||||||||||||||||||||
| 72 | } ## end sub validate | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | =head1 METHODS | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | =head2 check | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | Does the check. | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | =cut | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 6 | ▶ | sub check { | ||||||||||||||||||||||||||
|
Subroutine
check: called CC=14 SCAR=26.4
Pod
documented
| |||||||||||||||||||||||||||||
| 83 | 6 | my ($self) = @_; | |||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 6 | require Net::IP; | |||||||||||||||||||||||||||
| 86 | 6 | require Data::Validate::IP; | |||||||||||||||||||||||||||
| 87 | 6 | my $resolver = $self->resolver( servers => $self->opt('server') ); | |||||||||||||||||||||||||||
| 88 | 6 | my $host = $self->opt('hostname'); | |||||||||||||||||||||||||||
| 89 | 6 | my @addresses; | |||||||||||||||||||||||||||
| 90 | 6 | ▶ | if ( Data::Validate::IP::is_ip($host) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 4 | @addresses = ($host); | |||||||||||||||||||||||||||
| 92 | } else { | ||||||||||||||||||||||||||||
| 93 | 2 | foreach my $type ( 'A', 'AAAA' ) { | |||||||||||||||||||||||||||
| 94 | 4 | my $reply = $resolver->send( $host, $type ); | |||||||||||||||||||||||||||
| 95 | 4 | ▶ | next if !defined($reply); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | push( @addresses, | ||||||||||||||||||||||||||||
| 97 | 2 | ▶ | map { $type eq 'AAAA' ? $_->address_short : $_->address } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | 4 | grep { $_->type eq $type } $reply->answer ); | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | 2 | ▶ | if ( !defined( $addresses[0] ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 1 | return $self->finish( $self->UNKNOWN, 'could not look up ' . $host ); | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | } ## end else [ if ( Data::Validate::IP::is_ip($host) ) ] | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 5 | my @listed; | |||||||||||||||||||||||||||
| 106 | my @failed; | ||||||||||||||||||||||||||||
| 107 | 5 | my $lookups = 0; | |||||||||||||||||||||||||||
| 108 | 5 | foreach my $address (@addresses) { | |||||||||||||||||||||||||||
| 109 | 6 | my $reversed = Net::IP->new($address)->reverse_ip; | |||||||||||||||||||||||||||
| 110 | 6 | $reversed =~ s/\.(?:in-addr|ip6)\.arpa\.?\z//; | |||||||||||||||||||||||||||
| 111 | 6 | foreach my $zone ( @{ $self->opt('rbl') } ) { | |||||||||||||||||||||||||||
| 112 | 12 | $lookups++; | |||||||||||||||||||||||||||
| 113 | 12 | my $reply = $resolver->send( $reversed . '.' . $zone, 'A' ); | |||||||||||||||||||||||||||
| 114 | 12 | ▶ | if ( !defined($reply) || $reply->header->rcode !~ /\A(?:NOERROR|NXDOMAIN)\z/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $reply and $reply->header->rcode =~ /\A(?:NOERROR|NXDOMAIN)\z/u
MC/DC50%1/2
defined $reply and $reply->header->rcode =~ /\A(?:NOERROR|NXDOMAIN)\z/u
defined $reply
$reply->header->rcode =~ /\A(?:NOERROR|NXDOMAIN)\z/u
Truth table66%2/3
A and B
A= defined $replyB= $reply->header->rcode =~ /\A(?:NOERROR|NXDOMAIN)\z/u
| |||||||||||||||||||||||||||||
| 115 | 4 | push( @failed, $address . ' on ' . $zone ); | |||||||||||||||||||||||||||
| 116 | 4 | next; | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | 8 | my @answers = map { $_->address } grep { $_->type eq 'A' } $reply->answer; | |||||||||||||||||||||||||||
| 119 | 8 | ▶ | push( @listed, $address . ' on ' . $zone . ' (' . join( ', ', @answers ) . ')' ) if defined( $answers[0] ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | } ## end foreach my $zone ( @{ $self->opt('rbl') } ) | ||||||||||||||||||||||||||||
| 121 | } ## end foreach my $address (@addresses) | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 5 | my $count = scalar(@listed); | |||||||||||||||||||||||||||
| 124 | 5 | $self->perfdata( | |||||||||||||||||||||||||||
| 125 | label => 'listed', | ||||||||||||||||||||||||||||
| 126 | value => $count, | ||||||||||||||||||||||||||||
| 127 | warning => $self->opt('warning'), | ||||||||||||||||||||||||||||
| 128 | critical => $self->opt('critical'), | ||||||||||||||||||||||||||||
| 129 | min => 0 | ||||||||||||||||||||||||||||
| 130 | ); | ||||||||||||||||||||||||||||
| 131 | 5 | my @codes = ( $self->threshold_status( $count, $self->opt('warning'), $self->opt('critical') ) ); | |||||||||||||||||||||||||||
| 132 | 5 | ▶ | push( @codes, $self->UNKNOWN ) if defined( $failed[0] ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 5 | my $message = join( ', ', @addresses ) . ' listed ' . $count . ' times in ' . $lookups . ' lookups'; | |||||||||||||||||||||||||||
| 135 | 5 | ▶ | $message = $message . ', ' . join( ', ', @listed ) if defined( $listed[0] ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | 5 | ▶ | $message = $message . ', no answer for ' . join( ', ', @failed ) if defined( $failed[0] ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 5 | return $self->finish( $self->worst(@codes), $message ); | |||||||||||||||||||||||||||
| 139 | } ## end sub check | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 3 | 1; | |||||||||||||||||||||||||||