2008-10-21 18:23:18 +00:00
|
|
|
#!/usr/bin/env perl
|
|
|
|
# IBM(c) 2007 EPL license http://www.eclipse.org/legal/epl-v10.html
|
|
|
|
#Note, this ppping 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.
|
2009-08-11 20:29:13 +00:00
|
|
|
|
|
|
|
# This script starts with a noderange from the user, expands it using the
|
|
|
|
# xcat daemon, then sends a command to every node that can be pinged
|
|
|
|
# from the machine where it is run to ping all of the other nodes
|
|
|
|
# obtained from the user. It then prints out the result and exits.
|
2008-10-21 18:23:18 +00:00
|
|
|
BEGIN
|
|
|
|
{
|
2009-08-11 20:29:13 +00:00
|
|
|
# XCATROOT must be set for this script to work
|
2008-10-21 18:23:18 +00:00
|
|
|
$::XCATROOT = $ENV{'XCATROOT'} ? $ENV{'XCATROOT'} : '/opt/xcat';
|
|
|
|
}
|
2009-09-17 18:23:42 +00:00
|
|
|
use strict;
|
2008-10-21 18:23:18 +00:00
|
|
|
use lib "$::XCATROOT/lib/perl";
|
|
|
|
|
2009-09-17 18:23:42 +00:00
|
|
|
use xCAT::Utils;
|
|
|
|
use POSIX qw(:signal_h :errno_h :sys_wait_h);
|
2008-10-21 18:23:18 +00:00
|
|
|
use IO::Socket::SSL;
|
|
|
|
use XML::Simple;
|
2009-08-11 20:29:13 +00:00
|
|
|
if ($^O =~ /^linux/i) {
|
|
|
|
$XML::Simple::PREFERRED_PARSER='XML::Parser';
|
|
|
|
}
|
2011-04-11 16:17:08 +00:00
|
|
|
#use Data::Dumper;
|
2008-10-21 18:23:18 +00:00
|
|
|
use IO::Handle;
|
|
|
|
use IO::Select;
|
|
|
|
use Getopt::Long;
|
|
|
|
my $interface;
|
|
|
|
|
2009-11-16 12:52:51 +00:00
|
|
|
my $USAGE="Usage: ppping [-i|--interface interfaces] [-d|--debug] [-V|--verbose] [-q|--quiet] [-s|--serial] noderange
|
2008-10-21 18:23:18 +00:00
|
|
|
ppping -h|--help
|
2009-09-17 18:23:42 +00:00
|
|
|
ppping -v|--version\n";
|
2008-10-21 18:23:18 +00:00
|
|
|
|
2009-08-11 20:29:13 +00:00
|
|
|
# Parse the options
|
2013-01-08 08:30:21 +00:00
|
|
|
require Getopt::Long;
|
|
|
|
Getopt::Long::Configure ("bundling");
|
2008-10-21 18:23:18 +00:00
|
|
|
if(!GetOptions(
|
|
|
|
'h|help' => \$::HELP,
|
2009-08-12 20:52:53 +00:00
|
|
|
'v|version' => \$::VERSION,
|
2008-10-21 18:23:18 +00:00
|
|
|
's|serial' => \$::SERIAL,
|
2009-09-17 18:23:42 +00:00
|
|
|
'd|debug' => \$::DEBUG,
|
|
|
|
'V|verbose' => \$::VERBOSE,
|
|
|
|
'H|hierarchical' => \$::HIERARCHY,
|
|
|
|
'q|quiet' => \$::QUIET,
|
2008-10-21 18:23:18 +00:00
|
|
|
'interface=s' => \$interface))
|
|
|
|
{
|
|
|
|
print "$USAGE";
|
|
|
|
exit 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($::HELP) { print "$USAGE"; exit 0}
|
|
|
|
if ($::VERSION) {print xCAT::Utils->Version() . "\n"; exit 0}
|
|
|
|
|
2009-08-11 20:29:13 +00:00
|
|
|
# A method to prefix and print debug information only if it is wanted.
|
|
|
|
sub debug {
|
2009-09-17 18:23:42 +00:00
|
|
|
my $debug_text = shift;
|
|
|
|
if($::DEBUG){
|
2009-08-11 20:29:13 +00:00
|
|
|
print("---$debug_text");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-21 18:23:18 +00:00
|
|
|
my $xcathost='localhost:3001';
|
|
|
|
if ($ENV{XCATHOST}) {
|
|
|
|
$xcathost=$ENV{XCATHOST};
|
|
|
|
}
|
|
|
|
|
|
|
|
unless (@ARGV) {
|
|
|
|
print "$USAGE";
|
|
|
|
exit 1;
|
|
|
|
}
|
2009-08-11 20:29:13 +00:00
|
|
|
|
|
|
|
## Connect to xcatd to expand a noderange
|
2008-10-21 18:23:18 +00:00
|
|
|
my $noderange = $ARGV[0];
|
2012-03-22 13:57:17 +00:00
|
|
|
my @user = getpwuid($>);
|
|
|
|
my $homedir=$user[7];
|
|
|
|
|
2008-10-21 18:23:18 +00:00
|
|
|
my $client = IO::Socket::SSL->new(
|
|
|
|
PeerAddr=>$xcathost,
|
2012-03-22 13:57:17 +00:00
|
|
|
SSL_key_file=>$homedir."/.xcat/client-cred.pem",
|
|
|
|
SSL_cert_file=>$homedir."/.xcat/client-cred.pem",
|
|
|
|
SSL_ca_file => $homedir."/.xcat/ca.pem",
|
2008-10-21 18:23:18 +00:00
|
|
|
SSL_use_cert => 1,
|
|
|
|
#SSL_verify_mode => 1,
|
|
|
|
);
|
|
|
|
die "Connection failure: $!\n" unless ($client);
|
|
|
|
my %cmdref = (command => 'noderange', noderange => $noderange);
|
2009-08-11 20:29:13 +00:00
|
|
|
# Request nodes from xcatd
|
|
|
|
debug("[start] - request nodes from xcatd\n");
|
2008-10-21 18:23:18 +00:00
|
|
|
$SIG{ALRM} = sub { die "No response getting noderange" };
|
|
|
|
alarm(15);
|
|
|
|
print $client XMLout(\%cmdref,RootName=>'xcatrequest', NoAttr=>1, KeyAttr => []);
|
|
|
|
alarm(15);
|
2009-08-11 20:29:13 +00:00
|
|
|
debug("[stop] - request nodes from xcatd\n");
|
|
|
|
|
|
|
|
## Recieve nodes from xcatd and place them in @nodes
|
2008-10-21 18:23:18 +00:00
|
|
|
my $response="";
|
|
|
|
my @nodes=();
|
2009-08-11 20:29:13 +00:00
|
|
|
debug("[start] - recieve nodes from xcatd\n");
|
2008-10-21 18:23:18 +00:00
|
|
|
while (<$client>) {
|
|
|
|
alarm(0);
|
|
|
|
$response .= $_;
|
|
|
|
if ($response =~ m/<\/xcatresponse>/) {
|
2009-09-17 18:23:42 +00:00
|
|
|
my $rsp=XMLin($response, ForceArray => ['node']);
|
2008-10-21 18:23:18 +00:00
|
|
|
$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);
|
2009-08-11 20:29:13 +00:00
|
|
|
debug("[stop] - recieve nodes from xcatd\n");
|
|
|
|
debug("\@nodes LENGTH:" . scalar @nodes . "\n");
|
|
|
|
|
2009-09-17 18:23:42 +00:00
|
|
|
unless (scalar(@nodes)) {
|
|
|
|
exit 1;
|
|
|
|
}
|
|
|
|
|
2008-10-21 18:23:18 +00:00
|
|
|
my $children = 0;
|
|
|
|
my $inputs = new IO::Select;
|
|
|
|
$SIG{CHLD} = sub { while (waitpid(-1,WNOHANG) > 0) { $children--; } };
|
|
|
|
|
2009-08-11 20:29:13 +00:00
|
|
|
## Use pping to determine which nodes are reachable
|
2008-10-21 18:23:18 +00:00
|
|
|
my @reachable_nodes=();
|
|
|
|
my @unreachable_nodes=();
|
2009-08-11 20:29:13 +00:00
|
|
|
debug("[start] - find unreachable nodes\n");
|
2009-09-17 18:23:42 +00:00
|
|
|
my $cmd = "$::XCATROOT/bin/pping ".join(',',@nodes). " --noexpand 2>&1";
|
|
|
|
debug("Running '$cmd'\n");
|
|
|
|
open (PPING, "$cmd |") or die("Cannot open ppinginternal pipe: $!");
|
2008-10-21 18:23:18 +00:00
|
|
|
while (<PPING>) {
|
|
|
|
if ($_ =~ / ping/) {
|
|
|
|
my @a_tmp=split(':', $_);
|
|
|
|
push(@reachable_nodes, $a_tmp[0]);
|
|
|
|
}
|
2009-09-17 18:23:42 +00:00
|
|
|
elsif ($_ =~ /noping/) {
|
2008-10-21 18:23:18 +00:00
|
|
|
my @a_tmp=split(':', $_);
|
|
|
|
push(@unreachable_nodes, $a_tmp[0]);
|
|
|
|
}
|
2009-09-17 18:23:42 +00:00
|
|
|
else { print $_; } # probably an error msg, so just echo it
|
2008-10-21 18:23:18 +00:00
|
|
|
}
|
|
|
|
close(PPING);
|
2009-09-17 18:23:42 +00:00
|
|
|
# this next line seems to always have a non-zero $?, even if pping succeeded...
|
|
|
|
#if ($?) { die "Error in the pping command used to first test the nodes (exit code=" . ($?>>8) . ").\n"; }
|
2009-08-11 20:29:13 +00:00
|
|
|
debug("[stop] - find unreachable nodes\n");
|
2008-10-21 18:23:18 +00:00
|
|
|
|
2009-08-11 20:29:13 +00:00
|
|
|
## Dispose of the unreachable nodes now
|
2008-10-21 18:23:18 +00:00
|
|
|
foreach(@unreachable_nodes) {
|
|
|
|
print "$_: noping\n";
|
|
|
|
}
|
|
|
|
|
2009-08-11 20:29:13 +00:00
|
|
|
## If there are nodes in @reachable nodes, use xdsh to make each reachable
|
|
|
|
## node pping2 every other node, even the unreachable ones.
|
|
|
|
debug("[start] - deal with reachable nodes\n");
|
|
|
|
debug("REACHABLE_NODES:@reachable_nodes\n");
|
2008-10-21 18:23:18 +00:00
|
|
|
|
2009-09-17 18:23:42 +00:00
|
|
|
if (@reachable_nodes == 0) { exit 1; }
|
2009-08-12 20:52:53 +00:00
|
|
|
|
2009-09-17 18:23:42 +00:00
|
|
|
my $allnodes=join(',', @nodes);
|
|
|
|
|
|
|
|
# Note: even if verbose is not set, we still do not want to set the quiet flag because we want
|
|
|
|
# to count the pings that come back, unless they request fully quiet mode.
|
|
|
|
my $quiet = "";
|
|
|
|
if($::QUIET) { $quiet = "-q "; }
|
2009-11-16 12:52:51 +00:00
|
|
|
# bypass mode for xdsh is not used any more, because it doesn't give any advantage
|
|
|
|
my $bypass = "";
|
2009-09-17 18:23:42 +00:00
|
|
|
|
|
|
|
my @interfaces;
|
|
|
|
if ($interface) { @interfaces = split(/,/, $interface); }
|
|
|
|
else { $interfaces[0] = ''; }
|
|
|
|
|
|
|
|
# Possible todo: this loop runs xdsh pping2 for each interface. An optimization could be to send all
|
|
|
|
# the interfaces to pping2 at once. But this would mix up all the ping responses and making it
|
|
|
|
# harder to tell what is going on.
|
|
|
|
foreach my $interf (@interfaces) {
|
|
|
|
my $i_string = $interf ? "-i $interf " : '';
|
2009-08-12 20:52:53 +00:00
|
|
|
|
2009-08-11 20:29:13 +00:00
|
|
|
## If the serial option was set, pping2 one reachable node at a time.
|
2008-10-21 18:23:18 +00:00
|
|
|
if ($::SERIAL) {
|
2009-08-11 20:29:13 +00:00
|
|
|
debug("SERIAL:$::SERIAL\n");
|
|
|
|
debug("[start] - xdsh to each reachable node\n");
|
2008-10-21 18:23:18 +00:00
|
|
|
foreach(@reachable_nodes) {
|
2009-09-17 18:23:42 +00:00
|
|
|
my $command = "$::XCATROOT/bin/xdsh $_ $bypass -s -e $::XCATROOT/sbin/pping2 \"$i_string$quiet$allnodes\" 2>&1";
|
|
|
|
debug("Running '$command'\n");
|
|
|
|
open (PPING2, "$command |") or die("Cannot open xdsh internal pipe: $!");
|
|
|
|
# Print out the result
|
|
|
|
my %numpings;
|
|
|
|
while (<PPING2>) {
|
|
|
|
output($_, \%numpings, $interf);
|
|
|
|
}
|
|
|
|
close(PPING2);
|
2008-10-21 18:23:18 +00:00
|
|
|
}
|
2009-08-11 20:29:13 +00:00
|
|
|
debug("[stop] - xdsh to each reachable node\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
## If the serial option was not set, pping2 all the reachable nodes simultaneously.
|
|
|
|
else {
|
2009-09-17 18:23:42 +00:00
|
|
|
debug("SERIAL not set.\n");
|
2008-10-21 18:23:18 +00:00
|
|
|
my $node_string=join(',', @reachable_nodes);
|
2009-09-17 18:23:42 +00:00
|
|
|
my $command = "$::XCATROOT/bin/xdsh $node_string $bypass -s -e $::XCATROOT/sbin/pping2 \"$i_string$quiet$allnodes\" 2>&1";
|
|
|
|
debug("Running '$command'\n");
|
|
|
|
open (PPING2, "$command |") or die("Cannot open xdsh internal pipe: $!");
|
|
|
|
# Print out the result
|
|
|
|
my %numpings;
|
2008-10-21 18:23:18 +00:00
|
|
|
while (<PPING2>) {
|
2009-09-17 18:23:42 +00:00
|
|
|
output($_, \%numpings, $interf);
|
2008-10-21 18:23:18 +00:00
|
|
|
}
|
2009-09-17 18:23:42 +00:00
|
|
|
close(PPING2);
|
2008-10-21 18:23:18 +00:00
|
|
|
}
|
|
|
|
}
|
2009-08-11 20:29:13 +00:00
|
|
|
debug("[stop] - deal with reachable nodes\n");
|
2008-10-21 18:23:18 +00:00
|
|
|
|
|
|
|
exit 0;
|
2009-09-17 18:23:42 +00:00
|
|
|
|
|
|
|
# Process the output if the xdsh pping2 commands
|
|
|
|
sub output {
|
|
|
|
my $line = shift;
|
|
|
|
my $numpings = shift;
|
|
|
|
my $interface = shift;
|
|
|
|
if ($line =~ /^\s*$/) { return; } # xdsh had a habit of adding an extra blank line in streaming mode
|
|
|
|
# In verbose mode we just echo everything. In quiet mode, the only thing that will come back
|
|
|
|
# from pping2 are the nopings, so we echo everything in that case too.
|
|
|
|
if ($::VERBOSE || $::QUIET) { print "$line"; return; }
|
|
|
|
|
|
|
|
# This is the default case in which pping2 will return both pings and nopings.
|
|
|
|
# We want to count the pings and display the nopings.
|
|
|
|
my $node;
|
|
|
|
if ( ($node) = $line =~ /^(\S+):.+:\s+ping$/) {
|
|
|
|
if (!($numpings->{$node}==-1)) {
|
|
|
|
$numpings->{$node}++;
|
|
|
|
# If we got pings for all the nodes then print success
|
|
|
|
if ($numpings->{$node} >= scalar(@nodes)) {
|
|
|
|
my $istr = $interface ? " on interface $interface" : '';
|
|
|
|
print "$node: pinged all nodes successfully$istr\n";
|
|
|
|
$numpings->{$node} = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
print "$line";
|
|
|
|
if ( ($node) = $line =~ /^(\S+?):/) { $numpings->{$node} = -1; } # something beside ping, stop counting
|
|
|
|
}
|
|
|
|
}
|