#!/opt/vdops/bin/perl # This script identifies ports on Cisco switches which have dropped packets # due to a Quality of Service feature. Requires CISCO-CLASS-BASED-QOS-MIB # or CISCO-PORT-QOS-MIB support # V Who When What # --------------------------------------------------------------------------- # 1.1.0 skendric 2011-02-21 Upgrade to Netops 1.4.0 # 1.0.0 skendric 2007-09-17 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 the ports on switches where the cbQosCMDropPkt64 or # cbQosCMNoBufDropPkt64 counters are non-zero # -Produces a report lists these switches/ports # # # 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 # -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 %dropped; # Hash of arrays listing the ports which have # experienced drops, keyed by switch name my %qos_type; # Hash holding either 'port' or 'class', # representing support for either the # CISCO-PORT-QOS-MIB or # CISCO-CLASS-BASED-QOS-MIB # Define global variables $program_name = 'find-qos-drops'; $usage = 'Usage: find-qos-drops -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 info_before(); # Identify QOS MIB support sanity_check(); # Check for errorsCheck for errors do_the_work(); # Do the work print_report(); # Print report } ##### End Main Program ############################################### ######################################################################## # Do the work ######################################################################## sub do_the_work { # Debug trace trace_location('begin') if $debug; # Notify operator print_it('Acquiring QOS drop counts...'); # Loop through targets TARGET: for my $target (@target) { my $if_name; # Switch on MIB support (CISCO-CLASS-BASED-QOS-MIB or CISCO-PORT-QOS-MIB) when ($qos_type{$target}) { given ('class') { my $class_drop_ref; # Acquire cbQosCMDropPkt64 say 'Walking cbQosCMDropPkt64' if $debug > 3; $class_drop_ref = snmpWalk({host=>$target, oid=>'cbQosCMDropPkt64'}); unless (defined $class_drop_ref and @$class_drop_ref > 0) { log_it("$target not responding to cbQosCMDropPkt64 walk, skipping"); print $DOT if $mode eq 'interactive'; next TARGET; } # Look for drops INTERFACE: for my $varbind (@$class_drop_ref) { if ($varbind->{val} != 0) { my ($q_index, $if_index, $if_name); $q_index = $varbind->{iid}; # Get cbQosIfIndex $if_index = snmpGet( {host => $target, oid => "cbQosIfIndex.$q_index"} ); unless (defined $if_index and $if_index ne $EMPTY_STR) { push @{$dropped{$target}}, $QUERY; next INTERFACE; } # Get ifName $if_name = snmpGet({host => $target, oid => "$if_name.$if_index"}); $if_name = $QUERY unless (defined $if_name and $if_name ne ''); push @{$dropped{$target}}, $if_name; } # End '$varbind->{val} != 0' } # End 'Look for drops' } # End case 'class' given ('port') { my $port_drop_ref; # Acquire cportQosDropPkts $port_drop_ref = snmpWalk({host => $target, oid => 'cportQosDropPkts'}); unless (defined $port_drop_ref and @$port_drop_ref > 0) { log_it("$target not responding to cportQosDropPkts walk, skipping"); next TARGET; print $DOT if $mode eq 'interactive'; } # Look for drops INTERFACE: for my $varbind (@$port_drop_ref) { if ($varbind->{val} != 0) { my ($drops, $if_index, $if_name); $drops = $varbind->{val}; # Dig out if_index ($if_index) = ($varbind->{iid} =~ /^(\d+)\./); unless (defined $if_index and $if_index ne $EMPTY_STR) { log_it("Cannot parse $if_index for $target, skipping"); next INTERFACE; } # Get ifName $if_name = snmpGet({host => $target, oid => "$if_name.$if_index"}); $if_name = $QUERY unless (defined $if_name and $if_name ne ''); push @{$dropped{$target}}, $if_name; } # End '$varbind->{val} != 0 } # End 'Look for drops' } # End case 'port' } # End 'switch' # Entertain operator print $BANG if $mode eq 'interactive'; } # End 'Loop through targets' # Make things look pretty say "\n" if $mode eq 'interactive'; # Debug trace trace_location('end') if $debug; return 1; } ######################################################################## # Gather more info ######################################################################## sub info_before { my @remove; # Debug trace trace_location('begin') if $debug; # Notify operator print_it('Identifying QOS MIB support...'); # Loop through targets, looking for problems TARGET: for my $target (@target) { my ($class_ref, $cport); # Does this device support CISCO-CLASS-BASED-QOS-MIB? $class_ref = snmpWalk( {host => $target, oid => 'cbQosIfType'} ); if (@$class_ref > 0) { $qos_type{$target} = 'class'; print $BANG if $mode eq 'interactive'; next TARGET; } # Does this device support CISCO-PORT-QOS-MIB? $cport = snmpWalk( {host => $target, oid => 'cportQosIndexTypeNew.0'} ); if (defined $cport and $cport ne $EMPTY_STR) { $qos_type{$target} = 'port'; print $BANG if $mode eq 'interactive'; next TARGET; } print $DOT if $mode eq 'interactive'; } # Remove entries which failed checks prune_basic(@remove); # 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; } # Count number of switches $shit_happens = keys %dropped; # 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} < 47) { if ($first == 1) { printf {$handle} " %-18s %-55s\n", $target, $line; $first = 0; undef $line; } else { printf {$handle} " %-55s\n", $line; undef $line; } } } # Handle any left-over interfaces if ($first == 1) { printf {$handle} " %-18s %-55s\n", $target, $line; } elsif (defined $line and length $line <= 47) { printf {$handle} " %-55s\n", $line; } } unless ($handle =~ /STDOUT/) { close $handle or warn "Cannot close $report_file: $!\n"; } # Debug trace trace_location('end') if $debug; return 1; } ######################################################################## # Sanity check ######################################################################## sub sanity_check { my @remove; # Debug trace trace_location('begin') if $debug; # Notify operator print_it('Sanity check...'); # Loop through targets, looking for problems for my $target (@target) { push @remove, $target unless defined $qos_type{$target}; print $BANG if $mode eq 'interactive'; } # Remove entries which failed checks prune_basic(@remove); # Make things look pretty say "\n" if $mode eq 'interactive'; # Debug trace trace_location('end') if $debug; return 1; } ######################################################################## # Output help ######################################################################## sub HELP_MESSAGE { print <