2008-02-25 22:00:46 +00:00
|
|
|
package xCAT_plugin::nodestat;
|
2008-07-18 19:59:20 +00:00
|
|
|
use strict;
|
|
|
|
use warnings;
|
2008-02-25 22:00:46 +00:00
|
|
|
|
|
|
|
use Socket;
|
2008-02-25 23:48:15 +00:00
|
|
|
use IO::Handle;
|
2008-10-31 19:12:14 +00:00
|
|
|
use Getopt::Long;
|
2009-04-07 15:47:42 +00:00
|
|
|
my %nodesetstats;
|
2008-02-25 22:00:46 +00:00
|
|
|
|
|
|
|
sub handled_commands {
|
|
|
|
return {
|
|
|
|
nodestat => 'nodestat',
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2009-04-06 21:35:48 +00:00
|
|
|
sub pinghost {
|
|
|
|
my $node = shift;
|
|
|
|
my $rc = system("ping -q -n -c 1 -w 1 $node > /dev/null");
|
|
|
|
if ($rc == 0) {
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub nodesockopen {
|
|
|
|
my $node = shift;
|
|
|
|
my $port = shift;
|
|
|
|
my $socket;
|
|
|
|
my $addr = gethostbyname($node);
|
|
|
|
my $sin = sockaddr_in($port,$addr);
|
|
|
|
my $proto = getprotobyname('tcp');
|
|
|
|
socket($socket,PF_INET,SOCK_STREAM,$proto) || return 0;
|
|
|
|
connect($socket,$sin) || return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2008-02-25 22:00:46 +00:00
|
|
|
sub installer_query {
|
|
|
|
my $node = shift;
|
|
|
|
my $destport = 3001;
|
|
|
|
my $socket;
|
|
|
|
my $text = "";
|
|
|
|
my $proto = getprotobyname('tcp');
|
|
|
|
socket($socket,PF_INET,SOCK_STREAM,$proto) || return 0;
|
|
|
|
my $addr = gethostbyname($node);
|
|
|
|
my $sin = sockaddr_in($destport,$addr);
|
|
|
|
connect($socket,$sin) || return 0;
|
2008-02-25 23:48:15 +00:00
|
|
|
print $socket "stat \n";
|
|
|
|
$socket->flush;
|
2008-02-25 22:00:46 +00:00
|
|
|
while (<$socket>) {
|
|
|
|
$text.=$_;
|
|
|
|
}
|
2008-02-26 00:07:46 +00:00
|
|
|
$text =~ s/\n.*//;
|
2008-02-25 22:00:46 +00:00
|
|
|
return $text;
|
|
|
|
close($socket);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-26 14:25:58 +00:00
|
|
|
sub getstat {
|
|
|
|
my $response = shift;
|
2009-04-07 15:47:42 +00:00
|
|
|
foreach (@{$response->{node}}) {
|
|
|
|
$nodesetstats{$_->{name}->[0]} = $_->{data}->[0];
|
|
|
|
}
|
2008-02-26 14:25:58 +00:00
|
|
|
}
|
2008-02-25 22:00:46 +00:00
|
|
|
|
2008-04-15 14:37:26 +00:00
|
|
|
#-------------------------------------------------------
|
|
|
|
|
|
|
|
=head3 preprocess_request
|
|
|
|
|
|
|
|
Check and setup for hierarchy
|
|
|
|
|
|
|
|
=cut
|
|
|
|
|
|
|
|
#-------------------------------------------------------
|
|
|
|
sub preprocess_request
|
|
|
|
{
|
|
|
|
my $req = shift;
|
|
|
|
my $cb = shift;
|
|
|
|
my %sn;
|
2009-07-15 15:32:16 +00:00
|
|
|
if ($req->{_xcatpreprocessed}->[0] == 1) { return [$req]; }
|
2008-04-15 14:37:26 +00:00
|
|
|
my $nodes = $req->{node};
|
|
|
|
my $service = "xcat";
|
2008-04-18 19:38:34 +00:00
|
|
|
my @requests;
|
2008-10-31 19:12:14 +00:00
|
|
|
if ($nodes){
|
2009-05-29 11:15:19 +00:00
|
|
|
return [$req]; #For now, do not distribute, nodestat seems to lose accuracy and slow down distributed
|
2008-10-31 19:12:14 +00:00
|
|
|
# find service nodes for requested nodes
|
|
|
|
# build an individual request for each service node
|
|
|
|
my $sn = xCAT::Utils->get_ServiceNode($nodes, $service, "MN");
|
|
|
|
|
|
|
|
# build each request for each service node
|
|
|
|
foreach my $snkey (keys %$sn)
|
|
|
|
{
|
2008-04-15 14:37:26 +00:00
|
|
|
my $reqcopy = {%$req};
|
|
|
|
$reqcopy->{node} = $sn->{$snkey};
|
|
|
|
$reqcopy->{'_xcatdest'} = $snkey;
|
2009-07-15 15:32:16 +00:00
|
|
|
$reqcopy->{_xcatpreprocessed}->[0] = 1;
|
2008-04-15 14:37:26 +00:00
|
|
|
push @requests, $reqcopy;
|
|
|
|
|
2008-10-31 19:12:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{ # non node options like -h
|
|
|
|
@ARGV=();
|
2008-10-31 19:17:40 +00:00
|
|
|
my $args=$req->{arg};
|
|
|
|
if ($args) {
|
|
|
|
@ARGV = @{$args};
|
|
|
|
} else {
|
|
|
|
&usage($cb);
|
|
|
|
return(1);
|
|
|
|
}
|
2008-10-31 19:12:14 +00:00
|
|
|
# parse the options
|
|
|
|
Getopt::Long::Configure("posix_default");
|
|
|
|
Getopt::Long::Configure("no_gnu_compat");
|
|
|
|
Getopt::Long::Configure("bundling");
|
|
|
|
if (!GetOptions(
|
|
|
|
'h|help' => \$::HELP,
|
|
|
|
'v|version' => \$::VERSION))
|
|
|
|
{
|
|
|
|
&usage($cb);
|
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
if ($::HELP) {
|
|
|
|
&usage($cb);
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
if ($::VERSION) {
|
|
|
|
my $version = xCAT::Utils->Version();
|
|
|
|
my $rsp={};
|
|
|
|
$rsp->{data}->[0] = "$version";
|
|
|
|
xCAT::MsgUtils->message("I", $rsp, $cb);
|
|
|
|
return(0);
|
|
|
|
}
|
2008-04-15 14:37:26 +00:00
|
|
|
}
|
|
|
|
return \@requests;
|
2009-02-21 14:25:06 +00:00
|
|
|
}
|
|
|
|
|
2009-04-06 21:35:48 +00:00
|
|
|
sub interrogate_node { #Meant to run against confirmed up nodes
|
|
|
|
my $node=shift;
|
|
|
|
my $doreq=shift;
|
|
|
|
my $status = "";
|
|
|
|
if (nodesockopen($node,15002)) {
|
|
|
|
$status.="pbs,"
|
|
|
|
}
|
|
|
|
if (nodesockopen($node,8002)) {
|
|
|
|
$status.="xend,"
|
|
|
|
}
|
|
|
|
if (nodesockopen($node,22)) {
|
|
|
|
$status.="sshd,"
|
|
|
|
}
|
|
|
|
$status =~ s/,$//;
|
|
|
|
if ($status) {
|
|
|
|
return $status;
|
|
|
|
}
|
|
|
|
if ($status = installer_query($node)) {
|
|
|
|
return $status;
|
|
|
|
} else { #pingable, but no *clue* as to what the state may be
|
|
|
|
$doreq->({command=>['nodeset'],
|
|
|
|
node=>[$node],
|
|
|
|
arg=>['stat']},
|
|
|
|
\&getstat);
|
2009-04-07 15:47:42 +00:00
|
|
|
return 'ping '.$nodesetstats{$node};
|
2009-04-06 21:35:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub process_request_nmap {
|
2008-02-25 22:00:46 +00:00
|
|
|
my $request = shift;
|
|
|
|
my $callback = shift;
|
2008-02-26 14:25:58 +00:00
|
|
|
my $doreq = shift;
|
2009-04-06 21:35:48 +00:00
|
|
|
my %portservices = (
|
2009-04-07 15:09:13 +00:00
|
|
|
'22' => 'sshd',
|
2009-04-04 01:00:38 +00:00
|
|
|
'15002' => 'pbs',
|
|
|
|
'8002' => 'xend',
|
|
|
|
);
|
|
|
|
my @livenodes;
|
2008-02-25 22:00:46 +00:00
|
|
|
my @nodes = @{$request->{node}};
|
2009-02-21 14:25:06 +00:00
|
|
|
my %unknownnodes;
|
|
|
|
foreach (@nodes) {
|
|
|
|
$unknownnodes{$_}=1;
|
2009-04-01 06:52:40 +00:00
|
|
|
my $packed_ip = undef;
|
|
|
|
$packed_ip = gethostbyname($_);
|
|
|
|
if( !defined $packed_ip) {
|
|
|
|
my %rsp;
|
|
|
|
$rsp{name}=[$_];
|
2009-04-04 01:00:38 +00:00
|
|
|
$rsp{data} = [ "Please make sure $_ exists in /etc/hosts or DNS" ];
|
2009-04-01 06:52:40 +00:00
|
|
|
$callback->({node=>[\%rsp]});
|
|
|
|
}
|
2009-02-21 14:25:06 +00:00
|
|
|
}
|
2009-04-01 06:52:40 +00:00
|
|
|
|
2008-02-25 22:00:46 +00:00
|
|
|
my $node;
|
2009-02-21 14:25:06 +00:00
|
|
|
my $fping;
|
2009-04-04 01:00:38 +00:00
|
|
|
my $ports = join ',',keys %portservices;
|
|
|
|
my %deadnodes;
|
|
|
|
foreach (@nodes) {
|
|
|
|
$deadnodes{$_}=1;
|
|
|
|
}
|
2009-04-22 19:54:54 +00:00
|
|
|
open($fping,"nmap -PE --send-ip -p $ports,3001 ".join(' ',@nodes). " 2> /dev/null|") or die("Can't start nmap: $!");
|
2009-04-04 01:00:38 +00:00
|
|
|
my $currnode='';
|
|
|
|
my $port;
|
|
|
|
my $state;
|
|
|
|
my %states;
|
|
|
|
my %rsp;
|
2009-04-07 15:09:13 +00:00
|
|
|
my $installquerypossible=0;
|
2009-04-07 15:47:42 +00:00
|
|
|
my @nodesetnodes=();
|
2009-02-21 14:25:06 +00:00
|
|
|
while (<$fping>) {
|
2009-04-04 01:00:38 +00:00
|
|
|
if (/Interesting ports on ([^ ]*) /) {
|
|
|
|
$currnode=$1;
|
2009-04-07 15:09:13 +00:00
|
|
|
$installquerypossible=0; #reset possibility indicator
|
2009-04-07 15:47:42 +00:00
|
|
|
%rsp=();
|
2009-04-04 01:00:38 +00:00
|
|
|
unless ($deadnodes{$1}) {
|
2009-07-09 18:26:56 +00:00
|
|
|
my $shortname;
|
2009-04-04 01:00:38 +00:00
|
|
|
foreach (keys %deadnodes) {
|
2009-07-09 18:26:56 +00:00
|
|
|
if (/\./) {
|
|
|
|
$shortname = $_;
|
|
|
|
$shortname =~ s/\..*//;
|
|
|
|
}
|
|
|
|
if ($currnode =~ /^$_\./ or ($shortname and $shortname eq $currnode)) {
|
2009-04-04 01:00:38 +00:00
|
|
|
$currnode = $_;
|
|
|
|
last;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
delete $deadnodes{$currnode};
|
|
|
|
} elsif ($currnode) {
|
|
|
|
if (/^MAC/) {
|
|
|
|
my $status = join ',',sort keys %states ;
|
2009-04-07 15:09:13 +00:00
|
|
|
unless ($status or ($installquerypossible and $status = installer_query($currnode))) { #pingable, but no *clue* as to what the state may be
|
2009-04-07 15:47:42 +00:00
|
|
|
push @nodesetnodes,$currnode; #Aggregate call to nodeset
|
|
|
|
next;
|
2009-04-04 01:00:38 +00:00
|
|
|
}
|
2009-04-07 15:47:42 +00:00
|
|
|
$rsp{name}=[$currnode];
|
2009-04-04 01:00:38 +00:00
|
|
|
$rsp{data} = [ $status ];
|
|
|
|
$callback->({node=>[\%rsp]});
|
|
|
|
$currnode="";
|
|
|
|
%states=();
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
if (/^PORT/) { next; }
|
|
|
|
($port,$state) = split;
|
|
|
|
if ($port =~ /^(\d*)\// and $state eq 'open') {
|
2009-04-07 15:09:13 +00:00
|
|
|
if ($1 eq "3001") {
|
|
|
|
$installquerypossible=1; #It is possible to actually query node
|
|
|
|
} else {
|
|
|
|
$states{$portservices{$1}}=1;
|
|
|
|
}
|
2009-04-04 01:00:38 +00:00
|
|
|
}
|
|
|
|
}
|
2009-02-21 14:25:06 +00:00
|
|
|
}
|
2009-04-07 15:47:42 +00:00
|
|
|
if (@nodesetnodes) {
|
|
|
|
$doreq->({command=>['nodeset'],
|
|
|
|
node=>\@nodesetnodes,
|
|
|
|
arg=>['stat']},
|
|
|
|
\&getstat);
|
|
|
|
foreach (@nodesetnodes) {
|
|
|
|
$rsp{name}=[$_];
|
|
|
|
$rsp{data} = [ "ping ".$nodesetstats{$_} ];
|
|
|
|
$callback->({node=>[\%rsp]});
|
|
|
|
}
|
|
|
|
}
|
2009-04-04 01:00:38 +00:00
|
|
|
foreach $currnode (sort keys %deadnodes) {
|
|
|
|
$rsp{name}=[$currnode];
|
2008-03-18 20:06:41 +00:00
|
|
|
$rsp{data} = [ 'noping' ];
|
|
|
|
$callback->({node=>[\%rsp]});
|
2009-04-04 01:00:38 +00:00
|
|
|
}
|
2008-02-25 22:00:46 +00:00
|
|
|
}
|
2009-04-06 21:35:48 +00:00
|
|
|
|
|
|
|
sub process_request {
|
2009-04-07 15:47:42 +00:00
|
|
|
%nodesetstats=();
|
2009-04-06 21:35:48 +00:00
|
|
|
if ( -x '/usr/bin/nmap' ) {
|
|
|
|
return process_request_nmap(@_);
|
|
|
|
}
|
|
|
|
my $request = shift;
|
|
|
|
my $callback = shift;
|
|
|
|
my $doreq = shift;
|
|
|
|
|
|
|
|
my @nodes = @{$request->{node}};
|
|
|
|
my %unknownnodes;
|
|
|
|
foreach (@nodes) {
|
|
|
|
$unknownnodes{$_}=1;
|
|
|
|
my $packed_ip = undef;
|
|
|
|
$packed_ip = gethostbyname($_);
|
|
|
|
if( !defined $packed_ip) {
|
|
|
|
my %rsp;
|
|
|
|
$rsp{name}=[$_];
|
|
|
|
$rsp{data} = [ "Please make sure $_ exists in /etc/hosts" ];
|
|
|
|
$callback->({node=>[\%rsp]});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
my $node;
|
|
|
|
my $fping;
|
|
|
|
open($fping,"fping ".join(' ',@nodes). " 2> /dev/null|") or die("Can't start fping: $!");
|
|
|
|
while (<$fping>) {
|
|
|
|
my %rsp;
|
|
|
|
my $node=$_;
|
|
|
|
$node =~ s/ .*//;
|
|
|
|
chomp $node;
|
|
|
|
if (/ is alive/) {
|
|
|
|
$rsp{name}=[$node];
|
|
|
|
$rsp{data} = [ interrogate_node($node,$doreq) ];
|
|
|
|
$callback->({node=>[\%rsp]});
|
|
|
|
} elsif (/is unreachable/) {
|
|
|
|
$rsp{name}=[$node];
|
|
|
|
$rsp{data} = [ 'noping' ];
|
|
|
|
$callback->({node=>[\%rsp]});
|
|
|
|
} elsif (/ address not found/) {
|
|
|
|
$rsp{name}=[$node];
|
|
|
|
$rsp{data} = [ 'nosuchhost' ];
|
|
|
|
$callback->({node=>[\%rsp]});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-04-07 15:47:42 +00:00
|
|
|
|
2008-10-31 19:12:14 +00:00
|
|
|
sub usage
|
|
|
|
{
|
|
|
|
my $cb=shift;
|
|
|
|
my $rsp={};
|
|
|
|
$rsp->{data}->[0]= "Usage:";
|
|
|
|
$rsp->{data}->[1]= " nodestat [noderange]";
|
|
|
|
$rsp->{data}->[2]= " nodestat [-h|--help|-v|--version]";
|
|
|
|
xCAT::MsgUtils->message("I", $rsp, $cb);
|
|
|
|
}
|
2008-02-25 22:00:46 +00:00
|
|
|
|
|
|
|
1;
|