我試圖監視一個使用opennms的python進程。爲此,我需要實現支持HOST-RESOURCES-MIB的代理。 Opennms通過檢查HOST-RESOURCES-MIB的hrSwRunTable來檢查進程的狀態。該測試通過將給定進程與hrSwRunState的數值匹配爲hrSwRunName來完成。pysnmp Agent with HOST-RESOURCES-MIB
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,因此它很可能它是一個愚蠢的錯誤
感謝您的評論。這非常有用。但是,我的SNMP代理仍然無法正常工作。你願意看看我的其他帖子,看看你是否可以點亮一些東西? http://stackoverflow.com/questions/41384941/using-pysnmp-to-write-an-snmp-agent –