pwrcap.pl Script

Return to "How to Use the Power Management Controls on SPARC CMT Servers"


#!/usr/bin/perl
#
# This script manages the power capping parameters and activation
# for one or more systems.  A time schedule can be setup in CRON
# or the script can be ran manually from the CLI.
#
# Input:
#  -h  # Displays Help message
#  -c  # Commit the new settings
#  -d  # disable power capping
#  -e  # enable power capping
#  -f <file of SP IPs/hostnames>
#  -p <power budget in watts>
#  -s <SP IP/hostname>  # A single host to manage
#  -t <timeout in seconds>
#  -v <violation action>  none | hardpoweroff

use Getopt::Std;

$DISABLED=disabled;
$ENABLED=enabled;
$MIB_ACTIONS=" sunHwCtrlPowerMgmtBudgetPendingTimelimitActions.0 ";
$MIB_ACTIVATION=" sunHwCtrlPowerMgmtBudget.0 ";
$MIB_COMMIT=" sunHwCtrlPowerMgmtBudgetCommitPending.0 = true ";
$MIB_POLICY=" sunHwCtrlPowerMgmtPolicy.0 ";
$MIB_POWERLIMIT=" sunHwCtrlPowerMgmtBudgetPendingPowerlimit.0 ";
$MIB_TIMELIMIT=" sunHwCtrlPowerMgmtBudgetPendingTimelimit.0 ";
$SNMPGET="/usr/sfw/bin/snmpget ";
$SNMPOPTS=" -v2c -cprivate ";
$SNMPSET="/usr/sfw/bin/snmpset ";

$path=$ENV{'PATH'};
$path="$path" . ":/usr/sfw/bin";
$ENV{'PATH'}=$path;

if (! exists($ENV{'SNMPCONFPATH'}))
{
  print "No SNMPCONFPATH in ENV. Please export the SNMPCONFPATH\n";
  print "variable to point to a snmp.conf file that contains the\n";
  print "location of the MIB files.\n";
  exit(2);
}

# Get the command line options
getopts("hcdef:p:s:t:v:", \%options);

if (defined $options{'h'}) {
  print "\nUSAGE: policy.pl -hcdef<file> -p<power> -s<sp> -t<secs> -v<action>\n";
  print "\th - help. Prints this message.\n";
  print "\tc - commit pending value settings\n";
  print "\td - disable power capping\n";
  print "\te - enable power capping\n";
  print "\tf - file to contain SP IP or hostname, one per line\n";
  print "\tp - power budget value in Watts\n";
  print "\ts - SP IP or hostname\n";
  print "\tt - timeout in seconds to meet budget\n";
  print "\tv - violation action. Must be 'none' or 'hardPowerOff'\n\n";
  exit(0);
}

$dt=`date`;
chomp($dt);

# Create a stack of SP IP/hostnames to use
if (defined $options{"s"}) {
  push(@spips, $options{"s"});
}
if (defined $options{"f"}) {
  # Read the SP IP/hostnames from a file
  $rc = open(SPFILE, $options{"f"});
  if ($rc == 0) {
    print("Failed to open source SP IP/hostname file $SRC_FILE.\n");
  } else {
    while (<SPFILE>) {
      chomp;
      push(@spips, $_);
    }
  }
}

# Loop through all SPs and apply the power capping options
# from the command line
for $sp (@spips) {
  $cmd=$SNMPSET . $SNMPOPTS . $sp;
  $len1=length($cmd);
  $setopts=0;

  # Set the pending options first
  if (defined $options{"p"}) {
    $cmd=$cmd . $MIB_POWERLIMIT . "= " . $options{"p"};
    $setopts++;
  }
  if (defined $options{"v"}) {
    $cmd=$cmd . $MIB_ACTIONS . "= " . $options{"v"};
    $setopts++;
  }
  if (defined $options{"t"}) {
    $timemsec = $options{"t"} * 1000;
    $cmd=$cmd . $MIB_TIMELIMIT . "= " . $timemsec;
    $setopts++;
  }

  # Enable or disable power capping before committing the new settings
  if (defined $options{"d"}) {
    $cmd=$cmd . $MIB_ACTIVATION . "= " . $DISABLED;
  } elsif (defined $options{"e"}) {
    $cmd=$cmd . $MIB_ACTIVATION . "= " . $ENABLED;
  }

  # Commit the pending values
  if (defined $options{"c"}) {
    $cmd=$cmd . $MIB_COMMIT;
  }

  # Test to see if any options were specified, except for the SP name.
  # If not, then there is no command to execute.
  if (length($cmd) > $len1) {
    print "$dt: cmd: $cmd\n";
    # Errors come back on STDERR
    $result=`$cmd 2>&1`;

    # If multiple options are being set and one is an invalid option, then
    # none of the options will get set.
    if ($result =~ /error/i || $result =~ /unknown/i || $result =~ /fail/i ||
      $result =~ /bad/i || $result =~ /wrong/i) {
      print "$dt: FAIL: $result\n";
    } else {
      print "$dt:\n$result\n";
    }
  }
}