#!/opt/vdops/bin/perl # This script automates the process of rebooting APC management cards # 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 12-23-2009 Remove Net::SNMP support # 1.6.6 skendric 12-30-2008 Support new snmpSet format # 1.6.5 skendric 04-09-2007 Change name # 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 WI::VDOPS module structure # 1.5.0 skendric 05-09-2005 Support Netops.pm-1.2 # 1.4.1 skendric 04-03-2005 Pause before info_gather # 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.3 skendric 02-02-2004 Documentation fixes # 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-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) # -Reboots them # -Pings them and reports on any which did not survive # # 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-reset-device" to see the command-line options # -Try it out # # # Caveats: # # # Known Bugs: # # # To do: # -Test username/password foo # # 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; # Define global variables $program_name = 'apc-reset-device'; $usage = 'Usage: apc-reset-device -s {yes|no} [-d {integer}] [-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 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 { my $val; # Result of snmpGet my @varbind; # List of triplets to set # Debug trace trace_location('begin') if $debug; # Notify operator print_it('Resetting devices...'); # Do it if ($dome) { # Loop through @target, resetting management cards for my $target (@target) { # Reboot say 'Setting mcontrolRestartAgent.0' if $debug > 2; push @varbind, 'mcontrolRestartAgent', 1, 1, 'INTEGER'; $val = snmpSet( {host => $target, varbind => \@varbind} ); # Set error message $error{$target} = 'Unable to reset' unless $val; # Make things look pretty print $BANG if $mode eq 'interactive'; } } else { sleep $short; print_it('just kidding'); return 1; } # Make things look pretty say "\n" if $mode eq 'interactive'; # Pause to allow target SNMP agents to reset sleep $long; sleep $long; # Debug trace trace_location('end') if $debug; return 1; } ######################################################################## # Gather information after file uploads ######################################################################## sub info_after { # Debug trace trace_location('begin') if $debug; # Notify operator print_it('Pinging targets ...'); # Loop through targets, gathering information TARGET: for my $target (@target) { unless (ping_it($target)) { print_it("\nCannot ping $target"); $error{$target} = 'Cannot ping'; next TARGET; } } # 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# Here is what I see"; # If we are running in test mode, then return unless ($dome) { print "Running in test mode, cannot print a meaningful report\n\n"; return 1; } # Iterate through @target for my $target (@target) { log_it("Set variables on $target") if $dome; $shit_happens++ if defined $error{$target}; } return 1 if $mode eq 'batch'; # Congratulate operator say "\n\nDone rebooting devices"; print "\n"; print < 0) { print "\n\n\n\n"; print <