#!/opt/vdops/bin/perl # This script reports on the status of the UPS chassis # V Who When What # --------------------------------------------------------------------------- # 1.8.0 skendric 03-30-2010 Upgrade to perl 5.10.1 # 1.7.0 skendric 12-23-2009 Remove Net::SNMP support # 1.6.4 skendric 03-21-2007 Stylistic mods # 1.6.3 skendric 11-16-2006 Replace Object Values with OIDs # 1.6.2 skendric 09-22-2006 Change variable names # 1.6.1 skendric 11-05-2005 Upgrade to new FHCRC::VDOPS module structure # 1.6.0 skendric 05-09-2005 Support Netops.pm-1.2 # 1.5.1 skendric 11-15-2004 Fiddle with report columns # 1.5.0 skendric 05-10-2004 Migrate common functions to Netops.pm # 1.4.0 skendric 04-30-2004 Enhance command-line options # 1.3.3 skendric 02-02-2004 Debugging fix # 1.3.2 skendric 11-16-2003 Use Net::Ping::External # 1.3.1 skendric 08-10-2003 Minor bug fixes # 1.3.0 skendric 03-16-2003 Tighted scoping, changed DEBUG to $debug # 1.2.0 skendric 12-14-2002 Fiddled with column widths in report # 1.1.0 skendric 11-17-2002 Added @version, improved build_target() # 1.0.0 skendric 10-30-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 FHCRC::Netops collection # # # Assumptions: # # # Tested on: # -APC 9606 Web/SNMP card, APC 961x Network Management Card, # SmartUPS, Symmetra, Silcon # -perl-5.10.1 # -net-snmp-5.5 # # # Instructions: # -Customize the script for your site: find the 'user-configurable # variables' section and modify as appropriate # -Type "apc-chassis-status" 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 FHCRC::Netops::APCTools 1.1.1; use FHCRC::Netops::HostTools 1.0.3; use FHCRC::Netops::NetopsTools 2.0.7; use FHCRC::Netops::NetopsData 1.3.0; use FHCRC::Netops::PingTools 1.1.5; use FHCRC::Netops::SNMPTools 1.3.9; use FHCRC::Netops::Utilities 1.3.9; # Declare global variables my %diagnosticSchedule; # upsAdvTestDiagnosticSchedule my %inputLineFailCause; # upsAdvInputLineFailCause my %lastDiagnosticsDate; # upsAdvTestLastDiagnosticsDate my %numOfBattPacks; # upsAdvBatteryNumOfBattPacks my %numOfBadBattPacks; # upsAdvBatteryNumOfBadBattPacks my %outputStatus; # upsBasicOutputStatus # Define global variables $debug = 0; # 10 = Logging # 9 = Database SELECT operations # 8 = Per IP/MAC/Port processing # 7 = Database INSERT/UPDATE/DELETE # 6 = Dump SNMP var # 5 = Dump snmp_packets # 4 = Grody: print big var # 3 = Verbose: print mid var # 2 = Simple: print small var # 1 = Basic: subroutine trace # 0 = Disable debugging $program_name = 'apc-chassis-status'; $usage = 'Usage: apc-chassis-status -s {yes|no} [-d {integer}] [-r] [-a | -e {expr} | -f {filename} | target1 target2 target3 ...]'; $version = '1.8.0'; # Define user-configurable variables $institution = 'Widgets International'; # Binaries $grab_hosts = '/bin/cat /etc/hosts'; # Files $report_file = "/home/netops/rpts/apc-chassis-status.txt"; # Mail stuff $report_queries = 'bsmith@widgets.com'; # Pause parameters $long = 30; $mid = 10; $short = 5; # Ping Stuff $ping_count = 3; $ping_timeout = 1; # SNMP Stuff # Optimize performance by sorting your community strings and SNMP version # list, most frequently used to the left, least frequently used to the right @mib_dir = qw(/opt/vdops/share/snmp/mibs/APC); @mib_file = qw/PowerNet-MIB/; @snmp_read_list = qw/not-secret public public/; @snmp_version_list = qw/2 1/; $snmp_port = 161; $snmp_retries = 3; $snmp_timeout = 2000000; # Syslog stuff $syslog_facility = 'local5'; $syslog_host = 'localhost'; $syslog_port = 514; $syslog_priority = 'info'; $syslog_socket = 'unix'; # Other possibilites include 'udp' and # 'stream'; depending on the flavor of Unix, # I've employed each of these # Target details @skip_name = qw/swamp/; @suffixes = qw/-ups/; # 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 compile_mibs(); # Compile MIB files build_target(); # Populate @target target_check(); # Look for errors in @target basic_info(); # Check for major errors sanity_check(); # Check for major errors 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 upsAdvBatteryNumOfBattPacks say 'Getting upsAdvBatteryNumOfBattPacks' if $debug > 2; $val = snmpGet({host => $target, oid => '.1.3.6.1.4.1.318.1.1.1.2.2.5.0'}); $val //= $DASH; $numOfBattPacks{$target} = $val; # Acquire upsAdvBatteryNumOfBadBattPacks say 'Getting upsAdvBatteryNumOfBadBattPacks' if $debug > 2; $val = snmpGet({host => $target, oid => '.1.3.6.1.4.1.318.1.1.1.2.2.6.0'}); $val //= $DASH; $val = $DASH if $val == -1; $numOfBadBattPacks{$target} = $val; # Acquire upsAdvInputLineFailCause say 'Getting upsAdvInputLineFailCause.0' if $debug > 2; $val = snmpGet({host => $target, oid => '.1.3.6.1.4.1.318.1.1.1.3.2.5.0'}); $val //= $DASH; $inputLineFailCause{$target} = $val; # Acquire upsBasicOutputStatus say 'Getting upsBasicOutputStatus.0\n' if $debug > 2; $val = snmpGet({host => $target, oid => '.1.3.6.1.4.1.318.1.1.1.4.1.1.0'}); $val //= $DASH; $outputStatus{$target} = $val; # Acquire upsAdvTestDiagnosticSchedule say 'Getting upsAdvTestDiagnosticSchedule' if $debug > 3; $val = snmpGet({host => $target, oid => '.1.3.6.1.4.1.318.1.1.1.7.2.1.0'}); $val //= $DASH; $diagnosticSchedule{$target} = $val; # Acquire upsAdvTestLastDiagnosticsDate say 'Getting upsAdvTestLastDiagnosticsDate' if $debug > 3; $val = snmpGet({host => $target, oid => '.1.3.6.1.4.1.318.1.1.1.7.2.4.0'}); $val //= $DASH; $lastDiagnosticsDate{$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 $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\n"); return 1; } # Debug trace trace_location('begin') if $debug; # 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} <