home *** CD-ROM | disk | FTP | other *** search
/ ftp.parl.clemson.edu / 2015-02-07.ftp.parl.clemson.edu.tar / ftp.parl.clemson.edu / pub / pvfs2 / orangefs-2.8.3-20110323.tar.gz / orangefs-2.8.3-20110323.tar / orangefs / examples / heartbeat / hardware-specific / apc-switched-pdu-hybrid-control.pl next >
Perl Script  |  2008-04-24  |  4KB  |  139 lines

  1. #!/usr/bin/perl 
  2.  
  3. # requires APC MIB file, which is available for download from APC's web site
  4.  
  5. # this script will first attempt to use SNMP to send power control commands,
  6. # and then fail back to using SSH
  7.  
  8. use strict;
  9. use Getopt::Long;
  10. use File::Find;
  11. use File::Path;
  12. use POSIX qw(setsid);
  13. use Socket;
  14.  
  15. my $host;
  16. my $ssh_user;
  17. my $ssh_pass; 
  18. my $snmp_user;
  19. my $snmp_pass;
  20. my $outlet;
  21. my $cmd;
  22.  
  23. my $snmp_command;
  24.  
  25. &process_args;
  26.  
  27. if($cmd eq "on")
  28. {
  29.    $snmp_command = "snmpset -v3 -a MD5 -l authNoPriv -u $snmp_user -A $snmp_pass -m \"/var/lib/filesystems/powernet387.mib\" $host PowerNet-MIB::rPDUOutletControlOutletCommand.$outlet = 1";
  30. }
  31. elsif($cmd eq "off")
  32. {
  33.    $snmp_command = "snmpset -v3 -a MD5 -l authNoPriv -u $snmp_user -A $snmp_pass -m \"/var/lib/filesystems/powernet387.mib\" $host PowerNet-MIB::rPDUOutletControlOutletCommand.$outlet = 2";
  34. }
  35. elsif($cmd eq "reboot")
  36. {
  37.    $snmp_command = "snmpset -v3 -a MD5 -l authNoPriv -u $snmp_user -A $snmp_pass -m \"/var/lib/filesystems/powernet387.mib\" $host PowerNet-MIB::rPDUOutletControlOutletCommand.$outlet = 3";
  38. }
  39. else
  40. {
  41.    die("Error: $cmd is not a valid outlet command.\n");
  42. }
  43.  
  44. # try snmp first
  45. my $snmp_output = `$snmp_command 2>&1`;
  46. if ( $? == 0 )
  47. {
  48.    exit 0;
  49. }
  50.  
  51. # fall back to ssh
  52. my $ssh_command = "/usr/bin/apc-switched-pdu-ssh-control.exp $host $ssh_user $ssh_pass $outlet $cmd";
  53.  
  54. my $ssh_output = `$ssh_command 2>&1`;
  55. if ( $? == 0 )
  56. {
  57.    exit 0;
  58. }
  59.  
  60. # if we reach this point, then neither worked
  61. print "Error: failed to contact APC unit.\n";
  62. print "SNMP output: $snmp_output";
  63. print "SSH output: $ssh_output";
  64.  
  65. exit 1;
  66.  
  67. sub process_args
  68. {
  69.    # Parse the command line options
  70.    # For a description of the command line options see &print_help
  71.    use vars qw( $opt_help $opt_host $opt_ssh_user $opt_ssh_pass $opt_snmp_user $opt_snmp_pass $opt_outlet $opt_cmd);
  72.  
  73.    Getopt::Long::Configure( "no_ignore_case", "bundling");
  74.    GetOptions( "help",
  75.                "host=s",
  76.                "ssh-user=s",
  77.                "ssh-pass=s",
  78.                "snmp-user=s",
  79.                "snmp-pass=s",
  80.                "outlet=i",
  81.                "cmd=s");
  82.  
  83.    if ($opt_help)
  84.    {
  85.       &print_help;
  86.       exit(0);
  87.    }
  88.  
  89.    if(!$opt_host || !$opt_ssh_user || !$opt_ssh_pass || 
  90.       !$opt_snmp_user || !$opt_snmp_pass || !$opt_outlet || !$opt_cmd)
  91.    {
  92.       &print_help;
  93.       die "Error: missing arguments.\n";
  94.    }
  95.  
  96.    $host = $opt_host;
  97.    $ssh_user = $opt_ssh_user;
  98.    $ssh_pass = $opt_ssh_pass; 
  99.    $snmp_user = $opt_snmp_user;
  100.    $snmp_pass = $opt_snmp_pass;
  101.    $outlet = $opt_outlet;
  102.    $cmd = $opt_cmd;
  103. }
  104.  
  105.  
  106. # --------------- print help information ------------------------------
  107. sub print_help {
  108.  
  109.    print <<EOF;
  110.  
  111. This script will control a single outlet on an APC power strip.  It will 
  112. first attempt communication via SNMP and then fall back to ssh if that fails.
  113.  
  114. usage: apc-switched-pdu-hybrid-control.pl <options>
  115.  
  116. options:
  117.    --help                   print this help and exit
  118.    --host        <STRING>   hostname of APC unit
  119.    --ssh-user    <STRING>   ssh username for APC unit
  120.    --ssh-pass    <STRING>   ssh password for APC unit
  121.    --snmp-user   <STRING>   SNMP username for APC unit
  122.    --snmp-pass   <STRING>   SNMP authentication pass phrase (MD5) for APC unit
  123.    --outlet      <INTEGER>  APC outlet to control
  124.    --cmd         <on|off|reboot> control command to send (on, off, or reboot)
  125.  
  126. EOF
  127. }
  128.  
  129. # Local variables:
  130. #  c-basic-offset: 3
  131. #  perl-indent-level: 3
  132. #  tab-width: 3
  133. #  indent-tabs-mode: nil
  134. #  shiftwidth: 3
  135. # End:
  136. #
  137. # vim: ts=3 expandtab
  138.  
  139.