问题
I am trying to write a piece of code that accepts a JSON object with OIDs as keys and OID values as values. An Example would be:
{".1.3.6.1.4.1.562.29.6.2.3": "Link Down",
...
}
When this JSON object is received I want to translate the OID and the OID value using PySNMP, but I do not know how I can translate the OID value according to Textual Conventions defined within a corresponding MIB file.
An example MIB file would define:
TruthValue ::= TEXTUAL-CONVENTION
STATUS current
DESCRIPTION
"Represents a boolean value."
SYNTAX INTEGER { true(1), false(2) }
Given an OID and an OID value that follows a textual convention like the one above I would like to translate:
{"OID": 1,...}
into {"OID": true,...}
Is this possible with PySNMP?
回答1:
That is possible with pysnmp:
from pysnmp.smi import builder
mibBuilder = builder.MibBuilder()
TruthValue, = mibBuilder.importSymbols('SNMPv2-TC', 'TruthValue')
print(TruthValue(1).prettyPrint()) # prints 'true'
However in general you would have to somehow map OIDs to value types (some of which may resolve into TEXTUAL-CONVENTIONS). This can be done in an ad-hoc manner by hardcoding OID->type mapping for specific OIDs in your app, but a more general solution is to employ pysnmp MIB services:
from pysnmp.smi import view, builder
mibViewController = view.MibViewController(builder.MibBuilder())
varName = mibvar.MibVariable('1.3.6.1.6.3.10.2.1.1.0').loadMibs('SNMP-FRAMEWORK-MIB').resolveWithMib(mibViewController)
print(varName.getMibNode().getSyntax().clone('12341234'))
The above example would fetch value type for 1.3.6.1.6.3.10.2.1.1.0 and cast 12341234 value into associated type.
UPDATED:
Consider using the higher-level interface to MIB services which is available since pysnmp 4.3
来源:https://stackoverflow.com/questions/27097809/translate-oid-value-pairs-from-mib-textual-convention-using-pysnmp