简体   繁体   English

使用自定义MIB与pysnmp

[英]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. 我是python编程的新手,目前正在开展一个项目,以了解有关SNMP和MIB的更多信息。 I am programming using Eclipse and pyDev with the pysnmp framework. 我正在使用带有pysnmp框架的Eclipse和pyDev进行编程。 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. 我从创建一个小型应用程序开始,该应用程序将获取单个设备的CPU温度。 I know that my code is working as it successfully gets other values from pre-installed MIB's (things like sysName and sysUpTime). 我知道我的代码可以正常运行,因为它可以从预装的MIB(例如sysName和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: 我使用build-pysnmp-mib将LM-SENSORS-MIB文件转换为.py扩展名,并将其包含在我的项目中,并使用以下代码将该MIB加载到搜索路径中:

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 当我继续执行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()
     )

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. 正如我所说的,我知道我的代码适用于预安装的MIB的功能,所以我知道问题出在MIB本身或我调用它的方式上。 If anyone has any ideas or needs more information. 如果有人有任何想法或需要更多信息。 Please let me know. 请告诉我。

Thanks! 谢谢!

EDIT: 编辑:

LM-SENSORS-MIB.py output: 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)

It looks like your MIB conversion was unsuccessful what ultimately produced an empty .py MIB module. 看来您的MIB转换未成功,最终产生了一个空的.py MIB模块。 Frequent cause of this is other MIB modules, required by LM-SENSORS-MIB.mib, were not found by smidump tool. 造成这种情况的常见原因是smidump工具未找到LM-SENSORS-MIB.mib所需的其他MIB模块。 So look at LM-SENSORS-MIB.mib and make sure you have all referred MIBs in smidump search path. 因此,请查看LM-SENSORS-MIB.mib并确保您已在middump搜索路径中引用了所有MIB。

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. shell脚本以了解如何调用smidump,然后尝试在不带'-k'选项且可见stderr的情况下重新调用它。

Also it's advisable to have a fresh libsmi/smidump package installed. 另外,建议安装新的libsmi / smidump软件包。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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