【问题标题】:pysnmp Agent with HOST-RESOURCES-MIB带有 HOST-RESOURCES-MIB 的 pysnmp 代理
【发布时间】:2016-12-28 15:58:02
【问题描述】:

我正在尝试使用 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 的代码失败了@

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

谁能解释我做错了什么?我意识到我是 SNMP 新手,所以很可能这是一个愚蠢的错误

【问题讨论】:

    标签: snmp mib pysnmp opennms


    【解决方案1】:

    根据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 是为了为您的新表格对象(例如 MibScalarInstance)构建 OID。

    【讨论】:

    • 感谢您的评论。这非常有用。但是,我的 SNMP 代理无法正常工作时仍然存在问题。你能不能看一下我的其他帖子,看看你是否能有所启发? stackoverflow.com/questions/41384941/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多