Using the Net::SNMP module is a very good way to build your own SNMP check plugins:
#!/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)) { my $err = $session->error; |
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!
SNMP OID for UNIX number of logged in users:
# snmpget -v2c -c public hpunixhost .1.3.6.1.4.1.11.2.3.1.1.2.0
SNMPv2-SMI::enterprises.11.2.3.1.1.2.0 = Gauge32: 1
SNMP OID for UNIX number of running processes:
# snmpget -v2c -c public hpunixhost enterprises.11.2.3.1.4.1.0
SNMPv2-SMI::enterprises.11.2.3.1.4.1.0 = Gauge32: 134
SNMP OID for LINUX number of logged in users:
# snmpget linuxhost -v2c -c public 1.3.6.1.2.1.25.1.5.0
HOST-RESOURCES-MIB::hrSystemNumUsers.0 = Gauge32: 1
Leave a Reply