Querying SNMP data

In the previous section we examined how to locate the data items you are interested in querying for in the MIB tree. In this section we shall examine how to query an SNMP enabled host for this information using the utilities provided with the net-analyzer/net-snmp package.

Simple SNMP queries

We have already used the snmpwalk utility to retrieve some data from an SNMP daemon when we tested our installation in the Installing the SNMP software section of this guide. As you can see from the example below the snmpwalk utility can be used to retrieve data from an SNMP enabled host, however it is really designed more for exploring the available data as it is capable of retrieving multiple data items.

lisa snmpwalk -c hacking -v 2c localhost system
SNMPv2-MIB::sysDescr.0 = STRING: "Web server (node 1)" 
SNMPv2-MIB::sysObjectID.0 = OID: NET-SNMP-MIB::netSnmpAgentOIDs.10 
DISMAN-EVENT-MIB::sysUpTimeInstance = Timeticks: (216451) 0:36:04.51 
SNMPv2-MIB::sysContact.0 = STRING: sysdamin@internal.hacking.co.uk 
SNMPv2-MIB::sysName.0 = STRING: web-1-1 
SNMPv2-MIB::sysLocation.0 = STRING: "Colo 1 Shelf 2" 
SNMPv2-MIB::sysServices.0 = INTEGER: 72 
... 

Whilst we could use the snmpwalk utility to retrieve single data items from an SNMP daemon a better choice would be the snmpget utility which is designed specifically for this purpose. In the example below we use the snmpget utility to retrieve the current system up-time using the user-friendly name DISMAN-EVENT-MIB::sysUpTimeInstance although we could have used the dotted-decimal notation had we preferred.

lisa snmpget -c hacking -v 2c localhost DISMAN-EVENT-MIB::sysUpTimeInstance
DISMAN-EVENT-MIB::sysUpTimeInstance = Timeticks: (25899653) 2 days, 23:56:36.53 

The snmpget utility has a variety of options which control the display of the returned data, the most useful of which is probably the -Oqv option which returns just the data without the Object ID (OID) portion of the query. In the example below we show the use of this option when retrieving the email address of the administrative contact.

lisa snmpget -c hacking -v 2c localhost -Oqv SNMPv2-MIB::sysContact.0
sysdamin@internal.hacking.co.uk 

Tabular SNMP queries

In the above examples we have used the snmpget utility to retrieve a variety of single data values from an SNMP daemon however not all of the data available is easily consumed in this form. Some of the data available from an SNMP daemon is inherently tabular in nature. A good example of such data can be found in the LM-SENSORS-MIB.

Let us begin by using the snmptranslate utility to search for any objects pertaining to voltage sensors using the example command below. As you can see it returns a single value LM-SENSORS-MIB::lmVoltSensorsTable which looks likely to be the voltage information we are seeking.

lisa snmptranslate -TB 'volt'
LM-SENSORS-MIB::lmVoltSensorsTable 

The snmpget application cannot be used to retrieve the entire lmVoltSensorsTable in a single command. If we were to attempt to do so it would simply inform us that there is no object available with this OID, as can be seen from the example below.

lisa snmpget -c hacking -v 2c host1 LM-SENSORS-MIB::lmVoltSensorsTable
LM-SENSORS-MIB::lmVoltSensorsTable = No Such Object available on this agent at this OID 

If we use the snmptranslate utility to show a tree of the nodes available starting with the LM-SENSORS-MIB::lmVoltSensorsTable we can immediately see the problem. The lmVoltSensorsTable is merely an intermediate node not a node we can query. The actual data we are interested in is stored in an indexed table below this node.

lisa snmptranslate -Tp LM-SENSORS-MIB::lmVoltSensorsTable
+--lmVoltSensorsTable(4) 
   +--lmVoltSensorsEntry(1) 
      |  Index: lmVoltSensorsIndex 
      +-- -R-- Integer32 lmVoltSensorsIndex(1) 
      |        Range: 0..65535 
      +-- -R-- String    lmVoltSensorsDevice(2) 
      |        Textual Convention: DisplayString 
      |        Size: 0..255 
      +-- -R-- Gauge     lmVoltSensorsValue(3) 

Before we can access such indexed data we need to obtain the dotted-decimal address of the LM-SENSORS-MIB::lmVoltSensorsTable node using the snmptranslate utility as shown below.

lisa snmptranslate -On LM-SENSORS-MIB::lmVoltSensorsTable
.1.3.6.1.4.1.2021.13.16.4 

Armed with the output of the the above two commands we can query the table using the snmpget application, as shown below.

lisa snmpget -c hacking -v 2c -Oqv host1 .1.3.6.1.4.1.2021.13.16.4.1.2.1
VCoreA 
lisa snmpget -c hacking -v 2c -Oqv host1 .1.3.6.1.4.1.2021.13.16.4.1.3.1
1010 

As you can see this is a fairly cumbersome set of commands to have to execute just to retrieve the value of a single sensor output. Luckily the net-analyzer/net-snmp package includes the snmptable utility which is designed to extract tabular data from an SNMP daemon.

The example command below demonstrates how to display a formatted table of data retrieved from an SNMP daemon using the snmptable utility.

lisa snmptable -c hacking -v 2c host1 LM-SENSORS-MIB::lmVoltSensorsTable
SNMP table: LM-SENSORS-MIB::lmVoltSensorsTable 
 
 lmVoltSensorsIndex lmVoltSensorsDevice lmVoltSensorsValue 
                  1              VCoreA               1016 
                  2              VCoreB                264 
                  3                 Vtt               1198 
                  4               +3.3V               3344 
                  5                +12V              12096 
                  6                 +5V               4974 
                  7                5VSB               5070 
                  8                VBAT               3072