Homebrew NMS: Put It Together with Perl and Net::SNMP - Page 2

Understanding SNMP is key to understanding what's going on with your network, and critical to any tool you build. Perl's Net::SNMP helps gather the data.

 By Charlie Schluting
Page 2 of 2   |  Back to Page 1
Print Article

Put It All Together With Perl

Hopefully we know Perl already. We'll start by finding the OID we want. This is done by examining the MIBs from your switch's vendor, and painstakingly figuring out which object provides the information. Luckily I already know that IF-MIB::ifAlias.22 will give me the description for port alias number 22. Also, this isn't a vendor-specific item.This isn't the normal "Gi2/37" form we're used to, but it can be converted to the friendly string using other objects to dereference the ifAlias number 22.

% snmpwalk -v2c -c public switch_hostname
 IF-MIB::ifAlias.22 = STRING: nermal.domain.com [F324 60-18]

My computer's name is nermal, and ourport descriptions at work include two other pieces of information: the wall jack number, and room number. Now that we know what OID is required, it's time to pull all port descriptions from the entire switch, just: 'snmpwalk'.

In Perl, we simply need to define our OIDs, and connect to the agent on our Cisco to grab the information. Using the Net::SNMP module is the best way:

use Net::SNMP;
my $desc = ';
($session, $error) = Net::SNMP->session(
                   -hostname => "switch",
                   -community => "public",
                   -timeout  => "30",
                   -port => "161");

if (!defined($session)) {
      printf("ERROR: %s.\n",  $error);
      exit 1;
my $response = $session->get_request($desc);
my %pdesc = %{$response};

my $err = $session->error;
if ($err){
   return 1;
print %pdesc;
exit 0;

That's all there is to it. Net::SNMP->session() creates an object, which can be used to call the get_table() method. If you want multiple OIDs in the same session, just define them as comma-seperated values, like so: get_request($desc,$uptime,$etc). The get_table() method returns a reference to a hash, by the way.

If you run that snippet of code, you should get a huge jumbled list, which will contain the key-value pairs of the interface alias and the string. It's a hash, so to print it prettily you must reference each entry individually. Most likely you'll be using this data in combination with other data, possibly from a database. You can compare values on-the-fly, in this example, to ensure that the string is correct.

Likely the most common use of Net::SNMP by sysadmins is to poll equipment for temperature, CPU, memory and other data. This functionality is built-in to most monitoring applications, but often times it needs to be told about MIBs for new devices that do not follow standards.

I hope that it's clear how easy it is to fetch this information, once you know the correct OID. It is amazing how useful Net::SNMP becomes once you start using it. You'll want to pull all kinds of tiny nuggets of information from all sorts of SNMP-enabled devices. By all means, gather as much data as you can!

Having the ability to verify a device's configuration, or perhaps configuring the device in real-time, is an admirable goal. Next time we'll talk about using DBD::Pg to store this information or retrieve other information, which, when combined with Net::SNMP, is quite useful for real-time querying of network information.

Previously: Your NMS: Time to Go Homebrew?

This article was originally published on Aug 30, 2007
Get the Latest Scoop with Networking Update Newsletter