简体   繁体   中英

using custom MIB's with pysnmp

I am new to python programming and am currently embarking on a project to learn more about SNMP and MIB's. I am programming using Eclipse and pyDev with the pysnmp framework. Anyone who has used this before will know that the documentation is not exhaustive.

On to my questions!

I have started off by creating a small application that will get the CPU temperature for a single device. I know that my code is working as it successfully gets other values from pre-installed MIB's (things like sysName and sysUpTime). I have used build-pysnmp-mib to convert the LM-SENSORS-MIB file into the .py extension and i have included this in my project and have used the following code to load this MIB into the search path:

mibBuilder - cmdGen.snmpEngine.msgAndPduDsp.mibInstrumController.mibBuilder

mibSources = mibBuilder.getMibSources() + (
   builder.DirMidSource('/path/'),
   )

mibBuilder.setMibSources(*mibSources)

when i then go on to performing the snmp query i use

   errorIndication, errorStatus, errorIndex, varBinds = cmdGen.getCmd(
        cmdgen.CommunityData('public'),
        cmdgen.UdpTransportTarget((self.getIp(), 161)),
        cmdgen.MibVariable('1.3.6.1.4.1.2021.13.16.2.1.3.2').loadMibs()
     )

i get the error

Traceback (most recent call last):
File "/home/adam/workspace/MSc Project/Interface.py", line 20, in <module>
print(temp.getTemperature())
File "/home/adam/workspace/MSc Project/Classes/Device.py", line 34, in getTemperature
cmdgen.MibVariable('1.3.6.1.4.1.2021.13.16.2.1.3.2').loadMibs()
File "/usr/lib/python2.7/dist-packages/pysnmp/entity/rfc3413/oneliner/cmdgen.py", line   424, in getCmd
kwargs.get('contextName', null)
File "/usr/lib/python2.7/dist-packages/pysnmp/entity/rfc3413/oneliner/cmdgen.py", line 277, in getCmd
self.makeReadVarBinds(varNames),
File "/usr/lib/python2.7/dist-packages/pysnmp/entity/rfc3413/oneliner/cmdgen.py", line 201, in makeReadVarBinds
[ (x, self._null) for x in varNames ], oidOnly=True
File "/usr/lib/python2.7/dist-packages/pysnmp/entity/rfc3413/oneliner/cmdgen.py", line 209, in makeVarBinds
varName.resolveWithMib(self.mibViewController, oidOnly=True)
File "/usr/lib/python2.7/dist-packages/pysnmp/entity/rfc3413/oneliner/mibvar.py", line 79, in resolveWithMib
mibViewController.mibBuilder.loadModules(*self.__modNamesToLoad)
File "/usr/lib/python2.7/dist-packages/pysnmp/smi/builder.py", line 259, in loadModules
'MIB module \"%s\" load error: %s' % (modPath,   traceback.format_exception(*sys.exc_info()))
pysnmp.smi.error.SmiError: MIB module "Custom_MIBs/LM-SENSORS-MIB.py" load error:  ['Traceback (most recent call last):\n', '  File "/usr/lib/python2.7/dist- packages/pysnmp/smi/builder.py", line 255, in loadModules\n    exec(modData, g)\n', '  File  "<string>", line 7, in <module>\n', '  File "/usr/lib/python2.7/dist- packages/pysnmp/smi/builder.py", line 294, in importSymbols\n    \'importSymbols: empty MIB  module name\'\n', 'SmiError: importSymbols: empty MIB module name\n']

As i said i know that my code works for the functions with a pre-installed MIB, so i know the issue lies with either the MIB itself or the way that i am calling upon it. If anyone has any ideas or needs more information. Please let me know.

Thanks!

EDIT:

LM-SENSORS-MIB.py output:

# PySNMP SMI module. Autogenerated from smidump -f python LM-SENSORS-MIB
# by libsmi2pysnmp-0.1.3 at Thu May 29 22:09:39 2014,
# Python version sys.version_info(major=2, minor=7, micro=7, releaselevel='candidate', serial=1)

# Imports

( DisplayString, Gauge32, Integer32, ModuleIdentity, MibScalar, MibTable, MibTableRow, MibTableColumn, ucdExperimental, ) = mibBuilder.importSymbols("", "DisplayString", "Gauge32", "Integer32", "ModuleIdentity", "MibScalar", "MibTable", "MibTableRow", "MibTableColumn", "ucdExperimental")
( Integer, ObjectIdentifier, OctetString, ) = mibBuilder.importSymbols("ASN1", "Integer", "ObjectIdentifier", "OctetString")
( NamedValues, ) = mibBuilder.importSymbols("ASN1-ENUMERATION", "NamedValues")
( ConstraintsIntersection, ConstraintsUnion, SingleValueConstraint, ValueRangeConstraint, ValueSizeConstraint, ) = mibBuilder.importSymbols("ASN1-REFINEMENT", "ConstraintsIntersection", "ConstraintsUnion", "SingleValueConstraint", "ValueRangeConstraint", "ValueSizeConstraint")
( Bits, Integer32, MibIdentifier, TimeTicks, ) = mibBuilder.importSymbols("SNMPv2-SMI", "Bits", "Integer32", "MibIdentifier", "TimeTicks")

# Exports

# Module identity
mibBuilder.exportSymbols("LM-SENSORS-MIB", PYSNMP_MODULE_ID=lmSensorsMIB)

It looks like your MIB conversion was unsuccessful what ultimately produced an empty .py MIB module. Frequent cause of this is other MIB modules, required by LM-SENSORS-MIB.mib, were not found by smidump tool. So look at LM-SENSORS-MIB.mib and make sure you have all referred MIBs in smidump search path.

To further debug this issue try running:

sh -x build-pysnmp-mib LM-SENSORS-MIB.mib

shell script to see how smidump is invoked, then try to re-invoke it without the '-k' option and with stderr visible.

Also it's advisable to have a fresh libsmi/smidump package installed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM