fileinsert
fileinsert

Reputation: 430

PySNMP adding custom MIBs

I've been trying to get custom MIBs working with no success.

I would ideally like to get the full list of MIBs from the ASN1 source, but with this failing I've tried multiple options.

I've downloaded IF-MIB and IANAifType-MIB and copied both the text based and PySNMP format compiled version but none appear to be read. The .py files are not being compiled.

2018-08-21 21:14:18,442 pysmi: running pysmi version 0.3.1
2018-08-21 21:14:18,442 pysmi: debug category 'searcher' enabled
2018-08-21 21:14:18,442 pysmi: debug category 'reader' enabled
2018-08-21 21:14:18,442 pysmi: debug category 'compiler' enabled
2018-08-21 21:14:19,595 pysmi: current MIB source(s): FileReader{"/usr/share/snmp/mibs"}, FileReader{"/usr/share/mibs"}
2018-08-21 21:14:19,595 pysmi: current compiled MIBs location(s): StubSearcher
2018-08-21 21:14:19,595 pysmi: current compiled MIBs location(s): StubSearcher, PyPackageSearcher{"/usr/local/lib/python3.5/dist-packages/pysnmp/smi/mibs"}, PyPackageSearcher{"/usr/local/lib/python3.5/dist-packages/pysnmp/smi/mibs/instances"}, PyPackageSearcher{"pysnmp_mibs"}
2018-08-21 21:14:19,595 pysmi: current MIB borrower(s): 
2018-08-21 21:14:20,343 pysmi: current MIB source(s): FileReader{"/usr/share/snmp/mibs"}, FileReader{"/usr/share/mibs"}
2018-08-21 21:14:20,343 pysmi: current compiled MIBs location(s): StubSearcher
2018-08-21 21:14:20,343 pysmi: current compiled MIBs location(s): StubSearcher, PyPackageSearcher{"/usr/local/lib/python3.5/dist-packages/pysnmp/smi/mibs"}, PyPackageSearcher{"/usr/local/lib/python3.5/dist-packages/pysnmp/smi/mibs/instances"}, PyPackageSearcher{"pysnmp_mibs"}
2018-08-21 21:14:20,343 pysmi: current MIB borrower(s): 
2018-08-21 21:14:22,527 pysmi: current MIB source(s): FileReader{"/usr/share/snmp/mibs"}, FileReader{"/usr/share/mibs"}
2018-08-21 21:14:22,527 pysmi: current compiled MIBs location(s): StubSearcher
2018-08-21 21:14:22,527 pysmi: current compiled MIBs location(s): StubSearcher, PyPackageSearcher{"/usr/local/lib/python3.5/dist-packages/pysnmp/smi/mibs"}, PyPackageSearcher{"/usr/local/lib/python3.5/dist-packages/pysnmp/smi/mibs/instances"}, PyPackageSearcher{"pysnmp_mibs"}
2018-08-21 21:14:22,527 pysmi: current MIB borrower(s): 


:~$ ls /usr/share/snmp/mibs/I*
/usr/share/snmp/mibs/IANA-ADDRESS-FAMILY-NUMBERS-MIB.txt
/usr/share/snmp/mibs/IANAifType-MIB
/usr/share/snmp/mibs/IANAifType-MIB.py
/usr/share/snmp/mibs/IANAifType-MIB.txt
/usr/share/snmp/mibs/IANA-LANGUAGE-MIB.txt
/usr/share/snmp/mibs/IANA-RTPROTO-MIB.txt
/usr/share/snmp/mibs/IF-MIB
/usr/share/snmp/mibs/IF-MIB.py

:~$ ls /usr/local/lib/python3.5/dist-packages/pysnmp/smi/mibs/I*     
/usr/local/lib/python3.5/dist-packages/pysnmp/smi/mibs/IANAifType-MIB.py
/usr/local/lib/python3.5/dist-packages/pysnmp/smi/mibs/IF-MIB.py
/usr/local/lib/python3.5/dist-packages/pysnmp/smi/mibs/INET-ADDRESS-MIB.py

:~$ ls /usr/local/lib/python3.5/dist-packages/pysnmp/smi/mibs/__pycache__/I*
/usr/local/lib/python3.5/dist-packages/pysnmp/smi/mibs/__pycache__/INET-ADDRESS-MIB.cpython-35.pyc

I've tried to import from the snmplabs site using the following:

from pysnmp.smi import builder 

# load mibs
MIBDIR = 'http://mibs.snmplabs.com/asn1/@mib@'
mibBuilder = builder.MibBuilder()
mibSources = mibBuilder.getMibSources() + (builder.DirMibSource(MIBDIR),)
mibBuilder.setMibSources(*mibSources)

I have also tried appending .addAsn1MibSource('http://mibs.snmplabs.com/asn1/@mib@') to the end of my OIDs on the cmdgen.

I still get output like SNMPv2-SMI::mib-2.2.2.1.2.27 = Ethernet0/1/0.101 instead of IF-MIB::ifDescr.27 = STRING: Ethernet0/1/0.101. What else can I do to figure out what I'm doing wrong?

Upvotes: 0

Views: 3954

Answers (2)

rozi
rozi

Reputation: 71

@Ilya Etingof

Sorry could not reply on comment due to not enough reputation ................ could you please provide an example to the answer above. I am constrantly getting

pysnmp.smi.error.SmiError: ObjectType object not fully initialized

for

ObjectType(ObjectIdentity('1.3.6.1.2.1.25.2.3').loadMibs('HOST-RESOURCES-MIB'))

The steps I have followed so far is


from pysnmp.hlapi import ObjectIdentity, ObjectType


mibBuilder = builder.MibBuilder()
mibBuilder.addMibSources(builder.DirMibSource('C://Users//Downloads//HOST-RESOURCES-MIB.txt'))

errorIndication, errorStatus, errorIndex, \
varBindTable = cmdgen.CommandGenerator().bulkCmd(
    cmdgen.CommunityData('test'),
    cmdgen.UdpTransportTarget(('test-ip', 161)),
    0,
    1,
    ObjectType(ObjectIdentity('1.3.6.1.2.1.25.2.3').loadMibs('HOST-RESOURCES-MIB'))
)

if errorIndication:
   print(errorIndication)
else:
    if errorStatus:
        print('%s at %s\n' % (
            errorStatus.prettyPrint(),
            errorIndex and varBindTable[-1][int(errorIndex)-1] or '?'
            ))
    else:
        for varBindTableRow in varBindTable:
            for name, val in varBindTableRow:
                print('%s = %s' % (name.prettyPrint(), val.prettyPrint()))

Upvotes: 1

Ilya Etingof
Ilya Etingof

Reputation: 5555

Try appending .loadMibs('IF-MIB', ...) to the ObjectType object you pass to cmdgen. The thing is that if you do your query without referencing the MIB explicitly (e.g. use OID), pysnmp won't know which MIB should be loaded to resolve response variables.

If your custom MIB is a well-formed ASN.1 document, you do not need to compile it explicitly into pysnmp - pysnmp will call pysmi to compile the MIB for you.

UPDATE:

The MIBs are loaded just once on the first .loadMibs() call and remain in memory since then. So, technically, you do not need to repeat the .loadMibs() call on every iteration.

One way to avoid explicit .loadMibs() is to pass managed object in the query as a MIB & symbol pair rather than OID. That will trigger implicit MIB load.

But that won't work for reverse look up e.g. if you walk SNMP agent and eventually receive OIDs that belong to some other MIB you've never loaded. For that case .loadMibs() call is still advised.

Upvotes: 1

Related Questions