Pages

Monday, January 20, 2014

Getting the connector type on your Cisco devices using SNMP

At the end of 2013, the company where I'm currently working at, needed to know how many unused connectors are on our running devices (mainly Cisco devices).
So, I needed to know what kind of information I could get from these devices.

The simplest way to do that would be to log on each device and then check the unused connector with a show command. We could make a script that will perform the task and return the connector (unfortunately there were a lot of devices to check on). The problem with this method is that using the script or using the manual way, we have to log on the device, and the company has a "no connection" policy (and that makes you realize that the Network is a really sensible thing on the general infrastructure).

I was working on a script using SNMP polling to make my own "show interface status" merged with the "show ip int brief" and "show etherchannel summary" command, and so I looked on how the connector information was stored in the MIB...
But at first, let's talk briefly about the SNMP protocol.

The SNMP protocol defines the way to deal with network information contained on the MIB of each device. Some MIBs follows the IETF RFCs (standard MIB) and each manufacturers can create their own MIB as well. Each information in the MIB is defined with an ID called OID (Object IDentifier).
Getting the information using SNMP can be done actively (we request the information) or passively (the network device is configured to send a notification - a SNMP trap). In our case, we'll poll each device to get the content of some parts of the MIB.

To retrieve the information, we'll use the snmpbulkwalk command on Linux system to poll what we want on our devices. We can also use the snmpwalk command, but it's a bit slower in our use (this is due to the command used : GetNext vs GetBulk).

Now, the trickiest part : find the corresponding OIDs. Some of them are quite easy to find, because they're follow a standard (ifIndex, ifName). But how about our connectors?

To be simple, here what I found:
  • on Cisco IOS (tried on Cisco 6500)
    All the information are on the OID 1.3.6.1.2.1.47.1.1.1.1 (entPhysicalEntry).
    The information is kept in a container-contained fashion. i.e: The port (container) contains a SFP connector (contained item).
    The interface and the connector are identified using an ID.
  • on Cisco NX-OS, it's a bit different (tried on Nexus3k and Nexus5k)
    The behavior is different that on Cisco IOS, and some MIBs are not supported yet (MIBs supported on NX-OS 6.2).
    For the Nexus3k, the information that need to be retrieve is stored in the OID 1.3.6.1.2.1.26.2.1.1.11 (ifMauDefaultType). Please note that the Nexus3k don't provide the default value for the SFP-1G connector (maybe a bug?). Anyway, the OID will return a code which corresponds to a specific type, i.e : the code 36 is for 10G-SR. (MAU MIB type code
    On the Nexus5k, the information I used is stored in 1.3.6.1.2.1.31.1.1.1.17 (ifConnectorPresent), to check if there is a connector. I didn't took the time to search if there is or no an OID for this model.
Anyway, now that we have the OID, we just have to write a few lines of code to:
- get the content of the MIB (with the snmpbulkwalk command)
- read the content and get the interesting part
- print it.

In example, I write down how to get the interface name, but the code is quite close to get the connectors.

my $switch = 'router';
my $community ='public';
my $cmd = "/usr/bin/snmpbulkwalk -v 2c -c $community $switch ifName";
my %ifIndex;
open (SNMP, "$cmd") or die "error\n";
while (<SNMP>){
   my $line = $_;
   if ($line =~ /:ifName.(\d*) = STRING (.*)/{
   my $index = $1;
   my $value = $2;
   $ifIndex{$index} = $value;
   }
}
close (SNMP);

foreach my $key (keys %ifIndex){
   print "ifIndex: $key \t ifName: $ifIndex{$key} \n";
}

Finally, it turns out that it wasn't that simple, but the script is working for let's say about 90% of our devices, so it's better than nothing.

PS : Cisco Mib browser may be useful
PS 2 : snmp ifIndex persist can be useful too.

No comments: