| 1 | # | ||
| 2 | # $Id$ | ||
| 3 | # | ||
| 4 | # network::freebsd::ping Brik | ||
| 5 | # | ||
| 6 | package Metabrik::Network::Freebsd::Ping; | ||
| 7 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | 1 | ▶ | use base qw(Metabrik::Shell::Command); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | |||
| 12 | sub brik_properties { | ||
| 13 | return { | ||
| 14 | revision => '$Revision$', | ||
| 15 | tags => [ qw(unstable) ], | ||
| 16 | author => 'GomoR <GomoR[at]metabrik.org>', | ||
| 17 | license => 'http://opensource.org/licenses/BSD-3-Clause', | ||
| 18 | attributes => { | ||
| 19 | try => [ qw(count) ], | ||
| 20 | timeout => [ qw(seconds) ], | ||
| 21 | }, | ||
| 22 | attributes_default => { | ||
| 23 | try => 2, | ||
| 24 | timeout => 5, | ||
| 25 | ignore_error => 0, # We need return code from ping command | ||
| 26 | }, | ||
| 27 | commands => { | ||
| 28 | is_alive => [ qw(host try|OPTIONAL timeout|OPTIONAL) ], | ||
| 29 | }, | ||
| 30 | require_binaries => { | ||
| 31 | ping => [ ], # ping is a standard userlang command in FreeBSD | ||
| 32 | }, | ||
| 33 | }; | ||
| 34 | } | ||
| 35 | |||
| 36 | # | ||
| 37 | # Sends ICMP echo-requests $try number of times or until $timeout seconds occurs. | ||
| 38 | # | ||
| 39 | sub is_alive { | ||
| 40 | my $self = shift; | ||
| 41 | my ($host, $try, $timeout) = @_; | ||
| 42 | |||
| 43 | $try ||= $self->try; | ||
| 44 | $timeout ||= $self->timeout; | ||
| 45 | $self->brik_help_run_undef_arg('is_alive', $host) or return; | ||
| 46 | |||
| 47 | my $cmd = "ping -c $try -t $timeout $host > /dev/null 2>&1"; | ||
| 48 | |||
| 49 | my $r = $self->system($cmd) or return; | ||
| 50 | if ($r == 1) { | ||
| 51 | return 1; # Host is alive | ||
| 52 | } | ||
| 53 | |||
| 54 | return 0; # Host not alive | ||
| 55 | } | ||
| 56 | |||
| 57 | 1; | ||
| 58 | |||
| 59 | __END__ | ||