我试图使用opennms监视python进程。为此,我需要实现一个支持HOST-RESOURCES-MIB的代理。 Opennms通过检查HOST-RESOURCES-MIB的hrSwRunTable来检查进程的状态。测试是通过将给定进程与hrSwRunName与hrSwRunState的数值进行匹配来完成的。
pysnmp给出了一些编写我试图修改的代理的例子,但是我没有取得多大成功。
我的代码的相关部分如下
import logging
from pysnmp import debug
from pysnmp.carrier.asyncore.dgram import udp
from pysnmp.entity import engine, config
from pysnmp.entity.rfc3413 import cmdrsp, context
from pysnmp.proto.api import v2c
from pysnmp.smi import builder, instrum, exval
debug.setLogger(debug.Debug('all'))
formatting = '[%(asctime)s-%(levelname)s]-(%(module)s) %(message)s'
logging.basicConfig(level=logging.DEBUG, format=formatting, )
logging.info("Starting....")
# Create SNMP engine
snmpEngine = engine.SnmpEngine()
# Transport setup
# UDP over IPv4
config.addTransport(
snmpEngine,
udp.domainName,
udp.UdpTransport().openServerMode(('mypc', 12345))
)
# SNMPv2c setup
# SecurityName <-> CommunityName mapping.
config.addV1System(snmpEngine, 'my-area', 'public')
# Allow read MIB access for this user / securityModels at VACM
config.addVacmUser(snmpEngine, 2, 'my-area', 'noAuthNoPriv', (1, 3, 6, 1, 2, 1, 25, 4, 2), (1, 3, 6, 1, 2, 1, 25, 4, 2))
# Create an SNMP context
snmpContext = context.SnmpContext(snmpEngine)
# --- define custom SNMP Table within a newly defined EXAMPLE-MIB ---
# ==================================================================
logging.debug('Loading SNMP-TARGET-MIB module...'),
mibBuilder1 = builder.MibBuilder().loadModules('SNMP-TARGET-MIB')
logging.debug('done')
logging.debug('Building MIB tree...'),
mibInstrum1 = instrum.MibInstrumController(mibBuilder1)
logging.debug('done')
logging.debug('Building table entry index from human-friendly representation...')
snmpTargetAddrEntry, = mibBuilder1.importSymbols('SNMP-TARGET-MIB', 'snmpTargetAddrEntry')
instanceId1 = snmpTargetAddrEntry.getInstIdFromIndices('my-area')
# ==================================================================
logging.debug('Loading HOST-RESOURCES-MIB module...'),
mibBuilder = builder.MibBuilder().loadModules('HOST-RESOURCES-MIB')
logging.debug('done')
logging.debug('Building MIB tree...'),
mibInstrum = instrum.MibInstrumController(mibBuilder)
logging.debug('done')
logging.debug('Building table entry index from human-friendly representation...')
# see http://www.oidview.com/mibs/0/HOST-RESOURCES-MIB.html
hostRunTable, = mibBuilder.importSymbols('HOST-RESOURCES-MIB', 'hrSWRunEntry')
instanceId = hostRunTable.getInstIdFromIndices('my-area')
logging.debug('done')
您将看到在代码的最后我尝试生成&#39; SNMP-TARGET-MIB-&gt; snmpTargetAddrEntry&#39;和&#39; HOST-RESOURCES-MIB-&gt; hrSWRunEntry&#39;。 SNMP-TARGET-MIB的代码(位于pysnmp文档中)工作正常,但是当我尝试在行instanceId = hostRunTable.getInstIdFromIndices('my-area')
上生成实例时尝试生成HOST-RESOURCES-MIB的代码失败
错误是 pyasn1.error.PyAsn1Error: Can't coerce 'my-area' into integer: invalid literal for int() with base 10: 'my-area'
任何人都可以了解我做错了什么吗?我意识到我是SNMP的新手,所以它很可能是一个愚蠢的错误
答案 0 :(得分:1)
根据HOST-RESOURCES-MIB
,hrSWRunTable
被hrSWRunIndex
列编入索引,其值属于Integer32
类型:
hrSWRunEntry OBJECT-TYPE
SYNTAX HrSWRunEntry
INDEX { hrSWRunIndex }
::= { hrSWRunTable 1 }
hrSWRunIndex OBJECT-TYPE
SYNTAX Integer32 (1..2147483647)
::= { hrSWRunEntry 1 }
您正在尝试从索引值构建OID索引,该索引值是字符串类型,而不是整数。这导致string-&gt; int转换错误:
instanceId = hostRunTable.getInstIdFromIndices('my-area')
所以你可能希望你的第一行有1
作为索引值:
instanceId = hostRunTable.getInstIdFromIndices(1)
在此,我假设您计算instanceId
是为了为新的表格对象构建OID(例如MibScalarInstance
)。