Search code examples
pythonsnmppysnmp

Translating pysnmp numerical oid output into symbolic names


I wrote a script (below) to test how well I could use pysnmp in one of my applications. I need to be able to mib-walk network elements with names like ifDescr, and my output needs to reference the same name I queried with...

from pysnmp.smi import builder, view, error
from pysnmp.entity.rfc3413.oneliner import cmdgen

cmdGen = cmdgen.CommandGenerator()
mibBuilder = builder.MibBuilder()
mibPath = mibBuilder.getMibPath() + ('/opt/python/Models/Network/MIBs',)
mibBuilder.setMibPath(*mibPath)
mibBuilder.loadModules(
    'RFC-1213',
    )
mibView = view.MibViewController(mibBuilder)
errorIndication, errorStatus, errorIndex, \
                 varBindTable = cmdGen.nextCmd(
    # SNMP v2
    cmdgen.CommunityData('test-agent', 'public'),
    # Transport
    cmdgen.UdpTransportTarget(('192.168.56.131', 161)),
    (('', 'ifDescr'),),
    )

print varBindTable

Problem

When I execute this code, pysnmp correctly recognizes ifDescr from my local definition of RFC-1213.py; however, pysnmp isn't translating the mib OIDs into names like ifDescr in the output below...

Question

How can I make pysnmp output the ObjectName() instances as ObjectName(ifDescr.1) instead of ObjectName(1.3.6.1.2.1.2.2.1.2.1)?

[
[(ObjectName(1.3.6.1.2.1.2.2.1.2.1), OctetString('Unit: 1 Slot: 0 Port: 1 10G - Level'))], 
[(ObjectName(1.3.6.1.2.1.2.2.1.2.2), OctetString('Unit: 1 Slot: 0 Port: 2 10G - Level'))], 
[(ObjectName(1.3.6.1.2.1.2.2.1.2.3), OctetString('Unit: 1 Slot: 0 Port: 3 10G - Level'))], 
[(ObjectName(1.3.6.1.2.1.2.2.1.2.4), OctetString('Unit: 1 Slot: 0 Port: 4 10G - Level'))], 
[(ObjectName(1.3.6.1.2.1.2.2.1.2.5), OctetString('Unit: 1 Slot: 0 Port: 5 10G - Level'))], 
[(ObjectName(1.3.6.1.2.1.2.2.1.2.6), OctetString('Unit: 1 Slot: 0 Port: 6 10G - Level'))], 
[(ObjectName(1.3.6.1.2.1.2.2.1.2.7), OctetString('Unit: 1 Slot: 0 Port: 7 10G - Level'))], 
[(ObjectName(1.3.6.1.2.1.2.2.1.2.8), OctetString('Unit: 1 Slot: 0 Port: 8 10G - Level'))], 
[(ObjectName(1.3.6.1.2.1.2.2.1.2.9), OctetString('Unit: 1 Slot: 0 Port: 9 10G - Level'))], 
[(ObjectName(1.3.6.1.2.1.2.2.1.2.10), OctetString('Unit: 1 Slot: 0 Port: 10 10G - Level'))], 
[(ObjectName(1.3.6.1.2.1.2.2.1.2.11), OctetString('Unit: 1 Slot: 0 Port: 11 10G - Level'))], 
[(ObjectName(1.3.6.1.2.1.2.2.1.2.12), OctetString('Unit: 1 Slot: 0 Port: 12 10G - Level'))], 
[(ObjectName(1.3.6.1.2.1.2.2.1.2.13), OctetString('Unit: 1 Slot: 0 Port: 13 10G - Level'))], 
[(ObjectName(1.3.6.1.2.1.2.2.1.2.14), OctetString('Unit: 1 Slot: 0 Port: 14 10G - Level'))], 
[(ObjectName(1.3.6.1.2.1.2.2.1.2.15), OctetString('Unit: 1 Slot: 0 Port: 15 10G - Level'))], 
[(ObjectName(1.3.6.1.2.1.2.2.1.2.16), OctetString('Unit: 1 Slot: 0 Port: 16 10G - Level'))], 
[(ObjectName(1.3.6.1.2.1.2.2.1.2.17), OctetString('Unit: 1 Slot: 0 Port: 17 10G - Level'))], 
[(ObjectName(1.3.6.1.2.1.2.2.1.2.18), OctetString('Unit: 1 Slot: 0 Port: 18 10G - Level'))], 
[(ObjectName(1.3.6.1.2.1.2.2.1.2.19), OctetString('Unit: 1 Slot: 0 Port: 19 10G - Level'))], 
[(ObjectName(1.3.6.1.2.1.2.2.1.2.20), OctetString('Unit: 1 Slot: 0 Port: 20 10G - Level'))], 
[(ObjectName(1.3.6.1.2.1.2.2.1.2.21), OctetString('Unit: 1 Slot: 0 Port: 21 10G - Level'))], 
[(ObjectName(1.3.6.1.2.1.2.2.1.2.22), OctetString('Unit: 1 Slot: 0 Port: 22 10G - Level'))], 
[(ObjectName(1.3.6.1.2.1.2.2.1.2.23), OctetString('Unit: 1 Slot: 0 Port: 23 10G - Level'))], 
[(ObjectName(1.3.6.1.2.1.2.2.1.2.24), OctetString('Unit: 1 Slot: 0 Port: 24 10G - Level'))], 
[(ObjectName(1.3.6.1.2.1.2.2.1.2.25), OctetString(' CPU Interface for Unit: 0 Slot: 5 Port: 1'))], 
[(ObjectName(1.3.6.1.2.1.2.2.1.2.26), OctetString('Link Aggregate 1'))], 
[(ObjectName(1.3.6.1.2.1.2.2.1.2.27), OctetString('Link Aggregate 2'))],  
[(ObjectName(1.3.6.1.2.1.2.2.1.2.28), OctetString('Link Aggregate 3'))], 
[(ObjectName(1.3.6.1.2.1.2.2.1.2.29), OctetString('Link Aggregate 4'))], 
[(ObjectName(1.3.6.1.2.1.2.2.1.2.30), OctetString('Link Aggregate 5'))], 
[(ObjectName(1.3.6.1.2.1.2.2.1.2.31), OctetString('Link Aggregate 6'))], 
[(ObjectName(1.3.6.1.2.1.2.2.1.2.32), OctetString('Link Aggregate 7'))], 
[(ObjectName(1.3.6.1.2.1.2.2.1.2.33), OctetString('Link Aggregate 8'))],  
[(ObjectName(1.3.6.1.2.1.2.2.1.2.38), OctetString('Vl21'))], 
[(ObjectName(1.3.6.1.2.1.2.2.1.2.39), OctetString('Vl22'))]
]

UPDATE

The explicit solution based on Popper12's answer is

import string
from pysnmp.smi import builder, view, error
from pysnmp.entity.rfc3413.oneliner import cmdgen
from collections import namedtuple as NT

cmdGen = cmdgen.CommandGenerator()
mibBuilder = builder.MibBuilder()
mibPath = mibBuilder.getMibPath() + ('/opt/python/Models/Network/MIBs',)
mibBuilder.setMibPath(*mibPath)
mibBuilder.loadModules(
    'RFC-1213',
    )
mibView = view.MibViewController(mibBuilder)
errorIndication, errorStatus, errorIndex, \
                 varBindTable = cmdGen.nextCmd(
    # SNMP v2
    cmdgen.CommunityData('test-agent', 'public'),
    # Transport
    cmdgen.UdpTransportTarget(('192.168.56.131', 161)),
    (('', 'ifDescr'),),
    )

SNMPObject = NT('SNMPObject', ['modName', 'symName', 'index', 'value'])

if errorIndication:
    print errorIndication
else:
    if errorStatus:
        print '%s at %s\n' % (
            errorStatus.prettyPrint(),
            varBindTable[-1][int(errorIndex)-1]
            )
    else:
        for varBindTableRow in varBindTable:
            for oid, val in varBindTableRow:
                (symName, modName), indices = cmdgen.mibvar.oidToMibName(
                    cmdGen.mibViewController, oid
                    )
                val = cmdgen.mibvar.cloneFromMibValue(
                              cmdGen.mibViewController, modName, symName, val
                      )
                index = int(string.join(map(lambda v: v.prettyPrint(), indices), '.'))
                value = val.prettyPrint()
                print SNMPObject._make([modName, symName, index, value])

When I run that, I now get...

SNMPObject(modName='RFC1213-MIB', symName='ifDescr', index=1, value="'Unit: 1 Slot: 0 Port: 1 10G - Level'")
SNMPObject(modName='RFC1213-MIB', symName='ifDescr', index=2, value="'Unit: 1 Slot: 0 Port: 2 10G - Level'")
SNMPObject(modName='RFC1213-MIB', symName='ifDescr', index=3, value="'Unit: 1 Slot: 0 Port: 3 10G - Level'")
SNMPObject(modName='RFC1213-MIB', symName='ifDescr', index=4, value="'Unit: 1 Slot: 0 Port: 4 10G - Level'")
SNMPObject(modName='RFC1213-MIB', symName='ifDescr', index=5, value="'Unit: 1 Slot: 0 Port: 5 10G - Level'")
SNMPObject(modName='RFC1213-MIB', symName='ifDescr', index=6, value="'Unit: 1 Slot: 0 Port: 6 10G - Level'")
SNMPObject(modName='RFC1213-MIB', symName='ifDescr', index=7, value="'Unit: 1 Slot: 0 Port: 7 10G - Level'")
SNMPObject(modName='RFC1213-MIB', symName='ifDescr', index=8, value="'Unit: 1 Slot: 0 Port: 8 10G - Level'")
SNMPObject(modName='RFC1213-MIB', symName='ifDescr', index=9, value="'Unit: 1 Slot: 0 Port: 9 10G - Level'")
SNMPObject(modName='RFC1213-MIB', symName='ifDescr', index=10, value="'Unit: 1 Slot: 0 Port: 10 10G - Level'")
SNMPObject(modName='RFC1213-MIB', symName='ifDescr', index=11, value="'Unit: 1 Slot: 0 Port: 11 10G - Level'")
SNMPObject(modName='RFC1213-MIB', symName='ifDescr', index=12, value="'Unit: 1 Slot: 0 Port: 12 10G - Level'")
SNMPObject(modName='RFC1213-MIB', symName='ifDescr', index=13, value="'Unit: 1 Slot: 0 Port: 13 10G - Level'")
SNMPObject(modName='RFC1213-MIB', symName='ifDescr', index=14, value="'Unit: 1 Slot: 0 Port: 14 10G - Level'")
SNMPObject(modName='RFC1213-MIB', symName='ifDescr', index=15, value="'Unit: 1 Slot: 0 Port: 15 10G - Level'")
SNMPObject(modName='RFC1213-MIB', symName='ifDescr', index=16, value="'Unit: 1 Slot: 0 Port: 16 10G - Level'")
SNMPObject(modName='RFC1213-MIB', symName='ifDescr', index=17, value="'Unit: 1 Slot: 0 Port: 17 10G - Level'")
SNMPObject(modName='RFC1213-MIB', symName='ifDescr', index=18, value="'Unit: 1 Slot: 0 Port: 18 10G - Level'")
SNMPObject(modName='RFC1213-MIB', symName='ifDescr', index=19, value="'Unit: 1 Slot: 0 Port: 19 10G - Level'")
SNMPObject(modName='RFC1213-MIB', symName='ifDescr', index=20, value="'Unit: 1 Slot: 0 Port: 20 10G - Level'")
SNMPObject(modName='RFC1213-MIB', symName='ifDescr', index=21, value="'Unit: 1 Slot: 0 Port: 21 10G - Level'")
SNMPObject(modName='RFC1213-MIB', symName='ifDescr', index=22, value="'Unit: 1 Slot: 0 Port: 22 10G - Level'")
SNMPObject(modName='RFC1213-MIB', symName='ifDescr', index=23, value="'Unit: 1 Slot: 0 Port: 23 10G - Level'")
SNMPObject(modName='RFC1213-MIB', symName='ifDescr', index=24, value="'Unit: 1 Slot: 0 Port: 24 10G - Level'")
SNMPObject(modName='RFC1213-MIB', symName='ifDescr', index=25, value="' CPU Interface for Unit: 0 Slot: 5 Port: 1'")
SNMPObject(modName='RFC1213-MIB', symName='ifDescr', index=26, value="'Link Aggregate 1'")
SNMPObject(modName='RFC1213-MIB', symName='ifDescr', index=27, value="'Link Aggregate 2'")
SNMPObject(modName='RFC1213-MIB', symName='ifDescr', index=28, value="'Link Aggregate 3'")
SNMPObject(modName='RFC1213-MIB', symName='ifDescr', index=29, value="'Link Aggregate 4'")
SNMPObject(modName='RFC1213-MIB', symName='ifDescr', index=30, value="'Link Aggregate 5'")
SNMPObject(modName='RFC1213-MIB', symName='ifDescr', index=31, value="'Link Aggregate 6'")
SNMPObject(modName='RFC1213-MIB', symName='ifDescr', index=32, value="'Link Aggregate 7'")
SNMPObject(modName='RFC1213-MIB', symName='ifDescr', index=33, value="'Link Aggregate 8'")
SNMPObject(modName='RFC1213-MIB', symName='ifDescr', index=34, value="'Link Aggregate 9'")
SNMPObject(modName='RFC1213-MIB', symName='ifDescr', index=35, value="'Link Aggregate 10'")
SNMPObject(modName='RFC1213-MIB', symName='ifDescr', index=36, value="'Link Aggregate 11'")
SNMPObject(modName='RFC1213-MIB', symName='ifDescr', index=37, value="'Link Aggregate 12'")
SNMPObject(modName='RFC1213-MIB', symName='ifDescr', index=38, value="'Vl21'")
SNMPObject(modName='RFC1213-MIB', symName='ifDescr', index=39, value="'Vl22'")

This doesn't answer my exact question about ObjectName instances with symbolic names; however, it's certainly good enough.


Solution

  • Generally speaking, to convert between OIDs and symbolic names you are to use a MIB viewer component.

    When you use pysnmp "oneliner" API, this conversion is invoked implicitly for your input OIDs/symbols. However it needs to be performed explicitly for your output OIDs (and possibly for values as well).

    See the following example for a recipe:

    https://docs.lextudio.com/pysnmp/