| 1 | package Net::Prober::ping; | ||
| 2 | $Net::Prober::ping::VERSION = '0.16'; | ||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use base 'Net::Prober::Probe::Base'; | ||
| 6 | |||
| 7 | sub defaults { | ||
| 8 | return { | ||
| 9 | host => undef, | ||
| 10 | port => undef, | ||
| 11 | timeout => undef, | ||
| 12 | # icmp requires root privileges, but we don't use tcp | ||
| 13 | # because it may incorrectly report hosts as down | ||
| 14 | proto => 'icmp', | ||
| 15 | size => undef, | ||
| 16 | }; | ||
| 17 | } | ||
| 18 | |||
| 19 | sub probe { | ||
| 20 | my ($self, $args) = @_; | ||
| 21 | |||
| 22 | my ($host, $port, $timeout, $proto, $size) = | ||
| 23 | $self->parse_args($args, qw(host port timeout proto size)); | ||
| 24 | |||
| 25 | #my ($host, $port, $timeout, $proto, $size) = | ||
| 26 | # @{$probe}{qw(host port timeout proto size)}; | ||
| 27 | |||
| 28 | my $pinger = Net::Ping->new($proto, $timeout); | ||
| 29 | $pinger->hires(); | ||
| 30 | |||
| 31 | if (defined $port) { | ||
| 32 | if ($proto eq 'icmp') { | ||
| 33 | Carp::croak("Ping on port $port with icmp protocol is not implemented"); | ||
| 34 | } | ||
| 35 | $pinger->port_number($port); | ||
| 36 | } | ||
| 37 | |||
| 38 | my ($ok, $elapsed, $ip) = $pinger->ping($host); | ||
| 39 | $pinger->close(); | ||
| 40 | |||
| 41 | my $result = { | ||
| 42 | ok => $ok ? 1 : 0, | ||
| 43 | time => $elapsed, | ||
| 44 | ip => $ip, | ||
| 45 | }; | ||
| 46 | |||
| 47 | return $result; | ||
| 48 | } | ||
| 49 | |||
| 50 | 1; | ||
| 51 | |||
| 52 | __END__ |