Nagios: howto become a happy network and system monitoring engineer

Nagios: howto become a happy network and system monitoring engineer

Nagios: howto become a happy network and system monitoring engineer 150 150 Roderick Derks

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)) {
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!

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

 

Roderick Derks

Liefhebber van fietsen, van het oplossen van IT puzzels, en van het delen van informatie om anderen te helpen.

All stories by:Roderick Derks

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

    Your Name (required)

    Your Email (required)

    Subject

    Your Message

      Your Name (required)

      Your Email (required)

      Subject

      Your Message