带有 HOST-RESOURCES-MIB 的 pysnmp 代理

pysnmp Agent with HOST-RESOURCES-MIB

我正在尝试使用 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 文档中)工作正常,但是当我尝试在行 instanceId = hostRunTable.getInstIdFromIndices('my-area')[= 上生成实例时,尝试生成 HOST-RESOURCES-MIB 的代码失败13=]

错误是pyasn1.error.PyAsn1Error: Can't coerce 'my-area' into integer: invalid literal for int() with base 10: 'my-area'

任何人都可以阐明我做错了什么吗?我意识到我是 SNMP 的新手,所以这很可能是一个愚蠢的错误

根据HOST-RESOURCES-MIBhrSWRunTablehrSWRunIndex列索引,其值属于Integer32类型:

hrSWRunEntry OBJECT-TYPE
    SYNTAX     HrSWRunEntry
    INDEX { hrSWRunIndex }
    ::= { hrSWRunTable 1 }

hrSWRunIndex OBJECT-TYPE
    SYNTAX     Integer32 (1..2147483647)
    ::= { hrSWRunEntry 1 }

您正在尝试从字符串类型而非整数类型的索引值构建 OID 索引。这会导致 string->int 转换错误:

instanceId = hostRunTable.getInstIdFromIndices('my-area')

所以您可能希望第一行具有 1 作为索引值:

instanceId = hostRunTable.getInstIdFromIndices(1)

这里我假设您计算 instanceId 是为了为您的新表格对象构建 OID(例如 MibScalarInstance)。