Python HOST-RESOURCES-MIB中的pysnmp轮询特定进程索引
我有下面的代码,我一直在使用pysnmp进行轮询。到目前为止,它一直被用来走路,但我希望能够得到一个具体的索引。例如,我想轮询Python HOST-RESOURCES-MIB中的pysnmp轮询特定进程索引,python,snmp,pysnmp,Python,Snmp,Pysnmp,我有下面的代码,我一直在使用pysnmp进行轮询。到目前为止,它一直被用来走路,但我希望能够得到一个具体的索引。例如,我想轮询HOST-RESOURCES-MIB::hrSWRunPerfMem.999 我可以使用getCounter('1.1.1.1','public','HOST-RESOURCES-MIB','hrSWRunPerfMem')成功地获取hrSWRunPerfMem中的所有内容。 但是,一旦我尝试包含索引编号getCounter('1.1.1.1','public','HOS
HOST-RESOURCES-MIB::hrSWRunPerfMem.999
我可以使用getCounter('1.1.1.1','public','HOST-RESOURCES-MIB','hrSWRunPerfMem')成功地获取hrSWRunPerfMem中的所有内容。
但是,一旦我尝试包含索引编号getCounter('1.1.1.1','public','HOST-RESOURCES-MIB','hrswrunperfm',indexNum=999)
我总是得到varBindTable=[]
from pysnmp.entity.rfc3413.oneliner import cmdgen
from pysnmp.smi import builder, view
def getCounter(ip, community, mibName, counterName, indexNum=None):
cmdGen = cmdgen.CommandGenerator()
mibBuilder = cmdGen.mibViewController.mibBuilder
mibPath = mibBuilder.getMibSources() + (builder.DirMibSource("/path/to/mibs"),)
mibBuilder.setMibSources(*mibPath)
mibBuilder.loadModules(mibName)
mibView = view.MibViewController(mibBuilder)
retList = []
if indexNum is not None:
mibVariable = cmdgen.MibVariable(mibName, counterName, int(indexNum))
else:
mibVariable = cmdgen.MibVariable(mibName, counterName)
errorIndication, errorStatus, errorIndex, varBindTable = cmdGen.nextCmd(cmdgen.CommunityData('test-agent', community),
cmdgen.UdpTransportTarget((ip, snmpPort)),
mibVariable)
有人知道如何使用pysnmp轮询特定索引吗?您应该使用cmdGen.getCmd()调用而不是nextCmd()调用。没有“下一个”OID超过叶1,因此响应为空 这里有一个稍微优化的代码版本。它应该在Python提示符下按原样运行:
from pysnmp.entity.rfc3413.oneliner import cmdgen
def getCounter(ip, community, mibName, counterName, indexNum=None):
if indexNum is not None:
mibVariable = cmdgen.MibVariable(mibName, counterName, int(indexNum))
else:
mibVariable = cmdgen.MibVariable(mibName, counterName)
cmdGen = cmdgen.CommandGenerator()
errorIndication, errorStatus, errorIndex, varBindTable = cmdGen.getCmd(
cmdgen.CommunityData(community),
cmdgen.UdpTransportTarget((ip, 161)),
mibVariable.addMibSource("/path/to/mibs")
)
if not errorIndication and not errorStatus:
return varBindTable
#from pysnmp import debug
#debug.setLogger(debug.Debug('msgproc'))
print(getCounter('demo.snmplabs.com',
'recorded/linux-full-walk',
'HOST-RESOURCES-MIB',
'hrSWRunPerfMem',
970))
就性能而言,建议重用CommandGenerator实例以节省引擎盖下发生的[heavy]snmpEngine初始化。就是这样!谢谢在你最后的建议中,我也修改了一些代码,事情似乎运行得更顺利了。