2007-10-26 22:44:33 +00:00
|
|
|
#!/usr/bin/env perl
|
|
|
|
# IBM(c) 2007 EPL license http://www.eclipse.org/legal/epl-v10.html
|
|
|
|
#Note, this pping still frontends fping. I think it would be possible to write a perl equivalent, but
|
|
|
|
#I've not had the time. Net::Ping shows perl code I could see being adapted for a somewhat
|
|
|
|
#asynchronous ICMP ping (the tcp syn is interesting, but far too limited, and that is currently the only async
|
|
|
|
#method Net::Ping provides.
|
2007-12-11 19:15:28 +00:00
|
|
|
BEGIN
|
|
|
|
{
|
2008-01-14 17:04:41 +00:00
|
|
|
$::XCATROOT = $ENV{'XCATROOT'} ? $ENV{'XCATROOT'} : -d '/opt/xcat' ? '/opt/xcat' : '/usr';
|
2007-12-11 19:15:28 +00:00
|
|
|
}
|
2009-09-17 18:23:42 +00:00
|
|
|
use strict;
|
2007-12-11 19:15:28 +00:00
|
|
|
use lib "$::XCATROOT/lib/perl";
|
|
|
|
|
2009-09-17 18:23:42 +00:00
|
|
|
use xCAT::Utils;
|
2012-08-22 07:36:09 +00:00
|
|
|
use xCAT::TableUtils;
|
2009-09-17 18:23:42 +00:00
|
|
|
use POSIX qw(:signal_h :errno_h :sys_wait_h);
|
2007-10-26 22:44:33 +00:00
|
|
|
use IO::Socket::SSL;
|
|
|
|
use XML::Simple;
|
2009-07-29 16:05:42 +00:00
|
|
|
$XML::Simple::PREFERRED_PARSER='XML::Parser';
|
2011-04-11 16:17:08 +00:00
|
|
|
#use Data::Dumper;
|
2007-10-26 22:44:33 +00:00
|
|
|
use IO::Handle;
|
|
|
|
use IO::Select;
|
|
|
|
use Getopt::Long;
|
2009-09-17 18:23:42 +00:00
|
|
|
|
2009-10-06 12:44:28 +00:00
|
|
|
my $USAGE="Usage: pping [-i|--interface interfaces] noderange
|
2010-04-09 20:47:05 +00:00
|
|
|
pping -f|--use_fping
|
2009-09-17 18:23:42 +00:00
|
|
|
pping -h|--help
|
|
|
|
pping -v|--version\n";
|
|
|
|
|
2007-10-26 22:44:33 +00:00
|
|
|
my $interface;
|
2013-03-05 05:29:49 +00:00
|
|
|
# Parse the options
|
|
|
|
require Getopt::Long;
|
|
|
|
Getopt::Long::Configure ("bundling");
|
2009-09-17 18:23:42 +00:00
|
|
|
if(!GetOptions(
|
2010-04-09 20:47:05 +00:00
|
|
|
'f|use_fping' => \$::USE_FPING,
|
2009-09-17 18:23:42 +00:00
|
|
|
'h|help' => \$::HELP,
|
|
|
|
'v|version' => \$::VERSION,
|
|
|
|
'X|noexpand' => \$::NOEXPAND,
|
2013-03-05 05:29:49 +00:00
|
|
|
'i|interface=s' => \$interface))
|
2009-09-17 18:23:42 +00:00
|
|
|
{
|
|
|
|
print "$USAGE";
|
|
|
|
exit 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($::HELP) { print "$USAGE"; exit 0}
|
|
|
|
if ($::VERSION) {print xCAT::Utils->Version() . "\n"; exit 0}
|
2007-10-26 22:44:33 +00:00
|
|
|
my $xcathost='localhost:3001';
|
|
|
|
if ($ENV{XCATHOST}) {
|
2008-02-09 00:45:50 +00:00
|
|
|
$xcathost=$ENV{XCATHOST};
|
2007-10-26 22:44:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unless (@ARGV) {
|
2009-09-17 18:23:42 +00:00
|
|
|
print "$USAGE";
|
2007-10-26 22:44:33 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
my $noderange = $ARGV[0];
|
2009-09-17 18:23:42 +00:00
|
|
|
my @nodes=();
|
|
|
|
|
|
|
|
if ($::NOEXPAND) { # this is when ppping is calling us and has already expanded the noderange
|
|
|
|
@nodes = split(/,/, $noderange);
|
|
|
|
}
|
|
|
|
else { # the normal case of the user running the cmd - expand the noderange using xcatd
|
|
|
|
my $client = IO::Socket::SSL->new(
|
2007-10-26 22:44:33 +00:00
|
|
|
PeerAddr=>$xcathost,
|
2009-04-22 19:54:54 +00:00
|
|
|
SSL_key_file=> xCAT::Utils->getHomeDir()."/.xcat/client-cred.pem",
|
|
|
|
SSL_cert_file=> xCAT::Utils->getHomeDir()."/.xcat/client-cred.pem",
|
|
|
|
SSL_ca_file => xCAT::Utils->getHomeDir()."/.xcat/ca.pem",
|
2007-10-26 22:44:33 +00:00
|
|
|
SSL_use_cert => 1,
|
2014-04-16 14:12:38 +00:00
|
|
|
SSL_verify_mode => 1,
|
2007-10-26 22:44:33 +00:00
|
|
|
);
|
2009-09-17 18:23:42 +00:00
|
|
|
die "Connection failure: $!\n" unless ($client);
|
|
|
|
my %cmdref = (command => 'noderange', noderange => $noderange);
|
|
|
|
$SIG{ALRM} = sub { die "No response getting noderange" };
|
|
|
|
alarm(15);
|
|
|
|
print $client XMLout(\%cmdref,RootName=>'xcatrequest', NoAttr=>1, KeyAttr => []);
|
|
|
|
alarm(15);
|
|
|
|
my $response="";
|
|
|
|
while (<$client>) {
|
|
|
|
alarm(0);
|
|
|
|
$response .= $_;
|
|
|
|
if ($response =~ m/<\/xcatresponse>/) {
|
|
|
|
my $rsp=XMLin($response, ForceArray => ['node']);
|
|
|
|
$response='';
|
|
|
|
if ($rsp->{warning}) {
|
|
|
|
printf "Warning: ".$rsp->{warning}."\n";
|
|
|
|
}
|
|
|
|
if ($rsp->{error}) {
|
|
|
|
die ("ERROR: ".$rsp->{error}."\n");
|
|
|
|
} elsif ($rsp->{node}) {
|
|
|
|
@nodes=@{$rsp->{node}};
|
|
|
|
}
|
|
|
|
if ($rsp->{serverdone}) {
|
|
|
|
last;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close($client);
|
|
|
|
} # end of else that expands the noderange using xcatd
|
|
|
|
|
|
|
|
unless (scalar(@nodes)) {
|
|
|
|
exit 1;
|
2007-10-26 22:44:33 +00:00
|
|
|
}
|
2009-09-17 18:23:42 +00:00
|
|
|
|
2009-10-06 12:44:28 +00:00
|
|
|
# I think this was only needed when we forked ping ourselves
|
|
|
|
#my $children = 0;
|
|
|
|
#my $inputs = new IO::Select;
|
|
|
|
#$SIG{CHLD} = sub { while (waitpid(-1,WNOHANG) > 0) { $children--; } };
|
|
|
|
|
|
|
|
my $usenmap = (-x '/usr/bin/nmap' or -x '/usr/local/bin/nmap');
|
2010-04-09 20:47:05 +00:00
|
|
|
if ($::USE_FPING) { # Use fping instead of nmap
|
|
|
|
$usenmap = 0;
|
|
|
|
}
|
2007-10-26 22:44:33 +00:00
|
|
|
|
2009-10-06 12:44:28 +00:00
|
|
|
my @interfaces;
|
|
|
|
if ($interface) { @interfaces = split(/,/, $interface); }
|
|
|
|
else { $interfaces[0] = ''; }
|
|
|
|
|
|
|
|
# Do the pings to the nodes for each interface in sequence. We could consider doing all the interfaces
|
|
|
|
# in parallel, but then the output would get all mixed up and be confusing for the user.
|
|
|
|
foreach my $interf (@interfaces) {
|
|
|
|
my $noderef;
|
|
|
|
if ($interf) {
|
|
|
|
# make a copy of the node list and add the interface on
|
|
|
|
$noderef = [ @nodes ];
|
2011-10-30 08:16:52 +00:00
|
|
|
foreach (@$noderef) {
|
2011-10-31 10:59:01 +00:00
|
|
|
s/-hf\d$//;
|
|
|
|
s/$/-$interf/;
|
2011-10-30 08:16:52 +00:00
|
|
|
}
|
2009-10-06 12:44:28 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
$noderef = \@nodes; # use the original node list
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($usenmap) { nmap_pping($noderef); }
|
|
|
|
else { fping_pping($noderef); }
|
2009-04-03 23:43:19 +00:00
|
|
|
}
|
2009-10-06 12:44:28 +00:00
|
|
|
|
|
|
|
sub fping_pping {
|
|
|
|
my $nodes = shift;
|
2012-08-22 07:36:09 +00:00
|
|
|
my $master = xCAT::TableUtils->get_site_Master();
|
2010-06-12 08:11:17 +00:00
|
|
|
my $masterip = xCAT::NetworkUtils->getipaddr($master);
|
|
|
|
if ($masterip =~ /:/) #IPv6, needs fping6 support
|
|
|
|
{
|
|
|
|
if (!-x '/usr/bin/fping6')
|
|
|
|
{
|
|
|
|
print "fping6 is not availabe for IPv6 ping.\n";
|
|
|
|
exit 1;
|
|
|
|
}
|
|
|
|
open (FPING, "fping6 ".join(' ',@$nodes). " 2>&1 |") or die("Cannot open fping pipe: $!");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
open (FPING, "fping ".join(' ',@$nodes). " 2>&1 |") or die("Cannot open fping pipe: $!");
|
|
|
|
}
|
2009-10-06 12:44:28 +00:00
|
|
|
while (<FPING>) {
|
|
|
|
if ($_ =~ /is unreachable/) {
|
|
|
|
s/ is unreachable/: noping/;
|
|
|
|
} elsif ($_ =~ /is alive/) {
|
|
|
|
s/ is alive/: ping/;
|
2012-01-06 08:30:34 +00:00
|
|
|
} elsif ($_ =~ /address not found/) {
|
|
|
|
s/ address not found/: noping/;
|
2009-10-06 12:44:28 +00:00
|
|
|
}
|
|
|
|
print $_;
|
|
|
|
}
|
2009-04-07 13:55:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub nmap_pping {
|
2009-10-06 12:44:28 +00:00
|
|
|
my $nodes = shift;
|
2009-04-07 13:55:26 +00:00
|
|
|
my %deadnodes;
|
2009-10-06 12:44:28 +00:00
|
|
|
foreach (@$nodes) {
|
2009-04-07 13:55:26 +00:00
|
|
|
$deadnodes{$_}=1;
|
|
|
|
}
|
2009-11-13 08:00:51 +00:00
|
|
|
open (FPING, "nmap -PE --system-dns --send-ip -sP ".join(' ',@$nodes). " 2> /dev/null|") or die("Cannot open nmap pipe: $!");
|
2009-04-07 13:55:26 +00:00
|
|
|
my $node;
|
|
|
|
while (<FPING>) {
|
|
|
|
if (/Host (.*) \(.*\) appears to be up/) {
|
|
|
|
$node=$1;
|
|
|
|
unless ($deadnodes{$node}) {
|
|
|
|
foreach (keys %deadnodes) {
|
|
|
|
if ($node =~ /^$_\./) {
|
|
|
|
$node = $_;
|
|
|
|
last;
|
|
|
|
}
|
2009-04-04 01:00:38 +00:00
|
|
|
}
|
|
|
|
}
|
2009-04-07 13:55:26 +00:00
|
|
|
delete $deadnodes{$node};
|
2011-09-19 13:46:51 +00:00
|
|
|
print "$node: ping\n" if ($node);
|
2010-09-23 18:15:48 +00:00
|
|
|
} elsif (/Nmap scan report for ([^ ]*) /) {
|
|
|
|
$node=$1;
|
|
|
|
} elsif (/Host is up./) {
|
|
|
|
unless ($deadnodes{$node}) {
|
|
|
|
foreach (keys %deadnodes) {
|
|
|
|
if ($node =~ /^$_\./) {
|
|
|
|
$node = $_;
|
|
|
|
last;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
delete $deadnodes{$node};
|
2011-09-19 13:46:51 +00:00
|
|
|
print "$node: ping\n" if ($node);
|
2009-04-04 01:00:38 +00:00
|
|
|
}
|
2009-04-07 13:55:26 +00:00
|
|
|
}
|
|
|
|
foreach (sort keys %deadnodes) {
|
|
|
|
print "$_: noping\n";
|
|
|
|
}
|
2009-04-03 23:43:19 +00:00
|
|
|
}
|