Search code examples
pythonpysnmp

Partial SNMP table retrieval in one call?


I'm loooking to retrieve the values for memSensorsTemperature and memSensorsHumidity from the Power-NET MIB. I'm not sure if If I need to call this whole thing twice or if there is a way with a single call just to weed out the specific OIDs that I need. It looks like it is doing some kind of nested look when I include both in one call.

There are 6 values in each OID call... so 6 temp and 6 hum readings.

#!/usr/bin/env python

from pysnmp.hlapi import *

result1 = bulkCmd(SnmpEngine(),
           CommunityData('public'),
           UdpTransportTarget(('xx.xx.xx.xx', 161)),
           ContextData(),
           1, 6,
           ObjectType(
            ObjectIdentity('PowerNet-MIB', 'memSensorsTemperature').addAsn1MibSource('http://mibs.snmplabs.com/asn1/@mib@')
            ),
           ObjectType(
            ObjectIdentity('PowerNet-MIB', 'memSensorsHumidity').addAsn1MibSource('http://mibs.snmplabs.com/asn1/@mib@')
            ),
               lexicographicMode=False
)
#errorIndication, errorStatus, errorIndex, varBinds = next()
#memSensorsTemperature
for errorIndication, errorStatus, errorIndex, varBinds in result1:
  if errorIndication:
    print(errorIndication)
  elif errorStatus:
    print('%s at %s' % (errorStatus.prettyPrint(),
    errorIndex and varBinds[int(errorIndex) - 1][0] or '?'))
  else:
    for varBind in varBinds:
      print(' = '.join([x.prettyPrint() for x in varBind]))

RESULT(Truncated)

PowerNet-MIB::memSensorsHumidity.0.1 = 16
PowerNet-MIB::memSensorsTemperature.0.1 = 81
PowerNet-MIB::memSensorsHumidity.0.2 = 56
PowerNet-MIB::memSensorsTemperature.0.1 = 81
PowerNet-MIB::memSensorsHumidity.0.3 = 16
PowerNet-MIB::memSensorsTemperature.0.1 = 81
PowerNet-MIB::memSensorsHumidity.0.4 = 41
PowerNet-MIB::memSensorsTemperature.0.1 = 81
PowerNet-MIB::memSensorsHumidity.0.5 = 46
PowerNet-MIB::memSensorsTemperature.0.1 = 81
PowerNet-MIB::memSensorsHumidity.0.6 = -1
PowerNet-MIB::memSensorsTemperature.0.2 = 80
PowerNet-MIB::memSensorsHumidity.0.6 = No more variables left in this MIB View
PowerNet-MIB::memSensorsTemperature.0.2 = 80
PowerNet-MIB::memSensorsHumidity.0.6 = No more variables left in this MIB View
PowerNet-MIB::memSensorsTemperature.0.2 = 80
PowerNet-MIB::memSensorsHumidity.0.6 = No more variables left in this MIB View
PowerNet-MIB::memSensorsTemperature.0.2 = 80
PowerNet-MIB::memSensorsHumidity.0.6 = No more variables left in this MIB View
PowerNet-MIB::memSensorsTemperature.0.2 = 80
PowerNet-MIB::memSensorsHumidity.0.6 = No more variables left in this MIB View
PowerNet-MIB::memSensorsTemperature.0.2 = 80
PowerNet-MIB::memSensorsHumidity.0.6 = No more variables left in this MIB View```




Solution

  • You code seems almost right! You just need to set the nonRepeaters value to 0 instead of 1. Because with 1, your first object (memSensorsTemperature) is not getting iterated through - it just re-reads the same table entry.

    Code-wise:

    result1 = bulkCmd(SnmpEngine(),
               CommunityData('public'),
               UdpTransportTarget(('xx.xx.xx.xx', 161)),
               ContextData(),
               0, 6,  # get up to 6 follow-up OIDs per each requested OID
               ObjectType(ObjectIdentity(
                   'PowerNet-MIB', 'memSensorsTemperature').addAsn1MibSource(
                       'http://mibs.snmplabs.com/asn1/@mib@')
               ),
               ObjectType(ObjectIdentity('PowerNet-MIB', 'memSensorsHumidity')),
               lexicographicMode=False
    )
    

    The response will always be a 2-d table with 0 to 6 rows (depending on what's in response).

    RFC1905 explains the semantics of nonRepeaters:

       The values of the non-repeaters and max-repetitions fields in the
       request specify the processing requested.  One variable binding in
       the Response-PDU is requested for the first N variable bindings in
       the request and M variable bindings are requested for each of the R
       remaining variable bindings in the request.  Consequently, the total
       number of requested variable bindings communicated by the request is
       given by N + (M * R), where N is the minimum of:  a) the value of the
       non-repeaters field in the request, and b) the number of variable
       bindings in the request; M is the value of the max-repetitions field
       in the request; and R is the maximum of:  a) number of variable
       bindings in the request - N, and b)  zero.