#!/opt/vdops/bin/perl # This script reports on the status of UPS batteries # V Who When What # --------------------------------------------------------------------------- # 1.9.0 skendric 02-21-2011 Upgrade to Netops 1.4.0 # 1.8.1 skendric 06-25-2010 Allow $error{$target} to be undef # 1.8.0 skendric 12-23-2009 Remove Net::SNMP support # 1.7.6 skendric 03-03-2009 Handle invalid temperature gracefully # 1.7.5 skendric 04-09-2007 Support reporting in Celsius # 1.7.4 skendric 03-21-2007 Stylistic mods # 1.7.3 skendric 11-16-2006 Replace Object Values with OIDs # 1.7.2 skendric 09-22-2006 Change variable names # 1.7.1 skendric 11-05-2005 Upgrade to new WI::VDOPS module structure # 1.7.0 skendric 05-09-2005 Support Netops.pm-1.2 # 1.6.0 skendric 05-10-2004 Migrate common functions to Netops.pm # 1.5.0 skendric 04-30-2004 Enhance command-line options # 1.4.3 skendric 02-03-2004 Debugging fix # 1.4.2 skendric 11-16-2003 Use Net::Ping::External # 1.4.1 skendric 08-10-2003 Minor bug fixes # 1.4.0 skendric 03-16-2003 Tighted scoping, changed DEBUG to $debug # 1.3.0 skendric 12-13-2002 Added support for AP961x # 1.2.0 skendric 11-17-2002 Added @version, improved build_target() # 1.1.0 skendric 10-29-2002 Added upsBasicOutputStatus # 1.0.0 skendric 10-22-2002 First Version # # Author: Stuart Kendrick, sbk {put at sign here} skendric {put dot here} com # # Source: http://www.skendric.com/device # # This software is available under the GNU GENERAL PUBLIC LICENSE, see # http://www.fsf.org/licenses/gpl.html # # # This script takes the following approach: # -Parses the hosts table for a list of targets (or accepts a command- # line list) # -Queries a bunch of APC-specific variables # -Produces a report # # # Requirements: # -The target(s) must be pingable # # -The following MIB modules stashed in /opt/vdops/share/snmp/mibs, # or wherever it is that you store MIB modules: # PowerNet-MIB # # -PERL modules: the WI::Netops collection # # # Assumptions: # # # Tested on: # -APC 9606 Web/SNMP card, AP961x + AP963x Network Management Card, # SmartUPS, Symmetra, Silcon # -perl-5.12.2 # -net-snmp-5.6 # # # Instructions: # -Customize the script for your site: find the 'user-configurable # variables' section and modify as appropriate # -Type "apc-battery-status --help" to see the command-line options # -Try it out # # # # Caveats: # # # Known Bugs: # # # To do: # # Begin script # Load modules use strict; use warnings; use feature 'say'; use feature 'switch'; use Carp qw(carp cluck croak confess); use Data::Dumper; use English qw( -no_match_vars ); use Getopt::Std; use Regexp::Common; use WI::Netops::APCTools 1.2.2; use WI::Netops::HostTools 1.0.4; use WI::Netops::NetopsTools 2.2.3; use WI::Netops::NetopsData 1.4.0; use WI::Netops::PingTools 1.1.7; use WI::Netops::SNMPTools 1.5.3; use WI::Netops::Utilities 1.4.4; # Declare global variables my %bat_status; # upsBasicBatteryStatus my %calibrate; # upsAdvTestCalibrationDate my %capacity; # upsAdvBatteryCapacity my %load; # upsAdvOutputLoad my %replace; # upsAdvBatteryReplaceIndicator my %runtime; # upsAdvBatteryRunTimeRemaining my %temperature; # upsAdvBatteryTemperature my %ups_status; # upsBasicOutputStatus # Define global variables $program_name = 'apc-battery-status'; $usage = 'Usage: apc-battery-status -s {yes|no} [-d {integer}] [-r] [-a | -e {expr} | -f {filename} | target1 target2 target3 ...]'; $version = '1.9.0'; # Grab arguments getopts('ad:e:f:rs:', \%option); @target = @ARGV; # Set mode if ($option{r}) { $mode = 'report' } elsif (-t STDIN) { $mode = 'interactive' } else { $mode = 'batch' } ### Begin Main Program ############################################### { check_args(); # Check arguments read_config(); # Read Netops config file compile_mibs(); # Compile MIB files build_target(); # Populate @target target_check(); # Look for errors in @target basic_info(); # Look for errors in @target sanity_check(); # Exclude unusual cases do_the_work(); # Do the work print_report(); # Print report } ##### End Main Program ############################################### ######################################################################## # Do the work: acquire image name ######################################################################## sub do_the_work { my %arg; # Args for snmpGet my $val; # Result of snmpGet # Debug trace trace_location('begin') if $debug; # Notify operator print_it('Querying targets...'); unless ($dome) { sleep $short; return 1; } # Loop through targets for my $target (@target) { # Acquire upsBasicBatteryStatus $val = snmpGet({host => $target, oid => 'upsBasicBatteryStatus.0'}); $val //= $DASH; ($bat_status{$target} = $val) =~ s/battery//; # Acquire upsAdvTestCalibrationDate $val = snmpGet({host => $target, oid => 'upsAdvTestCalibrationDate.0'}); $val //= $DASH; $calibrate{$target} = $val; # Acquire upsAdvBatteryCapacity $val = snmpGet({host => $target, oid => 'upsAdvBatteryCapacity.0'}); $val //= $DASH; $capacity{$target} = $val; # Acquire upsAdvOutputLoad $val = snmpGet({host => $target, oid => 'upsAdvOutputLoad.0'}); $val //= $DASH; $load{$target} = $val; # Acquire upsAdvBatteryTemperature, convert to Fahrenheit $val = snmpGet({host => $target, oid => 'upsAdvBatteryTemperature.0'}); $val //= $DASH; if ($RE{num}{int}->matches($val) and $temperature_units eq 'F') { $val = convert_celsius_to_fahrenheit($val); } $temperature{$target} = $val; # Acquire upsAdvBatteryRunTimeRemaining %arg = ( host => $target, oid => 'upsAdvBatteryRunTimeRemaining.0', translate => 0, ); $val //= $DASH; $runtime{$target} = $val; # Acquire upsAdvBatteryReplaceIndicator $val = snmpGet({host => $target, oid => 'upsAdvBatteryReplaceIndicator.0'}); $val //= $DASH; given ($val) { when ('noBatteryNeedsReplacing') { $replace{$target} = 'no' } when ('batteryNeedsReplacing') { $replace{$target} = 'yes' } default { $replace{$target} = $val } } # Acquire upsBasicOutputStatus $val = snmpGet({host => $target, oid => 'upsBasicOutputStatus.0'}); $val //= $DASH; $ups_status{$target} = $val; # Entertain operator print $BANG if $mode eq 'interactive'; } # Make things look pretty say('') if $mode eq 'interactive'; # Debug trace trace_location('end') if $debug; return 1; } ######################################################################## # Tell the operator what I discovered ######################################################################## sub print_report { my $handle; my $operating_range; my $t; my $total = @target; my $now = get_now(); # If we are running in test mode, skip this routine unless ($dome) { print_it("Running in test mode, cannot print a meaningful report\n"); return 1; } # Debug trace trace_location('begin') if $debug; # Define local variables $t = $temperature_units; $operating_range = 'operating range is '; if ($t eq 'C') { $operating_range .= "0-60 $t"; } else { $operating_range .= "32-140 $t"; } # Direct output to screen or to file if ($mode eq 'interactive') { $handle = *STDOUT; } else { open $handle, '>', $report_file or die "Cannot open $report_file: $!\n"; } print {$handle} <