« blib/lib/Monitoring/Sneck/Checks/listening.pm
↑ Summary
blib/lib/Monitoring/Sneck/Checks/https_cert.pm »
| 1 | package Monitoring::Sneck::Checks::hosts; | ||||||||||||||||||||||||||||||||||||||||||
| 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::hosts - checks /etc/hosts | ||||||||||||||||||||||||||||||||||||||||||
| 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 | hosts|perl:hosts | ||||||||||||||||||||||||||||||||||||||||||
| 24 | hosts_db|perl:hosts -e db.example.org=192.0.2.20 -e db=192.0.2.20 | ||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | Reads the hosts file. Lines that do not start with a IP, have no names, | ||||||||||||||||||||||||||||||||||||||||||
| 29 | or have names that are not hostnames are WARNING, as is localhost not | ||||||||||||||||||||||||||||||||||||||||||
| 30 | being in it. Not being able to read it is CRITICAL, as is a name given | ||||||||||||||||||||||||||||||||||||||||||
| 31 | via --expect not mapping to the address given. | ||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | =head1 OPTIONS | ||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | - -f, --file :: The file. | ||||||||||||||||||||||||||||||||||||||||||
| 36 | Default :: /etc/hosts | ||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||
| 38 | - -e, --expect :: "name=address", a name that must map to that | ||||||||||||||||||||||||||||||||||||||||||
| 39 | address. May be given more than once. | ||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | - --no-localhost :: Do not require localhost to be in it. | ||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||
| 43 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | 6 | ▶ | sub option_spec { return ( 'file|f=s', 'expect|e=s@', 'no-localhost' ) } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
option_spec: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | 6 | ▶ | sub defaults { return { file => '/etc/hosts' } } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
defaults: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||
| 49 | 6 | ▶ | sub validate { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
validate: called CC=6 SCAR=18.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | 6 | my ( $self, $options ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||
| 52 | 6 | require Data::Validate::IP; | |||||||||||||||||||||||||||||||||||||||||
| 53 | 6 | my @problems; | |||||||||||||||||||||||||||||||||||||||||
| 54 | 6 | ▶ | foreach my $expect ( @{ $options->{expect} // [] } ) { | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$options->{"expect"} // []
MC/DC100%1/1
$options->{"expect"} // []
$options->{"expect"}
Truth table100%2/2
A
A= $options->{"expect"}
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | 5 | my ( $name, $address ) = split( /=/, $expect, 2 ); | |||||||||||||||||||||||||||||||||||||||||
| 56 | 5 | ▶ | if ( !defined($address) || !$self->is_host($name) || !Data::Validate::IP::is_ip($address) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
defined $address and $self->is_host($name) and Data::Validate::IP::is_ip($address)
defined $address and $self->is_host($name)
MC/DC33%1/3
defined $address and $self->is_host($name) and Data::Validate::IP::is_ip($address)
defined $address
$self->is_host($name)
Data::Validate::IP::is_ip($address)
Truth table50%2/4
A and B and C
A= defined $addressB= $self->is_host($name)C= Data::Validate::IP::is_ip($address)
| |||||||||||||||||||||||||||||||||||||||||||
| 57 | 1 | push( @problems, '--expect "' . $expect . '" is not name=address' ); | |||||||||||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | 6 | return @problems; | |||||||||||||||||||||||||||||||||||||||||
| 62 | } ## end sub validate | ||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | =head2 check | ||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | Does the check. | ||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | 5 | ▶ | sub check { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
check: called CC=18 SCAR=28.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | 5 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | 5 | require Data::Validate::IP; | |||||||||||||||||||||||||||||||||||||||||
| 76 | 5 | my $file = $self->opt('file'); | |||||||||||||||||||||||||||||||||||||||||
| 77 | 5 | my $content = read_file( $file, err_mode => 'quiet' ); | |||||||||||||||||||||||||||||||||||||||||
| 78 | 5 | ▶ | if ( !defined($content) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | 1 | return $self->finish( $self->CRITICAL, 'can not read ' . $file . ', ' . $! ); | |||||||||||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | 4 | my %addresses; | |||||||||||||||||||||||||||||||||||||||||
| 83 | my @problems; | ||||||||||||||||||||||||||||||||||||||||||
| 84 | 4 | my $line_number = 0; | |||||||||||||||||||||||||||||||||||||||||
| 85 | 4 | my $entries = 0; | |||||||||||||||||||||||||||||||||||||||||
| 86 | 4 | foreach my $line ( split( /\n/, $content ) ) { | |||||||||||||||||||||||||||||||||||||||||
| 87 | 10 | $line_number++; | |||||||||||||||||||||||||||||||||||||||||
| 88 | 10 | $line =~ s/#.*\z//; | |||||||||||||||||||||||||||||||||||||||||
| 89 | 10 | $line =~ s/\A\s+|\s+\z//g; | |||||||||||||||||||||||||||||||||||||||||
| 90 | 10 | ▶ | next if $line eq ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | 10 | my ( $address, @names ) = split( /\s+/, $line ); | |||||||||||||||||||||||||||||||||||||||||
| 93 | 10 | ▶ | if ( !Data::Validate::IP::is_ip( $address =~ s/%.*\z//r ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 94 | 1 | push( @problems, 'line ' . $line_number . ' "' . $address . '" is not a IP' ); | |||||||||||||||||||||||||||||||||||||||||
| 95 | 1 | next; | |||||||||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||||||||||
| 97 | 9 | ▶ | if ( !defined( $names[0] ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 98 | 1 | push( @problems, 'line ' . $line_number . ' has no names' ); | |||||||||||||||||||||||||||||||||||||||||
| 99 | 1 | next; | |||||||||||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||||||||||
| 101 | 8 | my $good_names = 0; | |||||||||||||||||||||||||||||||||||||||||
| 102 | 8 | foreach my $name (@names) { | |||||||||||||||||||||||||||||||||||||||||
| 103 | 12 | ▶ | if ( !$self->is_host($name) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 104 | 1 | push( @problems, 'line ' . $line_number . ' "' . $name . '" is not a hostname' ); | |||||||||||||||||||||||||||||||||||||||||
| 105 | 1 | next; | |||||||||||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||||||||||||
| 107 | 11 | push( @{ $addresses{ lc($name) } }, lc($address) ); | |||||||||||||||||||||||||||||||||||||||||
| 108 | 11 | $good_names++; | |||||||||||||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||||||||||||
| 110 | 8 | ▶ | $entries++ if $good_names; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 111 | } ## end foreach my $line ( split( /\n/, $content ) ) | ||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||
| 113 | 4 | ▶ | if ( !$self->opt('no-localhost') && !defined( $addresses{localhost} ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->opt("no-localhost") or defined $addresses{"localhost"}
MC/DC100%2/2
$self->opt("no-localhost") or defined $addresses{"localhost"}
$self->opt("no-localhost")
defined $addresses{"localhost"}
Truth table100%3/3
A or B
A= $self->opt("no-localhost")B= defined $addresses{"localhost"}
| |||||||||||||||||||||||||||||||||||||||||||
| 114 | 1 | push( @problems, 'localhost is not in it' ); | |||||||||||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||||||||||||
| 116 | 4 | ▶ | my @codes = ( defined( $problems[0] ) ? $self->WARNING : $self->OK ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||
| 118 | 4 | ▶ | foreach my $expect ( @{ $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")
| |||||||||||||||||||||||||||||||||||||||||||
| 119 | 4 | my ( $name, $address ) = split( /=/, $expect, 2 ); | |||||||||||||||||||||||||||||||||||||||||
| 120 | 4 | ▶ | next if grep { $_ eq lc($address) } @{ $addresses{ lc($name) } // [] }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$addresses{lc $name} // []
MC/DC100%1/1
$addresses{lc $name} // []
$addresses{lc $name}
Truth table100%2/2
A
A= $addresses{lc $name}
| |||||||||||||||||||||||||||||||||||||||||||
| 121 | 2 | push( @codes, $self->CRITICAL ); | |||||||||||||||||||||||||||||||||||||||||
| 122 | push( @problems, | ||||||||||||||||||||||||||||||||||||||||||
| 123 | $name . ' is ' | ||||||||||||||||||||||||||||||||||||||||||
| 124 | 2 | ▶ | . ( defined( $addresses{ lc($name) } ) ? join( ', ', @{ $addresses{ lc($name) } } ) : 'not in it' ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 125 | . ', not ' | ||||||||||||||||||||||||||||||||||||||||||
| 126 | . $address ); | ||||||||||||||||||||||||||||||||||||||||||
| 127 | } ## end foreach my $expect ( @{ $self->opt('expect') //...}) | ||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||
| 129 | 4 | my $message = $file . ', ' . $entries . ' entries'; | |||||||||||||||||||||||||||||||||||||||||
| 130 | 4 | ▶ | $message = $message . ', ' . join( ', ', @problems ) if defined( $problems[0] ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||
| 132 | 4 | return $self->finish( $self->worst(@codes), $message ); | |||||||||||||||||||||||||||||||||||||||||
| 133 | } ## end sub check | ||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||
| 135 | 3 | 1; | |||||||||||||||||||||||||||||||||||||||||