#!/opt/vdops/bin/perl # This script identifies ports in switches configured in SPAN mode. Uses # SMON-MIB support # V Who When What # --------------------------------------------------------------------------- # 1.1.0 skendric 2011-02-21 Upgrade to Netops 1.4.0 # 1.0.2 skendric 2010-01-26 Upgrade to perl 5.10.1 # 1.0.1 skendric 2007-12-21 Fiddle with report # 1.0.0 skendric 2007-09-11 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) # -Identifies ports configured in SPAN mode # -Produces a report # # # Requirements: # -Devices must support SMON-MIB # # -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 # -Try it out # # # # Caveats: # # # Known Bugs: # # # To do: # -In the report, specify which modules contain afflicted ports # -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::CiscoTools 1.4.3; 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 %span; # Hash of arrays (keyed by switch name), each # element of which consists of a hash, keyed # by destination ifName, pointing to a two # element array: the 0th element is the ifAlias # of the destination port, the 1st element is # an array of source ifNames # Define global variables $program_name = 'find-span-ports'; $usage = 'Usage: find-span-ports -s {yes|no} [-d {integer}] [-r] [-a | -e {expr} | -f {filename} | target1 target2 target3 ...]'; $version = '1.1.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(); # Gather information do_the_work(); # Do the work print_report(); # Print report } ##### End Main Program ############################################### ######################################################################## # Do the work: find SPAN ports ######################################################################## sub do_the_work { # Debug trace trace_location('begin') if $debug; # Notify operator print_it('Acquiring SPAN info...'); # Loop through targets for my $target (@target) { my (%arg, %if_name, $span_port_ref); # Look for span ports say 'Walking portCopyStatus' if $debug > 3; %arg = ( host => $target, oid => '.1.3.6.1.2.1.16.22.1.3.1.1.5' ); $span_port_ref = snmpWalk(\%arg); # Pull out SPAN ports PORT: for my $varbind (@$span_port_ref) { my (%arg, $dst, $dst_alias, $dst_name, $src, $src_name, $status); $status = $varbind->{val}; next PORT unless defined $status; next PORT unless $status ne $EMPTY_STR; next PORT unless $status eq 'active'; ($src, $dst) = split /\./, $varbind->{iid}; # Find src IF name say "Getting ifName.$src" if $debug > 3; %arg = (host => $target, oid => ".1.3.6.1.2.1.31.1.1.1.1.$src" ); $src_name = snmpGet(\%arg); # Find dst IF name say "Getting ifName.$dst" if $debug > 3; %arg = (host => $target, oid => ".1.3.6.1.2.1.31.1.1.1.1.$dst" ); $dst_name = snmpGet(\%arg); # Find dst IF alias say "Getting ifAlias.$dst" if $debug > 3; %arg = (host => $target, oid => ".1.3.6.1.2.1.31.1.1.1.18.$dst"); $dst_alias = snmpGet(\%arg); # Build data structure $if_name{$dst_name}->[0] = $dst_alias; push @{$if_name{$dst_name}->[1]}, $src_name; $shit_happens++; } # Save results $span{$target} = \%if_name if keys %if_name > 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 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} <{$dst}->[1]}; my $dst_alias = $span_ref->{$dst}->[0]; printf {$handle} "%-16s %-28.28s %-6s %-24.24s\n", $target, $src_list, $dst, $dst_alias; } } unless ($handle =~ /STDOUT/) { close $handle or warn "Cannot close $report_file: $!\n"; } # Make things look pretty print_it("\n\nEnding $PROGRAM_NAME"); # Debug trace trace_location('end') if $debug; return 1; } ######################################################################## # Output help ######################################################################## sub HELP_MESSAGE { print <