added fping to all hosts
authorDobrica Pavlinusic <dpavlin@rot13.org>
Fri, 28 Aug 2009 22:29:08 +0000 (22:29 +0000)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Fri, 28 Aug 2009 22:29:08 +0000 (22:29 +0000)
lib/PXElator/amt.pm
lib/PXElator/client.pm
lib/PXElator/httpd.pm
lib/PXElator/ping.pm [new file with mode: 0644]
lib/PXElator/t/ping.t [new file with mode: 0755]

index 7dc53f9..c283072 100644 (file)
@@ -8,9 +8,9 @@ use Intel::AMT::RemoteControl;
 use Intel::AMT::NetworkAdministration;
 use Data::Dump qw/dump/;
 use Regexp::Common qw/net/;
-use Net::Ping;
 
 use html;
+use ping;
 use CouchDB;
 
 sub info {
@@ -20,9 +20,7 @@ sub info {
 
        $ip ||= $client_ip;
 
-       my $p = Net::Ping->new;
-
-       if ( ! $p->ping( $ip, 0.7 ) ) {
+       if ( ! ping::host( $ip ) ) {
                return "$ip unreachable";
        }
 
index a06a7e3..e826380 100644 (file)
@@ -5,12 +5,12 @@ use strict;
 use autodie;
 
 use File::Slurp;
-use Net::Ping;
 use Data::Dump qw/dump/;
 
 use server;
 use format;
 use ip;
+use ping;
 
 our $debug = $server::debug;
 
@@ -83,14 +83,12 @@ sub next_ip($) {
        my $mac = shift;
        $mac = format::mac($mac);
 
-       my $p = Net::Ping->new;
-
        my $prefix = $server::ip;
        $prefix =~ s{\.\d+$}{.};
        my $addr = $server::ip_from || die;
        my $ip = $prefix . $addr;
 
-       while ( -e ip_path($ip) || $p->ping( $ip, 0.7 ) ) {
+       while ( -e ip_path($ip) || ping::host($ip) ) {
                $ip = $prefix . $addr++;
                die "all addresses allocated!" if $addr == $server::ip_to;
                warn "skip $ip\n";
index 7387543..4c1fac2 100644 (file)
@@ -272,7 +272,11 @@ warn "XXX pids = ", dump( $daemons::pids );
 
                } else {
 
-                       my $arp = clinet::arp_mac_dev;
+                       my $arp = client::arp_mac_dev;
+                       my @ips = client::all_ips;
+
+                       my $ping;
+                       $ping = ping::fping( @ips ) if $param->{ping};
 
                        print $client ok
                                , qq|<h2>Clients on $server::ip</h2>|
@@ -282,17 +286,28 @@ warn "XXX pids = ", dump( $daemons::pids );
                                                my $ip = $_;
                                                my $conf = client::all_conf( $ip );
                                                my $mac = delete $conf->{mac} || '';
+                                               my $style;
+                                               $style
+                                                       = 'style="color:'
+                                                       . ( $ping->{$ip} ? 'green' : 'red' )
+                                                       . '"'
+                                                       if $ping;
                                                (
-                                                       qq|<a name=$ip href=/client/$ip>$ip</a>|
+                                                       qq|<a $style name=$ip href=/client/$ip>$ip</a>|
                                                        , format::mac( $mac => 'html' )
                                                        , $arp->{$mac}
                                                        , delete $conf->{hostname}
                                                        , delete $conf->{deploy}
                                                        , ( %$conf ? html::pre_dump( $conf ) : qq|<a href=/nmap?scan=$ip>nmap</a>| )
                                                );
-                                       } client::all_ips
+                                       } @ips
                                )
                                ;
+                       print $client qq|
+                               <form method=get>
+                               <input type=submit name=ping value=ping>
+                               </form>
+                       |;
                }
        } elsif ( $path =~ m{^/brctl} ) {
                print $client ok
diff --git a/lib/PXElator/ping.pm b/lib/PXElator/ping.pm
new file mode 100644 (file)
index 0000000..fb99de6
--- /dev/null
@@ -0,0 +1,42 @@
+package ping;
+
+use warnings;
+use strict;
+
+use Net::Ping;
+use Data::Dump qw/dump/;
+use Time::HiRes;
+
+sub host {
+       Net::Ping->new->ping( shift, 0.7 );
+}
+
+sub fping {
+       my $p = Net::Ping->new('syn', 0.3);
+       $p->hires;
+
+       my $status;
+       my %syn;
+
+       foreach my $host ( @_ ) {
+               my ($ret,$nslookup_duration,$ip) = $p->ping($host);
+
+               if ( $ret ) {
+                       $syn{$host} = $ip;
+#                      $status->{dns}->{$ip} = $nslookup_duration * 1000;
+               } else {
+                       push @{ $status->{address_not_found} }, $host;
+               }
+       }
+
+       while (my ($host,$rtt,$ip) = $p->ack) {
+               $status->{$ip}->{rtt} = $rtt * 1000; # ms
+       }
+
+       warn dump($status);
+
+       return $status;
+
+}
+
+1;
diff --git a/lib/PXElator/t/ping.t b/lib/PXElator/t/ping.t
new file mode 100755 (executable)
index 0000000..681ff8f
--- /dev/null
@@ -0,0 +1,14 @@
+#!/usr/bin/perl
+
+use warnings;
+use strict;
+use autodie;
+
+use Test::More tests => 3;
+
+use_ok 'ping';
+
+ok( ping::host( 'localhost' ), 'ping' );
+
+ok( ping::fping( qw/localhost 127.0.0.1 192.168.1.1 172.16.10.1/ ), 'fping' );
+