#!/opt/vdops/bin/perl # This script automates the process of initiating the 'calibration' test # V Who When What # --------------------------------------------------------------------------- # 1.8.0 skendric 02-21-2011 Upgrade to Netops 1.4.0 # 1.7.1 skendric 06-25-2010 Allow $error{$target} to be undef # 1.7.0 skendric 03-30-2010 Upgrade to perl 5.10.1 # 1.6.0 skendric 12-23-2009 Remove Net::SNMP support # 1.5.4 skendric 03-21-2007 Stylistic mods # 1.5.3 skendric 11-16-2006 Replace Object Values with OIDs # 1.5.2 skendric 11-05-2005 Upgrade to new WI::VDOPS module structure # 1.5.1 skendric 07-04-2005 Fix parameters to snmpSet in do_the_work # 1.5.0 skendric 05-09-2005 Support Netops.pm-1.2 # 1.4.0 skendric 05-10-2004 Migrate common functions to Netops.pm # 1.3.0 skendric 04-30-2004 Enhance command-line options # 1.2.2 skendric 11-16-2003 Use Net::Ping::External # 1.2.1 skendric 08-10-2003 Minor bug fixes # 1.2.0 skendric 03-16-2003 Tighted scoping, changed DEBUG to $debug # 1.1.1 skendric 12-12-2002 Fixed write string bug in info_before # 1.1.0 skendric 11-17-2002 Added @version, improved build_target() # 1.0.0 skendric 10-24-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) # -Sets the 'upsAdvTestRuntimeCalibration' variable to 2 # -Pings them and reports on any which did not survive # # Requirements: # -The target(s) must be pingable # # -PERL modules: the WI::Netops collection # # # # Assumptions: # # # Tested on: # -APC 9606 Web/SNMP card, AP961x + AP963x Network Management Card, # SmartUPS, Symmetra # -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-calibrate" 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 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 %calib_date; # upsAdvTestCalibrationDate my %calib_result; # upsAdvTestCalibrationResults # 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-calibrate'; $usage = 'Usage: apc-calibrate -s {yes|no} [-d {integer}] [-r] [-a | -e {expr} | -f {filename} | target1 target2 target3 ...]'; $version = '1.8.0'; # Grab arguments getopts('ad:e:f:s:', \%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(); # Gather information sanity_check(); # Check for major errors info_before(); # Gather more information print_before(); # Tell operator what I will do do_the_work(); # Reset devices info_after(); # Gather information print_after(); # Tell the operator what I did } ##### End Main Program ############################################### ######################################################################## # Do the work: upload images ######################################################################## sub do_the_work { # Debug trace trace_location('begin') if $debug; # Notify operator print 'Initiating calibration...' if $mode eq 'interactive'; if ($dome) { say(''); } else { sleep $short; say "just kidding"; return 1; } # Loop through @target, initiating calibration for my $target (@target) { my $val; # Initiate calibration test $val = initiate_apc_calibration($target); say "Unable to start calibration for $target" unless (defined $val and $val == 1); } # Make things look pretty say('') if $mode eq 'interactive'; # Debug trace trace_location('end') if $debug; return 1; } ######################################################################## # Gather information after calibration initiation ######################################################################## sub info_after { # Debug trace trace_location('begin') if $debug; # Notify operator print_it('Pinging targets...'); sleep $short; # Loop through targets, pinging TARGET: for my $ups (@target) { my $val; # If we can't ping the target, skip to the next one unless (ping_it($ups)) { print_it("\nCannot ping $ups"); $error{$ups} = 'Cannot ping'; next TARGET; } # Describe previous calibration event say 'Getting upsAdvTestCalibrationDate.0' if $debug > 2; $val = snmpGet( {host => $ups, oid => '.1.3.6.1.4.1.318.1.1.1.7.2.7.0'} ); $calib_date{$ups} = $val; say 'Getting upsAdvTestCalibrationResults.0' if $debug > 2; $val = snmpGet( {host => $ups, oid => '.1.3.6.1.4.1.318.1.1.1.7.2.6.0'} ); $calib_result{$ups} = $val; } # Debug trace trace_location('end') if $debug; return 1; } ######################################################################## # Gather information ######################################################################## sub info_before { my @remove; # Debug trace trace_location('begin') if $debug; # Notify operator print_it('Gathering more information...'); # Loop through targets, gathering information for my $target (@target) { # Describe previous calibration event $calib_date{$target} = snmpGet( {host => $target, oid => 'upsAdvTestCalibrationDate.0'} ); $calib_result{$target} = snmpGet( {host => $target, oid => 'upsAdvTestCalibrationResults.0'} ); # Entertain operator print $BANG if $mode eq 'interactive'; } # Make things look pretty say "\n" if $mode eq 'interactive'; # Debug trace trace_location('end') if $debug; return 1; } ######################################################################## # Tell operator what I did ######################################################################## sub print_after { my $shit_happens = 0; # Debug trace trace_location('begin') if $debug; # If running from cron, don't print report return 1 if $mode eq 'batch'; say "\n\n\n# Here is what I see"; # Iterate through @target for my $target (@target) { log_it("Set variables on $target") if $dome; $shit_happens++ if defined $error{$target}; } return 1 unless $mode eq 'interactive'; say(''); print < 0) { print "\n\n\n\n"; print <