0

I need to find the object name and data type for a given OID. I can get the name, but can't find the functionality in PySNMP to also return the data type (integer, octet string, counter...).

What I have so far (example):

from pysnmp.smi import builder, view, compiler

mibBuilder = builder.MibBuilder()
compiler.addMibCompiler(mibBuilder, sources=['/usr/share/snmp/mibs'])
mibBuilder.loadModules('IF-MIB', ...)
mibView = view.MibViewController(mibBuilder)

oid, label, suffix = mibView.getNodeName((1,3,6,1,2,1,31,1,1,1,6))
print(label)

This returns the name for the oid (ifHCInOctets) but I also need it to return the data type, which in this case would be Counter.

Is there a function in PySNMP to find the data type?

alena
  • 61
  • 1
  • 1
  • 12

1 Answers1

3

With pysnmp model, there is the MibBuilder class which loads MIB objects into memory and address them by their MIB name and MIB object name.

On top of MibBuilder there is the MibViewController class which maintains a couple of indices to address the same MIB objects (as held by MibBuilder) by their other attributes such as OID.

Therefore:

from pysnmp.smi import builder, view, compiler

# Load MIB objects, index them by MIB::name
mibBuilder = builder.MibBuilder()

# If Pythonized MIB is not present, call pysmi parser to fetch
# and compile requested MIB into Python
compiler.addMibCompiler(mibBuilder, sources=['/usr/share/snmp/mibs'])

# Load or compile&load this MIB
mibBuilder.loadModules('IF-MIB')

# Index MIB objects (as maintained by `mibBulder`) by OID
mibView = view.MibViewController(mibBuilder)

# Look up MIB name and MIB object name by OID
modName, symName, suffix = mibView.getNodeLocation((1,3,6,1,2,1,31,1,1,1,6))

# Fetch MIB object
mibNode, = mibBuilder.importSymbols(modName, symName)

# This might be an ASN.1 schema object representing one of SNMP types
print(mibNode.syntax.__class__)

The documentation is lacking indeed.... Though this is improving in the upcoming version.

Hope this helps.

Ilya Etingof
  • 5,440
  • 1
  • 17
  • 21
  • Yes, thank you very much! I've tried to make sense of the documentation, but couldn't find anything helpful. This solves my problem perfectly. – alena Jan 11 '19 at 13:19