pythonpysnmpmibsnmp-trap

How to load a custom MIB


I have been trying to create a trap listener using the PySNMP library, that should load a custom MIB file (already compiled for PySNMP to use with mibdump.py), and print the traps received in a readable format of field name = value.

However, the code is unable to find the MIB files and returns an error:

pysnmp.smi.error.MibNotFoundError: MIB file "BMC-CONTROLMEM-MIB.py[co]" not found in search path (DirMibSource('/home/ign
asi/Workspace/trap-endpoint/.venv/lib/python3.7/site-packages/pysnmp/smi/mibs'), 
DirMibSource('/home/ignasi/Workspace/trap-endpoint/.venv/lib/python3.7/site-packages/pysnmp/smi/mibs/instances'),
DirMibSource('/home/ignasi/Workspace/trap-endpo
int/.venv/lib/python3.7/site-packages/pysnmp_mibs'),
DirMibSource('/home/ignasi/.pysnmp/mibs'), 
DirMibSource('python_packaged_mibs'), 
DirMibSource('/home/ignasi/.pysnmp/mibs')) 

I started by following the examples (this), however when I point to my MIB:

for (errorIndication,
        errorStatus,
        errorIndex,
        varBinds) in bulkCmd(SnmpEngine(),
                            UsmUserData('usr-none-none'),
                            UdpTransportTarget(('localhost', 5000)),
                            ContextData(),
                            0, 50,
                            ObjectType(
                                ObjectIdentity('BMC-CONTROLMEM-MIB', 'controlmAlert').addMibSource('/home/ignasi/.pysnmp/mibs').addMibSource('python_packaged_mibs')
                            ),
                            lexicographicMode=False):

It returns that error.

I have tried to list all paths as explained here and copy the MIB to all folders with no success.

I have also read this post and I do not understand how to generate the MIB in the /instances folder, with the "__" prefix.

Lastly, I do not understand why is searching for a .py[co] file, is it something that must be also compiled?

...modName and modName + "***.py[co]***", ', '.join([str(x) for x in self.__mibSources]))
pysnmp.smi.error.MibNotFoundError: MIB file "BMC-CONTROLMEM-MIB.***py[co]***"

Is there something I missed?

Thanks.


Solution

  • You do not need to compile MIBs explicitly - that would happen behind the scenes.

    If you are building a trap listener (notification receiver), you will get hold of the variable-bindings it received. I am guessing that at this point you should walk over them and resolve against the MIB like this:

    varBinds = [ObjectType(ObjectIdentity(*varBind).resolveWithMib(mibViewController)
                for varBind in varBinds]
    

    Prior to resolving varBinds, you might need to pre-load the MIBs you intend to receive the OIDs for. Prior to that you are likely to want to point pysnmp to a location holding all these MIBs and their dependencies.