| 1 | package Monitoring::Sneck::Checks::ping; | ||||||||||||||||||||||||||||||||
| 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::ping - checks packet loss and round trip time | ||||||||||||||||||||||||||||||||
| 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 | ping_gw|perl:ping -H 192.0.2.1 | ||||||||||||||||||||||||||||||||
| 23 | ping_www|perl:ping -H www.example.org -P tcp --port 443 -w 50,20% -c 200,60% | ||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||
| 25 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||
| 27 | Pings a host through L<Net::Ping>. ICMP needs root, which sneck often | ||||||||||||||||||||||||||||||||
| 28 | is not when run by snmpd, so --protocol tcp or udp may be needed. For | ||||||||||||||||||||||||||||||||
| 29 | tcp, a refused connection still counts as a reply, as the host answered. | ||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||
| 31 | Each ping gets the timeout split between the packets. | ||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||
| 33 | =head1 OPTIONS | ||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||
| 35 | - -H, --hostname :: The host. Required. | ||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||
| 37 | - -p, --packets :: How many to send. | ||||||||||||||||||||||||||||||||
| 38 | Default :: 5 | ||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||
| 40 | - -P, --protocol :: icmp, tcp, or udp. | ||||||||||||||||||||||||||||||||
| 41 | Default :: icmp | ||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||
| 43 | - --port :: The port for tcp or udp. | ||||||||||||||||||||||||||||||||
| 44 | Default :: Net::Ping's, 7 for echo | ||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||
| 46 | - -w, --warning :: "ms,loss%", the round trip average and packet loss | ||||||||||||||||||||||||||||||||
| 47 | at which it is WARNING. | ||||||||||||||||||||||||||||||||
| 48 | Default :: 100,20% | ||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||
| 50 | - -c, --critical :: The same, for CRITICAL. | ||||||||||||||||||||||||||||||||
| 51 | Default :: 500,60% | ||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||
| 53 | =cut | ||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||
| 55 | 8 | ▶ | sub option_spec { return ( 'hostname|H=s', 'packets|p=i', 'protocol|P=s', 'port=i', 'warning|w=s', 'critical|c=s' ) } | ||||||||||||||||||||||||||||||
|
Subroutine
option_spec: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||
| 57 | 8 | ▶ | sub defaults { return { packets => 5, protocol => 'icmp', warning => '100,20%', critical => '500,60%' } } | ||||||||||||||||||||||||||||||
|
Subroutine
defaults: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||
| 59 | 8 | ▶ | sub required { return ('hostname') } | ||||||||||||||||||||||||||||||
|
Subroutine
required: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||
| 61 | 8 | ▶ | sub validate { | ||||||||||||||||||||||||||||||
|
Subroutine
validate: called CC=7 SCAR=20.1
Pod
documented
| |||||||||||||||||||||||||||||||||
| 62 | 8 | my ( $self, $options ) = @_; | |||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||
| 64 | 8 | my @problems; | |||||||||||||||||||||||||||||||
| 65 | push( @problems, '--hostname "' . $options->{hostname} . '" is not a IP or hostname' ) | ||||||||||||||||||||||||||||||||
| 66 | 8 | ▶ | 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 | 8 | ▶ | push( @problems, '--packets must be at least 1' ) if $options->{packets} < 1; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 68 | 8 | ▶ | push( @problems, '--protocol must be icmp, tcp, or udp' ) if $options->{protocol} !~ /\A(?:icmp|tcp|udp)\z/; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 69 | 8 | foreach my $name ( 'warning', 'critical' ) { | |||||||||||||||||||||||||||||||
| 70 | push( @problems, '--' . $name . ' must be "ms,loss%", such as 100,20%' ) | ||||||||||||||||||||||||||||||||
| 71 | 16 | ▶ | if !defined( _limits( $options->{$name} ) ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||
| 74 | 8 | return @problems; | |||||||||||||||||||||||||||||||
| 75 | } ## end sub validate | ||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||
| 77 | =head1 METHODS | ||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||
| 79 | =head2 check | ||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||
| 81 | Does the check. | ||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||
| 83 | =cut | ||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||
| 85 | 6 | ▶ | sub check { | ||||||||||||||||||||||||||||||
|
Subroutine
check: called CC=15 SCAR=51.5
Pod
documented
| |||||||||||||||||||||||||||||||||
| 86 | 6 | my ($self) = @_; | |||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||
| 88 | 6 | require Net::Ping; | |||||||||||||||||||||||||||||||
| 89 | 6 | my $packets = $self->opt('packets'); | |||||||||||||||||||||||||||||||
| 90 | 6 | my $per_ping = $self->timeout / $packets; | |||||||||||||||||||||||||||||||
| 91 | 6 | ▶ | $per_ping = 0.1 if $per_ping < 0.1; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 92 | 6 | my $ping = Net::Ping->new( $self->opt('protocol'), $per_ping ); | |||||||||||||||||||||||||||||||
| 93 | 0 | $ping->hires(1); | |||||||||||||||||||||||||||||||
| 94 | 0 | ▶ | $ping->port_number( $self->opt('port') ) if defined( $self->opt('port') ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||
| 96 | 0 | my @times; | |||||||||||||||||||||||||||||||
| 97 | my $address; | ||||||||||||||||||||||||||||||||
| 98 | 0 | foreach my $count ( 1 .. $packets ) { | |||||||||||||||||||||||||||||||
| 99 | 0 | my ( $replied, $seconds, $ip ) = $ping->ping( $self->opt('hostname') ); | |||||||||||||||||||||||||||||||
| 100 | 0 | ▶ | if ( !defined($replied) ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 101 | 0 | $ping->close; | |||||||||||||||||||||||||||||||
| 102 | 0 | return $self->finish( $self->CRITICAL, 'could not resolve ' . $self->opt('hostname') ); | |||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||
| 104 | 0 | ▶ | $address = $ip if defined($ip); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 105 | 0 | ▶ | push( @times, $seconds * 1000 ) if $replied; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||
| 107 | 0 | $ping->close; | |||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||
| 109 | 0 | my $loss = sprintf( '%.0f', ( $packets - scalar(@times) ) / $packets * 100 ); | |||||||||||||||||||||||||||||||
| 110 | 0 | my ( $warning_ms, $warning_loss ) = @{ _limits( $self->opt('warning') ) }; | |||||||||||||||||||||||||||||||
| 111 | 0 | my ( $critical_ms, $critical_loss ) = @{ _limits( $self->opt('critical') ) }; | |||||||||||||||||||||||||||||||
| 112 | 0 | ▶ | my $host = $self->opt('hostname') | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $address and $address ne $self->opt("hostname")
MC/DC0%0/2
defined $address and $address ne $self->opt("hostname")
defined $address
$address ne $self->opt("hostname")
Truth table0%0/3
A and B
A= defined $addressB= $address ne $self->opt("hostname")
| |||||||||||||||||||||||||||||||||
| 113 | . ( defined($address) && $address ne $self->opt('hostname') ? ' (' . $address . ')' : '' ); | ||||||||||||||||||||||||||||||||
| 114 | 0 | $self->perfdata( | |||||||||||||||||||||||||||||||
| 115 | label => 'pl', | ||||||||||||||||||||||||||||||||
| 116 | value => $loss, | ||||||||||||||||||||||||||||||||
| 117 | uom => '%', | ||||||||||||||||||||||||||||||||
| 118 | warning => $warning_loss, | ||||||||||||||||||||||||||||||||
| 119 | critical => $critical_loss, | ||||||||||||||||||||||||||||||||
| 120 | min => 0, | ||||||||||||||||||||||||||||||||
| 121 | max => 100 | ||||||||||||||||||||||||||||||||
| 122 | ); | ||||||||||||||||||||||||||||||||
| 123 | 0 | ▶ | if ( !defined( $times[0] ) ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 124 | 0 | return $self->finish( $self->CRITICAL, $host . ', 100% packet loss' ); | |||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||
| 127 | 0 | my $total = 0; | |||||||||||||||||||||||||||||||
| 128 | 0 | $total += $_ foreach @times; | |||||||||||||||||||||||||||||||
| 129 | 0 | my $average = sprintf( '%.3f', $total / scalar(@times) ); | |||||||||||||||||||||||||||||||
| 130 | 0 | $self->perfdata( | |||||||||||||||||||||||||||||||
| 131 | label => 'rta', | ||||||||||||||||||||||||||||||||
| 132 | value => $average, | ||||||||||||||||||||||||||||||||
| 133 | uom => 'ms', | ||||||||||||||||||||||||||||||||
| 134 | warning => $warning_ms, | ||||||||||||||||||||||||||||||||
| 135 | critical => $critical_ms, | ||||||||||||||||||||||||||||||||
| 136 | min => 0 | ||||||||||||||||||||||||||||||||
| 137 | ); | ||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||
| 139 | 0 | my $code = $self->OK; | |||||||||||||||||||||||||||||||
| 140 | 0 | ▶ | if ( $loss >= $critical_loss || $average >= $critical_ms ) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/3
$loss >= $critical_loss or $average >= $critical_ms
MC/DC0%0/2
$loss >= $critical_loss or $average >= $critical_ms
$loss >= $critical_loss
$average >= $critical_ms
Truth table0%0/3
A or B
A= $loss >= $critical_lossB= $average >= $critical_ms
| |||||||||||||||||||||||||||||||||
| 141 | 0 | $code = $self->CRITICAL; | |||||||||||||||||||||||||||||||
| 142 | ▶ | } elsif ( $loss >= $warning_loss || $average >= $warning_ms ) { | |||||||||||||||||||||||||||||||
|
Condition0%0/3
$loss >= $warning_loss or $average >= $warning_ms
MC/DC0%0/2
$loss >= $warning_loss or $average >= $warning_ms
$loss >= $warning_loss
$average >= $warning_ms
Truth table0%0/3
A or B
A= $loss >= $warning_lossB= $average >= $warning_ms
| |||||||||||||||||||||||||||||||||
| 143 | 0 | $code = $self->WARNING; | |||||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||
| 146 | 0 | return $self->finish( $code, $host . ', ' . $loss . '% packet loss, rta ' . $average . ' ms' ); | |||||||||||||||||||||||||||||||
| 147 | } ## end sub check | ||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||
| 149 | # Parses a --warning or --critical limit. | ||||||||||||||||||||||||||||||||
| 150 | # | ||||||||||||||||||||||||||||||||
| 151 | # Args... | ||||||||||||||||||||||||||||||||
| 152 | # | ||||||||||||||||||||||||||||||||
| 153 | # - limit :: The text, "ms,loss%", the % being optional. | ||||||||||||||||||||||||||||||||
| 154 | # | ||||||||||||||||||||||||||||||||
| 155 | # Returns a array ref of the milliseconds and percent, or undef if it is | ||||||||||||||||||||||||||||||||
| 156 | # not valid. | ||||||||||||||||||||||||||||||||
| 157 | # | ||||||||||||||||||||||||||||||||
| 158 | # Example... | ||||||||||||||||||||||||||||||||
| 159 | # | ||||||||||||||||||||||||||||||||
| 160 | # _limits('100,20%'); | ||||||||||||||||||||||||||||||||
| 161 | # # [ 100, 20 ] | ||||||||||||||||||||||||||||||||
| 162 | 16 | ▶ | sub _limits { | ||||||||||||||||||||||||||||||
|
Subroutine
_limits: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||
| 163 | 16 | my ($limit) = @_; | |||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||
| 165 | 16 | ▶ | return undef if !defined($limit) || $limit !~ /\A([0-9]+(?:\.[0-9]+)?),([0-9]+(?:\.[0-9]+)?)%?\z/; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $limit and $limit =~ /\A([0-9]+(?:\.[0-9]+)?),([0-9]+(?:\.[0-9]+)?)%?\z/u
MC/DC50%1/2
defined $limit and $limit =~ /\A([0-9]+(?:\.[0-9]+)?),([0-9]+(?:\.[0-9]+)?)%?\z/u
defined $limit
$limit =~ /\A([0-9]+(?:\.[0-9]+)?),([0-9]+(?:\.[0-9]+)?)%?\z/u
Truth table66%2/3
A and B
A= defined $limitB= $limit =~ /\A([0-9]+(?:\.[0-9]+)?),([0-9]+(?:\.[0-9]+)?)%?\z/u
| |||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||
| 167 | 15 | return [ $1, $2 ]; | |||||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||
| 170 | 3 | 1; | |||||||||||||||||||||||||||||||