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-monitor.pl < prev    next >
Perl Script  |  2008-04-24  |  3KB  |  117 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 monitor a power strip,
  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.  
  21. &process_args;
  22.  
  23. # try snmp first
  24. my $snmp_command = "snmpstatus -v3 -a MD5 -l authNoPriv -u $snmp_user -A $snmp_pass $host";
  25.  
  26. my $snmp_output = `$snmp_command 2>&1`;
  27. if ( $? == 0 )
  28. {
  29.    # print "Success, snmp\n";
  30.    exit 0;
  31. }
  32.  
  33. # fall back to ssh
  34. my $ssh_command = "/usr/bin/apc-switched-pdu-ssh-monitor.exp $host $ssh_user $ssh_pass";
  35.  
  36. my $ssh_output = `$ssh_command 2>&1`;
  37. if ( $? == 0 )
  38. {
  39.    # print "Success, ssh\n";
  40.    exit 0;
  41. }
  42.  
  43. # if we reach this point, then neither worked
  44. print "Error: failed to contact APC unit.\n";
  45. print "SNMP output: $snmp_output";
  46. print "SSH output: $ssh_output";
  47.  
  48. exit 1;
  49.  
  50. sub process_args
  51. {
  52.    # Parse the command line options
  53.    # For a description of the command line options see &print_help
  54.    use vars qw( $opt_help $opt_host $opt_ssh_user $opt_ssh_pass $opt_snmp_user $opt_snmp_pass);
  55.  
  56.    Getopt::Long::Configure( "no_ignore_case", "bundling");
  57.    GetOptions( "help",
  58.                "host=s",
  59.                "ssh-user=s",
  60.                "ssh-pass=s",
  61.                "snmp-user=s",
  62.                "snmp-pass=s");
  63.  
  64.    if ($opt_help)
  65.    {
  66.       &print_help;
  67.       exit(0);
  68.    }
  69.  
  70.    if(!$opt_host || !$opt_ssh_user || !$opt_ssh_pass || 
  71.       !$opt_snmp_user || !$opt_snmp_pass)
  72.    {
  73.       &print_help;
  74.       die "Error: missing arguments.\n";
  75.    }
  76.  
  77.    $host = $opt_host;
  78.    $ssh_user = $opt_ssh_user;
  79.    $ssh_pass = $opt_ssh_pass; 
  80.    $snmp_user = $opt_snmp_user;
  81.    $snmp_pass = $opt_snmp_pass;
  82. }
  83.  
  84.  
  85. # --------------- print help information ------------------------------
  86. sub print_help {
  87.  
  88.    print <<EOF;
  89.  
  90. This script will check an APC power control device to see if it is 
  91. responding.  It will first attempt communication via SNMP and then fall 
  92. back to ssh if that fails.
  93.  
  94. usage: apc-switched-pdu-hybrid-monitor.pl <option>
  95.  
  96. options:
  97.    --help                   print this help and exit
  98.    --host        <STRING>   hostname of APC unit
  99.    --ssh-user    <STRING>   ssh username for APC unit
  100.    --ssh-pass    <STRING>   ssh password for APC unit
  101.    --snmp-user   <STRING>   SNMP username for APC unit
  102.    --snmp-pass   <STRING>   SNMP authentication pass phrase (MD5) for APC unit
  103.  
  104. EOF
  105. }
  106.  
  107. # Local variables:
  108. #  c-basic-offset: 3
  109. #  perl-indent-level: 3
  110. #  tab-width: 3
  111. #  indent-tabs-mode: nil
  112. #  shiftwidth: 3
  113. # End:
  114. #
  115. # vim: ts=3 expandtab
  116.  
  117.