3

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?

user3530640
  • 85
  • 1
  • 5

1 Answers1

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

Ilya Etingof
  • 5,440
  • 1
  • 17
  • 21
  • Thanks for your reply. I tried implementing your more general solution with a MIB file and OID that follows a textual convention and I included `from pysnmp.entity.rfc3413.oneliner import mibvar` in order to get access to mibvar. Is the print statement supposed to output a translated OID value? For me the output of `print(varName.getMibNode().getSyntax().clone('2'))` is the untranslated integer `2`. – user3530640 Nov 24 '14 at 14:46
  • Yes, you should get translated value if: 1) the OID your are working with has associated type which is a TEXTUAL-CONVENTION and 2) you have loaded MIB where the OID you are working with is defined and 3) value you are trying to translate (e.g. 2) has symbolic representation defined in its TEXTUAL-CONVENTION. – Ilya Etingof Nov 26 '14 at 21:55
  • I found that using prettyPrint() like so:`print(varName.getMibNode().getSyntax().clone('12341234').prettyPrint())`printed a translation from my test MIB but I seem to have issues with other MIBs and the `resolveWithMIB` function. I'm getting `pysnmp.smi.error.SmiError: Short OID for index NnExtAlarmEventType()`. Would it be best to ask this in another question? – user3530640 Nov 28 '14 at 06:12
  • Keep in mind that if OID acts as a table index, it must be translatable into index value(s). But that is a completely different issue. Code and MIB sample would be helpful as well. – Ilya Etingof Nov 29 '14 at 07:10
  • Thanks for you help! I've posted the other issue here: http://stackoverflow.com/questions/27224002/pysnmp-short-oid-error-trying-to-translate-oids-using-mib-textual-conventions – user3530640 Dec 01 '14 at 08:00