2007-11-16 19:47:00 +00:00
|
|
|
# IBM(c) 2007 EPL license http://www.eclipse.org/legal/epl-v10.html
|
|
|
|
|
|
|
|
package xCAT::PPCpower;
|
|
|
|
use strict;
|
|
|
|
use Getopt::Long;
|
|
|
|
use xCAT::PPCcli qw(SUCCESS EXPECT_ERROR RC_ERROR NR_ERROR);
|
|
|
|
|
|
|
|
|
|
|
|
##########################################################################
|
|
|
|
# Parse the command line for options and operands
|
|
|
|
##########################################################################
|
|
|
|
sub parse_args {
|
|
|
|
|
|
|
|
my $request = shift;
|
|
|
|
my $args = $request->{arg};
|
|
|
|
my %opt = ();
|
|
|
|
my @rpower = qw(on off stat state reset boot of);
|
|
|
|
my @VERSION = qw( 2.0 );
|
|
|
|
|
|
|
|
#############################################
|
|
|
|
# Responds with usage statement
|
|
|
|
#############################################
|
|
|
|
local *usage = sub {
|
|
|
|
return( [ $_[0],
|
|
|
|
"rpower -h|--help",
|
|
|
|
"rpower -v|--version",
|
|
|
|
"rpower [-V|--verbose] noderange " . join( '|', @rpower ),
|
|
|
|
" -h writes usage information to standard output",
|
|
|
|
" -v displays command version",
|
|
|
|
" -V verbose output" ]);
|
|
|
|
};
|
|
|
|
#############################################
|
|
|
|
# Process command-line arguments
|
|
|
|
#############################################
|
|
|
|
if ( !defined( $args )) {
|
|
|
|
return(usage( "No command specified" ));
|
|
|
|
}
|
|
|
|
#############################################
|
|
|
|
# Checks case in GetOptions, allows opts
|
|
|
|
# to be grouped (e.g. -vx), and terminates
|
|
|
|
# at the first unrecognized option.
|
|
|
|
#############################################
|
|
|
|
@ARGV = @$args;
|
|
|
|
$Getopt::Long::ignorecase = 0;
|
|
|
|
Getopt::Long::Configure( "bundling" );
|
|
|
|
|
|
|
|
if ( !GetOptions( \%opt, qw(h|help V|Verbose v|version) )) {
|
|
|
|
return( usage() );
|
|
|
|
}
|
|
|
|
####################################
|
|
|
|
# Option -h for Help
|
|
|
|
####################################
|
|
|
|
if ( exists( $opt{h} )) {
|
|
|
|
return( usage() );
|
|
|
|
}
|
|
|
|
####################################
|
2008-01-11 19:34:48 +00:00
|
|
|
# Option -v for version
|
|
|
|
####################################
|
|
|
|
if ( exists( $opt{v} )) {
|
|
|
|
return( \@VERSION );
|
|
|
|
}
|
|
|
|
####################################
|
2007-11-16 19:47:00 +00:00
|
|
|
# Check for "-" with no option
|
|
|
|
####################################
|
|
|
|
if ( grep(/^-$/, @ARGV )) {
|
|
|
|
return(usage( "Missing option: -" ));
|
|
|
|
}
|
|
|
|
####################################
|
|
|
|
# Unsupported commands
|
|
|
|
####################################
|
|
|
|
my ($cmd) = grep(/^$ARGV[0]$/, @rpower );
|
|
|
|
if ( !defined( $cmd )) {
|
|
|
|
return(usage( "Invalid command: $ARGV[0]" ));
|
|
|
|
}
|
|
|
|
####################################
|
|
|
|
# Check for an extra argument
|
|
|
|
####################################
|
|
|
|
shift @ARGV;
|
|
|
|
if ( defined( $ARGV[0] )) {
|
|
|
|
return(usage( "Invalid Argument: $ARGV[0]" ));
|
|
|
|
}
|
|
|
|
####################################
|
|
|
|
# Change "stat" to "state"
|
|
|
|
####################################
|
|
|
|
$request->{op} = $cmd;
|
|
|
|
$cmd =~ s/^stat$/state/;
|
|
|
|
|
|
|
|
####################################
|
|
|
|
# Power commands special case
|
|
|
|
####################################
|
|
|
|
if ( $cmd ne "state" ) {
|
|
|
|
$cmd = ($cmd eq "boot") ? "powercmd_boot" : "powercmd";
|
|
|
|
}
|
|
|
|
$request->{method} = $cmd;
|
|
|
|
return( \%opt );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
##########################################################################
|
|
|
|
# Builds a hash of CEC/LPAR information returned from HMC/IVM
|
|
|
|
##########################################################################
|
|
|
|
sub enumerate {
|
|
|
|
|
|
|
|
my $exp = shift;
|
|
|
|
my $node = shift;
|
|
|
|
my $mtms = shift;
|
|
|
|
my %outhash = ();
|
|
|
|
my %cmds = ();
|
|
|
|
|
|
|
|
######################################
|
|
|
|
# Check for CEC/LPAR/BPAs in list
|
|
|
|
######################################
|
|
|
|
while (my ($name,$d) = each(%$node) ) {
|
2008-03-06 13:26:58 +00:00
|
|
|
my $type = @$d[4];
|
|
|
|
$cmds{$type} = ($type=~/^lpar$/) ? "state,lpar_id" : "state";
|
2007-11-16 19:47:00 +00:00
|
|
|
}
|
2008-03-06 13:26:58 +00:00
|
|
|
foreach my $type ( keys %cmds ) {
|
|
|
|
my $filter = $cmds{$type};
|
|
|
|
my $values = xCAT::PPCcli::lssyscfg( $exp, $type, $mtms, $filter );
|
2007-11-16 19:47:00 +00:00
|
|
|
my $Rc = shift(@$values);
|
|
|
|
|
|
|
|
##################################
|
|
|
|
# Return error
|
|
|
|
##################################
|
|
|
|
if ( $Rc != SUCCESS ) {
|
|
|
|
return( [$Rc,@$values[0]] );
|
|
|
|
}
|
|
|
|
##################################
|
2008-03-06 13:26:58 +00:00
|
|
|
# Save LPARs by id
|
2007-11-16 19:47:00 +00:00
|
|
|
##################################
|
|
|
|
foreach ( @$values ) {
|
2008-03-06 13:26:58 +00:00
|
|
|
my ($state,$lparid) = split /,/;
|
|
|
|
|
|
|
|
##############################
|
|
|
|
# No lparid for fsp/bpa
|
|
|
|
##############################
|
|
|
|
if ( $type =~ /^fsp|bpa$/ ) {
|
|
|
|
$lparid = $type;
|
|
|
|
}
|
|
|
|
$outhash{ $lparid } = $state;
|
2007-11-16 19:47:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return( [SUCCESS,\%outhash] );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
##########################################################################
|
|
|
|
# Performs boot operation (Off->On, On->Reset)
|
|
|
|
##########################################################################
|
|
|
|
sub powercmd_boot {
|
|
|
|
|
|
|
|
my $request = shift;
|
|
|
|
my $hash = shift;
|
|
|
|
my $exp = shift;
|
|
|
|
my @output = ();
|
|
|
|
|
|
|
|
######################################
|
|
|
|
# Power commands are grouped by CEC
|
|
|
|
# not Hardware Control Point
|
|
|
|
######################################
|
|
|
|
|
|
|
|
######################################
|
|
|
|
# Get CEC MTMS
|
|
|
|
######################################
|
|
|
|
my ($name) = keys %$hash;
|
|
|
|
my $mtms = @{$hash->{$name}}[2];
|
|
|
|
|
|
|
|
######################################
|
|
|
|
# Build CEC/LPAR information hash
|
|
|
|
######################################
|
2008-03-06 13:26:58 +00:00
|
|
|
my $stat = enumerate( $exp, $hash, $mtms );
|
2007-11-16 19:47:00 +00:00
|
|
|
my $Rc = shift(@$stat);
|
|
|
|
my $data = @$stat[0];
|
|
|
|
|
|
|
|
while (my ($name,$d) = each(%$hash) ) {
|
2008-03-06 13:26:58 +00:00
|
|
|
##################################
|
|
|
|
# Look up by lparid
|
|
|
|
##################################
|
|
|
|
my $type = @$d[4];
|
|
|
|
my $id = ($type=~/^fsp|bpa$/) ? $type : @$d[0];
|
|
|
|
|
2007-11-16 19:47:00 +00:00
|
|
|
##################################
|
|
|
|
# Output error
|
|
|
|
##################################
|
|
|
|
if ( $Rc != SUCCESS ) {
|
2008-03-05 19:10:39 +00:00
|
|
|
push @output, [$name,$data,$Rc];
|
2007-11-16 19:47:00 +00:00
|
|
|
next;
|
|
|
|
}
|
|
|
|
##################################
|
|
|
|
# Node not found
|
|
|
|
##################################
|
2008-03-06 13:26:58 +00:00
|
|
|
if ( !exists( $data->{$id} )) {
|
2008-03-05 19:10:39 +00:00
|
|
|
push @output, [$name,"Node not found",1];
|
2007-11-16 19:47:00 +00:00
|
|
|
next;
|
|
|
|
}
|
|
|
|
##################################
|
|
|
|
# Convert state to on/off
|
|
|
|
##################################
|
2008-03-06 13:26:58 +00:00
|
|
|
my $state = power_status($data->{$id});
|
2007-11-16 19:47:00 +00:00
|
|
|
my $op = ($state =~ /^Off|Not Activated$/) ? "on" : "reset";
|
|
|
|
|
|
|
|
##############################
|
|
|
|
# Send power command
|
|
|
|
##############################
|
|
|
|
my $result = xCAT::PPCcli::chsysstate(
|
|
|
|
$exp,
|
|
|
|
$op,
|
|
|
|
$d );
|
2008-03-05 19:10:39 +00:00
|
|
|
push @output, [$name,@$result[1],@$result[0]];
|
2007-11-16 19:47:00 +00:00
|
|
|
}
|
|
|
|
return( \@output );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
##########################################################################
|
|
|
|
# Performs power control operations (on,off,reboot,etc)
|
|
|
|
##########################################################################
|
|
|
|
sub powercmd {
|
|
|
|
|
|
|
|
my $request = shift;
|
|
|
|
my $hash = shift;
|
|
|
|
my $exp = shift;
|
|
|
|
my @result = ();
|
|
|
|
|
|
|
|
####################################
|
|
|
|
# Power commands are grouped by CEC
|
|
|
|
# not Hardware Control Point
|
|
|
|
####################################
|
|
|
|
|
|
|
|
while (my ($name,$d) = each(%$hash) ) {
|
|
|
|
################################
|
|
|
|
# Send command to each LPAR
|
|
|
|
################################
|
|
|
|
my $values = xCAT::PPCcli::chsysstate(
|
|
|
|
$exp,
|
|
|
|
$request->{op},
|
|
|
|
$d );
|
|
|
|
my $Rc = shift(@$values);
|
|
|
|
|
|
|
|
################################
|
|
|
|
# Return result
|
|
|
|
################################
|
2008-03-05 19:10:39 +00:00
|
|
|
push @result, [$name,@$values[0],$Rc];
|
2007-11-16 19:47:00 +00:00
|
|
|
}
|
|
|
|
return( \@result );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
##########################################################################
|
|
|
|
# Queries CEC/LPAR power status (On or Off)
|
|
|
|
##########################################################################
|
|
|
|
sub power_status {
|
|
|
|
|
|
|
|
my @states = (
|
|
|
|
"Operating",
|
|
|
|
"Running",
|
|
|
|
"Open Firmware"
|
|
|
|
);
|
|
|
|
foreach ( @states ) {
|
|
|
|
if ( /^$_[0]$/ ) {
|
|
|
|
return("on");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return("off");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
##########################################################################
|
|
|
|
# Queries CEC/LPAR power state
|
|
|
|
##########################################################################
|
|
|
|
sub state {
|
|
|
|
|
|
|
|
my $request = shift;
|
|
|
|
my $hash = shift;
|
|
|
|
my $exp = shift;
|
|
|
|
my $prefix = shift;
|
|
|
|
my $convert = shift;
|
|
|
|
my @result = ();
|
|
|
|
|
|
|
|
if ( !defined( $prefix )) {
|
|
|
|
$prefix = "";
|
|
|
|
}
|
|
|
|
while (my ($mtms,$h) = each(%$hash) ) {
|
|
|
|
######################################
|
|
|
|
# Build CEC/LPAR information hash
|
|
|
|
######################################
|
2008-03-06 13:26:58 +00:00
|
|
|
my $stat = enumerate( $exp, $h, $mtms );
|
2007-11-16 19:47:00 +00:00
|
|
|
my $Rc = shift(@$stat);
|
|
|
|
my $data = @$stat[0];
|
|
|
|
|
|
|
|
while (my ($name,$d) = each(%$h) ) {
|
2008-03-06 13:26:58 +00:00
|
|
|
##################################
|
|
|
|
# Look up by lparid
|
|
|
|
##################################
|
|
|
|
my $type = @$d[4];
|
|
|
|
my $id = ($type=~/^fsp|bpa$/) ? $type : @$d[0];
|
|
|
|
|
2007-11-16 19:47:00 +00:00
|
|
|
##################################
|
|
|
|
# Output error
|
|
|
|
##################################
|
|
|
|
if ( $Rc != SUCCESS ) {
|
2008-03-05 19:10:39 +00:00
|
|
|
push @result, [$name, "$prefix$data",$Rc];
|
2007-11-16 19:47:00 +00:00
|
|
|
next;
|
|
|
|
}
|
|
|
|
##################################
|
|
|
|
# Node not found
|
|
|
|
##################################
|
2008-03-06 13:26:58 +00:00
|
|
|
if ( !exists( $data->{$id} )) {
|
2008-03-05 19:10:39 +00:00
|
|
|
push @result, [$name, $prefix."Node not found",1];
|
2007-11-16 19:47:00 +00:00
|
|
|
next;
|
|
|
|
}
|
|
|
|
##################################
|
|
|
|
# Output value
|
|
|
|
##################################
|
2008-03-06 13:26:58 +00:00
|
|
|
my $value = $data->{$id};
|
2007-11-16 19:47:00 +00:00
|
|
|
|
|
|
|
##############################
|
|
|
|
# Convert state to on/off
|
|
|
|
##############################
|
|
|
|
if ( defined( $convert )) {
|
|
|
|
$value = power_status( $value );
|
|
|
|
}
|
2008-03-05 19:10:39 +00:00
|
|
|
push @result, [$name,"$prefix$value",$Rc];
|
2007-11-16 19:47:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return( \@result );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1;
|
2008-03-05 19:10:39 +00:00
|
|
|
|