| 1 | use strict; | ||
| 2 | use warnings; | ||
| 3 | package Juno::Check::FPing; | ||
| 4 | # ABSTRACT: An FPing check for Juno | ||
| 5 | $Juno::Check::FPing::VERSION = '0.010'; | ||
| 6 | use Carp; | ||
| 7 | use AnyEvent::Util 'fork_call'; | ||
| 8 | use Moo; | ||
| 9 | use MooX::Types::MooseLike::Base qw<Int>; | ||
| 10 | use namespace::sweep; | ||
| 11 | |||
| 12 | extends 'Juno::Check::RawCommand'; | ||
| 13 | |||
| 14 | has '+cmd' => ( | ||
| 15 | is => 'lazy', | ||
| 16 | builder => '_build_cmd', | ||
| 17 | ); | ||
| 18 | |||
| 19 | has count => ( | ||
| 20 | is => 'ro', | ||
| 21 | isa => Int, | ||
| 22 | default => sub {3}, | ||
| 23 | ); | ||
| 24 | |||
| 25 | sub _build_cmd { | ||
| 26 | my $self = shift; | ||
| 27 | my $count = $self->count; | ||
| 28 | return "fping -A -q -c $count \%h"; | ||
| 29 | } | ||
| 30 | |||
| 31 | sub analyze_ping_result { | ||
| 32 | my $self = shift; | ||
| 33 | my $timing = shift; | ||
| 34 | my $regex1 = qr{ | ||
| 35 | # 1.1.1.1 : xmt/rcv/%loss = 5/5/0%, min/avg/max = 235/379/602 | ||
| 36 | ^ # start | ||
| 37 | ( \d+ \. \d+ \. \d+ \. \d+ ) # host ip | ||
| 38 | \s+ : \s+ # results separator | ||
| 39 | xmt/rcv/%loss \s = \s \d+/\d+/(\d+)%, \s # loss percentage | ||
| 40 | min/avg/max \s = \s | ||
| 41 | \d+(?:\.\d+)?/(\d+(?:\.\d+)?)/\d+(?:\.\d+)? # average | ||
| 42 | $ # finish | ||
| 43 | }x; | ||
| 44 | |||
| 45 | if ( ! defined $timing or $timing eq '' ) { | ||
| 46 | return; | ||
| 47 | } | ||
| 48 | |||
| 49 | if ( $timing =~ $regex1 ) { | ||
| 50 | my ( $ip, $loss, $average ) = ( $1, $2, $3 ); | ||
| 51 | |||
| 52 | return ( $ip, $loss, $average ); | ||
| 53 | } | ||
| 54 | |||
| 55 | return 0; | ||
| 56 | } | ||
| 57 | |||
| 58 | 1; | ||
| 59 | |||
| 60 | __END__ |