#!/opt/vdops/bin/perl # This script reports the value of romId.0 # V Who When What # --------------------------------------------------------------------------- # 1.2.0 skendric 2011-02-21 Upgrade to Netops 1.4.0 # 1.1.0 skendric 2010-03-30 Upgrade to perl 5.10.1 # 1.0.0 skendric 2009-10-07 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) # -Uses a variety of methods to ask each target for the name # of the image it is running and the name of its hardware type # -Produces a report # # # Requirements: # -The following MIB modules stashed in /opt/vdops/share/snmp/mibs, # or wherever it is that you store MIB modules: # CISCO-PRODUCTS-MIB.my # # -PERL modules: the WI::Netops collection # # # Assumptions: # # # Tested on: # -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 "inv-rom" to see the command-line options # -Try it out # # # # Caveats: # # # Known Bugs: # # # To do: # -Add support for SNMPv3 # # 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::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 @results; # Contents of report my %rom_version; # Define global variables $program_name = 'inv-rom'; $usage = 'Usage: inv-rom -s {yes|no} [-d {integer}] [-r] [-a | -e {expr} | -f {filename} | target1 target2 target3 ...]'; $version = '1.2.0'; # Target details @skip_name = qw/cf-vpn charon-x-vpn/; @suffixes = qw/-agw -dgw -esx -rtr -vpn -vpnhw/; # 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(); # Gather information do_the_work(); # Do the work build_report(); # Organize the data in a useful way print_report(); # Print report } ##### End Main Program ############################################### ######################################################################## # Build the report ######################################################################## sub build_report { my @a_fields; my @b_fields; my @raw; # If running in test mode, skip this routine return 1 unless $dome; # Debug trace trace_location('begin') if $debug; # Create an array containing the information I want to print for (my $i = 0; $i < @target; $i++) { my $target = $target[$i]; $raw[$i] = $target . ',' . $box{$target} . ',' . $rom_version{$target}; } # Create a sorted version of @raw @results = map { $_->[0] } sort { @a_fields = @$a[1..$#$a]; @b_fields = @$b[1..$#$b]; $a_fields[1] cmp $b_fields[1] || $a_fields[0] cmp $b_fields[0] } map { [ $_, split /,/ ] } @raw; # Debug trace trace_location('begin') if $debug; return 1; } ######################################################################## # Do the work: acquire image name ######################################################################## sub do_the_work { my @remove; # Debug trace trace_location('begin') if $debug; # Notify operator print_it('Acquiring ROM versions...'); # Loop through targets TARGET: for my $target (@target) { my ($rom, $string); # Acquire ROM string given ($box{$target}) { when (/ASA/i) { $rom = snmpGet( {host => $target, oid => 'entPhysicalFirmwareRev.1'} ); } when (/cat3|nme/i) { $rom = snmpGet( {host => $target, oid => 'romSysVersion.0'} ); ($rom) = ($rom =~ /Version (.*?),/); } when (/cat4/) { $rom = snmpGet({host=>$target, oid=>'entPhysicalFirmwareRev.1000'}); } when (/N5k/) { $rom = snmpGet( {host => $target, oid => 'entPhysicalFirmwareRev.22'} ); } default { $rom = snmpGet( {host => $target, oid => 'romId.0'} ); ($rom) = ($rom =~ /Version (.*?)[,|\s]/) if defined $rom; } } # Skip devices for which we didn't find a ROM version unless (defined $rom and $rom ne $EMPTY_STR) { print $DOT if $mode eq 'interactive'; push @remove, $target; next TARGET; } # Save result $rom_version{$target} = $rom; # Entertain operator print $BANG if $mode eq 'interactive'; } # Make things look pretty say "\n" if $mode eq 'interactive'; # Remove entries which failed checks prune_basic(@remove); # Debug trace trace_location('end') if $debug; return 1; } ######################################################################## # Tell the operator what I discovered ######################################################################## sub print_report { my @fields; my $handle; my $total = @target; my $now = get_now(); # Debug trace trace_location('begin') if $debug; # 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; } # 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} <