使用自定义MIB与pysnmp

时间:2014-05-29 23:50:53

标签: python snmp mib pysnmp

我是python编程的新手,目前正着手开展一个项目,以了解有关SNMP和MIB的更多信息。我正在使用Eclipse和pyDev使用pysnmp框架进行编程。之前使用过这个的人都会知道文档并不详尽。

关于我的问题!

我已经开始创建一个小型应用程序,它将获得单个设备的CPU温度。我知道我的代码正在运行,因为它成功地从预安装的MIB(sysName和sysUpTime之类的东西)中获取了其他值。我使用build-pysnmp-mib将LM-SENSORS-MIB文件转换为.py扩展名,我将其包含在我的项目中,并使用以下代码将此MIB加载到搜索路径中:

mibBuilder - cmdGen.snmpEngine.msgAndPduDsp.mibInstrumController.mibBuilder

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

mibBuilder.setMibSources(*mibSources)

当我继续执行snmp查询时,我使用

   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()
     )

我收到错误

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']

正如我所说,我知道我的代码适用于预先安装的MIB的功能,所以我知道问题在于MIB本身或我调用它的方式。如果有人有任何想法或需要更多信息。请告诉我。

谢谢!

编辑:

LM-SENSORS-MIB.py输出:

# 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)

1 个答案:

答案 0 :(得分:0)

看起来你的MIB转换不成功,最终产生了一个空的.py MIB模块。经常导致这种情况的原因是smidump工具找不到LM-SENSORS-MIB.mib所需的其他MIB模块。所以请看LM-SENSORS-MIB.mib并确保在smidump搜索路径中都有所有引用的MIB。

要进一步调试此问题,请尝试运行:

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

shell脚本,用于查看如何调用smidump,然后尝试重新调用它,而不使用&#39; -k&#39;选项和stderr可见。

同样建议安装一个新的libsmi / smidump软件包。