Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Loading uncompiled MIB files. #229

Closed
mitchell-es opened this issue Jan 19, 2019 · 10 comments
Closed

Loading uncompiled MIB files. #229

mitchell-es opened this issue Jan 19, 2019 · 10 comments

Comments

@mitchell-es
Copy link

I'm stumped on what I would think would be a simple task. The sample script "SNMP MIB browser" gives every indication that compiler.addMibCompiler(mibBuilder, sources=['/usr/share/snmp/mibs']) adds a MIB compiler to load uncompiled MIBs and that mibBuilder.addMibSources(builder.DirMibSource('/opt/pysnmp_mibs')) is to add a source containing pre-compiled MIBs.

#!/usr/bin/env python
from pysnmp import debug
from pysnmp.smi import builder, view, compiler, rfc1902, error

def main():
#    debug.setLogger(debug.Debug('all'))
    mibBuilder = builder.MibBuilder()
    compiler.addMibCompiler(mibBuilder, sources = ['file:///home/user/python-udp/MIBs', ])
    mibBuilder.loadModules('SNMPv2-SMI')
    mibBuilder.loadModules('SNMPv2-TC')
    mibBuilder.loadModules('INET-ADDRESS-MIB')
    mibBuilder.loadModules('IANAifType-MIB')
    mibViewController = view.MibViewController(mibBuilder)
    modName = mibViewController.getFirstModuleName()
    while 1:
        if modName: print(modName)
        try:
            modName = mibViewController.getNextModuleName(modName)
        except error.SmiError:
            break
    return

if __name__ == '__main__':
    main()

The loadModules() all work until the one to load IANAifType-MIB which is the first one which isn't included in pysnmp itself. At no point does it give any indication that it's looking in the directory I specified. I've tried numerous variations on the theme but cannot find anything which works. Is there a concise code snippet for loading uncompiled MIBs? Thanks in advance.

Traceback (most recent call last):
  File "./loadmib.py", line 24, in <module>
    main()
  File "./loadmib.py", line 12, in main
    mibBuilder.loadModules('IANAifType-MIB')
  File "/home/user/python-udp/venv/lib/python2.7/site-packages/pysnmp/smi/builder.py", line 388, in loadModules
    raise error.MibNotFoundError('%s compilation error(s): %s' % (modName, errs))
pysnmp.smi.error.MibNotFoundError: IANAifType-MIB compilation error(s): missingcaused by <class 'pysnmp.smi.error.MibNotFoundError'>: MIB file "IANAifType-MIB.py[co]" not found in search path (DirMibSource('/home/user/python-udp/venv/lib/python2.7/site-packages/pysnmp/smi/mibs'), DirMibSource('/home/user/python-udp/venv/lib/python2.7/site-packages/pysnmp/smi/mibs/instances'), DirMibSource('pysnmp_mibs'), DirMibSource('/home/user/.pysnmp/mibs'))caused by <type 'exceptions.IOError'>: [Errno 2] No suitable module found: 'IANAifType-MIB'
@etingof
Copy link
Owner

etingof commented Jan 19, 2019

Your code seems correct and it works for me. Try adding some pysmi debugging:

from pysmi import debug as pysmi_debug
pysmi_debug.setLogger(pysmi_debug.Debug('compiler'))

To see the search process:

2019-01-19 09:18:24,569 pysmi: trying source FileReader{"/home/user/python-udp/MIBs"}
2019-01-19 09:18:25,362 pysmi: no IANAifType-MIB found at FileReader{"/home/user/python-udp/MIBs"}
2019-01-19 09:18:25,362 pysmi: no IANAifType-MIB found everywhere
2019-01-19 09:18:25,362 pysmi: MIBs analyzed 0, MIBs failed 1

BTW, if you add this URL to the MIBs repo, all your MIBs would be resolved:

    compiler.addMibCompiler(mibBuilder, sources = [
        'file:///home/user/python-udp/MIBs',
        'http://mibs.snmplabs.com/asn1/'])

@mitchell-es
Copy link
Author

I turned on the debugging you suggested and that did let me move forward some. I still gets lots of behavior I just can't figure out. Here's a concise example. How can I load module SNMPv2-CONF, have it work, and just a few lines later have it not be available? I've been trying to make sure that ~/.pysnmp/mibs doesn't exist because I find it complicates troubleshooting. The caching means that script A might fail, I make some changes and it works (or fails differently), then the first version behaves differently then it just did. It leads to a feeling of the code being non-deterministic.

Anyway, it seems my local directory of MIBS did not have SNMPv2-CONF.MIB in it (Oddly Nokia seems to include every other dependency but that one in their zip of MIBS). If I put an uncompiled copy in there I can move ahead but quickly hit more unexplainable output. This particular MIB seems to be the first place I spot problems so I'll include it as an example.

#!/usr/bin/env python
from pysmi import debug as pysmi_debug
from pysnmp.smi import builder, view, compiler, rfc1902, error

def main():
    pysmi_debug.setLogger(pysmi_debug.Debug('compiler'))
    mibBuilder = builder.MibBuilder()
    compiler.addMibCompiler(mibBuilder, sources = ['file:///tmp/testing/python-udp/testmibs/', ])
    mibBuilder.loadModules('SNMPv2-CONF')
    mibViewController = view.MibViewController(mibBuilder)
    modName = mibViewController.getFirstModuleName()
    while 1:
        if modName: print(modName)
        try:
            modName = mibViewController.getNextModuleName(modName)
        except error.SmiError:
            break
    mibBuilder.loadModules('IANAifType-MIB')

if __name__ == '__main__':
    main()
2019-01-19 10:48:32,174 pysmi: running pysmi version 0.3.3
2019-01-19 10:48:32,174 pysmi: debug category 'compiler' enabled
2019-01-19 10:48:32,377 pysmi: current MIB source(s): FileReader{"/tmp/testing/python-udp/testmibs"}
2019-01-19 10:48:32,377 pysmi: current compiled MIBs location(s): StubSearcher
2019-01-19 10:48:32,377 pysmi: current compiled MIBs location(s): StubSearcher, PyPackageSearcher{"/tmp/testing/python-udp/venv/lib/python2.7/site-packages/pysnmp/smi/mibs"}, PyPackageSearcher{"/tmp/testing/python-udp/venv/lib/python2.7/site-packages/pysnmp/smi/mibs/instances"}, PyPackageSearcher{"pysnmp_mibs"}
2019-01-19 10:48:32,377 pysmi: current MIB borrower(s):
ASN1
ASN1-REFINEMENT
SNMPv2-CONF
SNMPv2-SMI
2019-01-19 10:48:32,386 pysmi: trying source FileReader{"/tmp/testing/python-udp/testmibs"}
2019-01-19 10:48:32,463 pysmi: IANAifType-MIB (IANAifType-MIB) read from file:///tmp/testing/python-udp/testmibs/IANAifType-MIB.mib, immediate dependencies: SNMPv2-CONF, SNMPv2-SMI, SNMPv2-TC
2019-01-19 10:48:32,463 pysmi: trying source FileReader{"/tmp/testing/python-udp/testmibs"}
2019-01-19 10:48:32,465 pysmi: no SNMPv2-CONF found at FileReader{"/tmp/testing/python-udp/testmibs"}
2019-01-19 10:48:32,465 pysmi: no SNMPv2-CONF found everywhere
2019-01-19 10:48:32,465 pysmi: trying source FileReader{"/tmp/testing/python-udp/testmibs"}
2019-01-19 10:48:32,467 pysmi: no SNMPv2-SMI found at FileReader{"/tmp/testing/python-udp/testmibs"}
2019-01-19 10:48:32,468 pysmi: no SNMPv2-SMI found everywhere
2019-01-19 10:48:32,468 pysmi: trying source FileReader{"/tmp/testing/python-udp/testmibs"}
2019-01-19 10:48:32,469 pysmi: no SNMPv2-TC found at FileReader{"/tmp/testing/python-udp/testmibs"}
2019-01-19 10:48:32,470 pysmi: no SNMPv2-TC found everywhere
2019-01-19 10:48:32,470 pysmi: MIBs analyzed 1, MIBs failed 3
2019-01-19 10:48:32,470 pysmi: checking if IANAifType-MIB requires updating
2019-01-19 10:48:32,470 pysmi: no compiled MIB IANAifType-MIB available through StubSearcher
2019-01-19 10:48:32,471 pysmi: no compiled MIB IANAifType-MIB available through PyPackageSearcher{"/tmp/testing/python-udp/venv/lib/python2.7/site-packages/pysnmp/smi/mibs"}
2019-01-19 10:48:32,471 pysmi: no compiled MIB IANAifType-MIB available through PyPackageSearcher{"/tmp/testing/python-udp/venv/lib/python2.7/site-packages/pysnmp/smi/mibs/instances"}
2019-01-19 10:48:32,471 pysmi: no compiled MIB IANAifType-MIB available through PyPackageSearcher{"pysnmp_mibs"}
2019-01-19 10:48:32,472 pysmi: no suitable compiled MIB IANAifType-MIB found anywhere
2019-01-19 10:48:32,473 pysmi: MIBs parsed 1, MIBs failed 3
2019-01-19 10:48:32,473 pysmi: compiling IANAifType-MIB read from file:///tmp/testing/python-udp/testmibs/IANAifType-MIB.mib
2019-01-19 10:48:32,486 pysmi: error from <pysmi.codegen.pysnmp.PySnmpCodeGen object at 0x7f5e53a44f90>: no module "SNMPv2-SMI" in symbolTable at MIB IANAifType-MIB
2019-01-19 10:48:32,486 pysmi: MIBs built 0, MIBs failed 4
2019-01-19 10:48:32,486 pysmi: MIBs available for borrowing 0, MIBs failed 4
2019-01-19 10:48:32,487 pysmi: MIBs built 0, MIBs failed 4
2019-01-19 10:48:32,487 pysmi: failing with problem MIBs SNMPv2-CONF, SNMPv2-SMI, SNMPv2-TC, IANAifType-MIB
Traceback (most recent call last):
  File "./loadmib.py", line 21, in <module>
    main()
  File "./loadmib.py", line 18, in main
    mibBuilder.loadModules('IANAifType-MIB')
  File "/tmp/testing/python-udp/venv/lib/python2.7/site-packages/pysnmp/smi/builder.py", line 388, in loadModules
    raise error.MibNotFoundError('%s compilation error(s): %s' % (modName, errs))
pysnmp.smi.error.MibNotFoundError: IANAifType-MIB compilation error(s): missing; missing; missing; no module "SNMPv2-SMI" in symbolTable at MIB IANAifType-MIBcaused by <class 'pysnmp.smi.error.MibNotFoundError'>: MIB file "IANAifType-MIB.py[co]" not found in search path (DirMibSource('/tmp/testing/python-udp/venv/lib/python2.7/site-packages/pysnmp/smi/mibs'), DirMibSource('/tmp/testing/python-udp/venv/lib/python2.7/site-packages/pysnmp/smi/mibs/instances'), DirMibSource('pysnmp_mibs'), DirMibSource('/home/user/.pysnmp/mibs'))caused by <type 'exceptions.IOError'>: [Errno 2] No suitable module found: 'IANAifType-MIB'

I appreciate that you have an extensive collection of MIBs available but it's important for me to understand how to get local MIBs loaded properly since I expect that I will need versions of MIBs which you don't yet have available. For example, I'm using rev 16.0 of TIMETRA-CHASSIS-MIB since I'm getting some traps which were added in recent firmware versions.

@etingof
Copy link
Owner

etingof commented Jan 19, 2019

Do you also have the SNMPv2-SMI MIB in your local collection?

Generally, to compile a MIB you have to have all the MIBs it imports (and the MIBs they import and so forth). In other words, you have to have all the MIBs in the dependency chain in uncompiled form.

@mitchell-es
Copy link
Author

I did not have that MIB in the local collection. I knew there was a pre-compiled version included in pysnmp so I was confused as to why it wasn't being used. Knowing that only uncompiled MIBs can be used for the compilation process helps clear up a lot of my confusion. Thanks for explaining that.

I was also getting stuck on a couple of things which seem to be unique to the vendor MIBs I'm looking at. One, neither Juniper nor Nokia seem to include SNMPv2-CONF in their packages of "all" the MIBs they need so I had to grab that since the precompiled one doesn't help. Second, Nokia is including a version of SNMPv2-SMI which has most of the basic type definitions commented out which lead to head-scratching errors like pysnmp.smi.error.MibNotFoundError: TIMETRA-CHASSIS-MIB compilation error(s): no symbol "Unsigned32" in module "SNMPv2-SMI" at MIB TIMETRA-CHASSIS-MIB. I've had to replace that with a more correct version of that MIB.

I am still having some compilation issues but I think those are specific to the vendor MIBs rather than my understanding of how to use the pysnmp libraries. If I can't get them worked out I will open a separate issue. I think this issue can be considered resolved. Thanks again for your help!

@etingof
Copy link
Owner

etingof commented Jan 20, 2019

Good to know you are progressing!

Yeah, MIBs are frequently broken in one way or the other... We had to implement a bunch of exceptions in the SMI grammar in the pysmi library to work around those weird nuisances.

@etingof etingof closed this as completed Jan 20, 2019
@f1r35pirit
Copy link

Just a wish: it would be very helpful if the exception contained the actual error reason, e.g. "Couldn't find dependency WHATEVER-MIB for SOME-MIB" or "Error in SOME-MIB", etc. The currently reported "file not found" is extremely confusing.

I'm facing a similar problem now with pysnmp 7.1.8 and this simple program:

from pysnmp.hlapi import *                                                                                                                                                                                                                                                                                                    
from pysnmp.smi import builder, view, compiler, rfc1902                                                                                                                                                                                                                                                                       
from pysnmp import debug                                                                                                                                                                                                                                                                                                      
debug.set_logger(debug.Debug('all'))                                                                                                                                                                                                                                                                                          
mibBuilder = builder.MibBuilder()                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
mibViewController = view.MibViewController(mibBuilder)                                                                                                                                                                                                                                                                        
test = ObjectIdentity('ATEN-PE-CFG', 'outlet23Status', 0).add_asn1_mib_source('/opt/conserv-tools-venv/usr/share/snmp/mibs', '/usr/share/snmp/mibs',)                                                                                                                                                                         
test.resolve_with_mib(mibViewController)                                                                                                                                                                                                                                                                                      
test.getOid()                                                                       

I used strace and found out that it DOES succesfully open the ATEN-PE-CFG.txt file and reads it, but then it still reports this:

2024-10-31 20:10:48,355 pysnmp: loadModules: calling MIB compiler for ATEN-PE-CFG
Traceback (most recent call last):
  File "/opt/conserv-tools-venv/lib/python3.12/site-packages/pysnmp/smi/builder.py", line 398, in load_modules
    self.load_module(modName, **userCtx)
  File "/opt/conserv-tools-venv/lib/python3.12/site-packages/pysnmp/smi/builder.py", line 374, in load_module
    raise error.MibNotFoundError(
pysnmp.smi.error.MibNotFoundError: MIB file "ATEN-PE-CFG.py[co]" not found in search path (DirMibSource('/opt/conserv-tools-venv/lib/python3.12/site-packages/pysnmp/smi/mibs'), DirMibSource('/opt/conserv-tools-venv/lib/python3.12/site-packages/pysnmp/smi/mibs/instances'), DirMibSource('pysnmp_mibs'), DirMibSource('/h
ome/amelkin/.pysnmp/mibs'), DirMibSource('/home/amelkin/.pysnmp/mibs'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/amelkin/conserv-tools/./snmpv3.py", line 47, in <module>
    test.resolve_with_mib(mibViewController)
  File "/opt/conserv-tools-venv/lib/python3.12/site-packages/pysnmp/smi/rfc1902.py", line 542, in resolve_with_mib
    (mibNode,) = mibViewController.mibBuilder.import_symbols(
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/conserv-tools-venv/lib/python3.12/site-packages/pysnmp/smi/builder.py", line 454, in import_symbols
    self.load_modules(modName, **userCtx)
  File "/opt/conserv-tools-venv/lib/python3.12/site-packages/pysnmp/smi/builder.py", line 416, in load_modules
    raise error.MibNotFoundError(
pysnmp.smi.error.MibNotFoundError: ATEN-PE-CFG compilation error(s): missing; missing caused by <class 'pysnmp.smi.error.MibNotFoundError'>: MIB file "ATEN-PE-CFG.py[co]" not found in search path (DirMibSource('/opt/conserv-tools-venv/lib/python3.12/site-packages/pysnmp/smi/mibs'), DirMibSource('/opt/conserv-tools-ve
nv/lib/python3.12/site-packages/pysnmp/smi/mibs/instances'), DirMibSource('pysnmp_mibs'), DirMibSource('/home/amelkin/.pysnmp/mibs'), DirMibSource('/home/amelkin/.pysnmp/mibs'))

I'm currently quite stuck. Any help would be appreciated. Thanks.
Full log is attached: pysnmp.log

P.S. Do you know why latest Ubuntu 24.04 LTS still has pysnmp 4.4.12 ? Is there any place where documentation for that extremely outdated version can still be found? I failed to google it.

@f1r35pirit
Copy link

Oh, almost as soon as posted the comment, I have found out the reason it was failing. The MIB I was loading was demanding RFC-1212 and RFC-1215 MIBs that weren't installed by Ubuntu's snmp-mibs-downloader and weren't provided by Aten along with their ATEN-PE-CFG MIB.

Nonetheless, I was able to find that out only using strace because pysnmp or pysmi didn't give a slightest hint regarding what was wrong and kept reporting 'file not found' about the main MIB file that clearly was present.

Clearer error messages could save me a day of debuggng.

@lextm
Copy link

lextm commented Oct 31, 2024

@f1r35pirit this repo is abandoned, #429, so if you wish the error message to be clearer, this isn't the place to report.

@kdarjoshi
Copy link

i was facing the similar issue that my private mib is not compile.
though inside that mib there is dependency
error from <pysmi.codegen.pysnmp.PySnmpCodeGen object at 0x7aa96ec9d900>: no symbol "Unsigned32" in module "SNMPv2-SMI" at MIB MY-MIB

how do i resolve the issue of "Unsigned32"?

@lextm
Copy link

lextm commented Dec 9, 2024

@kdarjoshi That indicates you are using very wrong versions of PySNMP and PySMI. Please read #429 to learn where to get started.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants