我正在尝试使用 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')
您会看到,在代码的末尾,我正在尝试生成“SNMP-TARGET-MIB->snmpTargetAddrEntry”和“HOST-RESOURCES-MIB->hrSWRunEntry”的实例。SNMP-TARGET-MIB 的代码(在 pysnmp 文档中)工作正常,但是当我尝试在线生成实例时,尝试生成 HOST-RESOURCES-MIB 的代码失败instanceId = hostRunTable.getInstIdFromIndices('my-area')
错误是pyasn1.error.PyAsn1Error: Can't coerce 'my-area' into integer: invalid literal for int() with base 10: 'my-area'
谁能阐明我做错了什么?我意识到我是 SNMP 新手,所以很可能这是一个愚蠢的错误