Homebrew NMS: Put It Together with Perl and Net::SNMP - Page 2
Put It All Together With Perl
SNMP
- SNMP - Anything But Simple
- What To Do About SNMP Vulnerabilities
- Make Sense of SNMP/MRTG Alphabet Soup
- SNMP for Everybody
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 1.3.6.1.2.1.31.1.1.1.18.22 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 1.3.6.1.2.1.31.1.1.1.18'.
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:
#!/usr/bin/perl
use Net::SNMP;
my $desc = '1.3.6.1.2.1.31.1.1.1.18;
($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;
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?



