我是 SNMP Context 的新手。我正在云中设置 SNMP 代理。这是我们从使用上下文名称的站点获得的基本格式。但问题是,当我们在 snmpwalk 请求中使用上下文名称时,它会给出类似的回复
“SNMPv2-SMI::dod = 此 MIB 视图中不再剩下变量(它超出了 MIB 树的末尾)”
我们遵循官方文档给出的格式
# Serve non-default MIB tree
# Listen and respond to SNMP GET / SET / GETNEXT / GETBULK queries with
# the following options:
# SNMPv3
# with USM username usr - md5 - none
# using alternative set of Managed Objects addressed by
# contextName: my - context
# allow access to SNMPv2 - MIB objects (1.3.6.1.2.1)
# over IPv4 / UDP, listening at 127.0.0.1:161
from pysnmp.entity import engine, config
from pysnmp.entity.rfc3413 import cmdrsp, context
from pysnmp.carrier.asyncore.dgram import udp
from pysnmp.smi import instrum, builder
from pysnmp.proto.api import v2c
snmpEngine = engine.SnmpEngine()
config.addTransport(
snmpEngine,
udp.domainName,
udp.UdpTransport().openServerMode(('127.0.0.1', 161))
)
# SNMPv3/USM setup
# user: usr-md5-none, auth: MD5, priv NONE
config.addV3User(
snmpEngine, 'usr-md5-none',
config.usmHMACMD5AuthProtocol, 'authkey1'
)
# Allow full MIB access for each user at VACM
config.addVacmUser(snmpEngine, 3, 'usr-md5-none', 'authNoPriv', (1, 3, 6, 1, 2, 1), (1, 3, 6, 1, 2, 1))
snmpContext = context.SnmpContext(
snmpEngine
)
# Create an [empty] set of Managed Objects (MibBuilder), pass it to
# Management Instrumentation Controller and register at SNMP Context
# under ContextName 'my-context'
snmpContext.registerContextName(
v2c.OctetString('my-context'), # Context Name
instrum.MibInstrumController(builder.MibBuilder().loadModules) # Managed Objects
)
# Register SNMP Applications at the SNMP engine for particular SNMP context
cmdrsp.GetCommandResponder(snmpEngine, snmpContext)
cmdrsp.SetCommandResponder(snmpEngine, snmpContext)
cmdrsp.NextCommandResponder(snmpEngine, snmpContext)
cmdrsp.BulkCommandResponder(snmpEngine, snmpContext)
# Register an imaginary never-ending job to keep I/O dispat#cher running forever
snmpEngine.transportDispatcher.jobStarted(1)
# Run I/O dispatcher which would receive queries and send responses
try:
snmpEngine.transportDispatcher.runDispatcher()
except:
snmpEngine.transportDispatcher.closeDispatcher()
raise
snmpwalk -v3 -u usr-md5-none -l authNoPriv -A authkey1 -n my-context 127.0.0.1 .1.3.6
使用上面的 snmpwalk 时,我们将得到类似的输出
SNMPv2-SMI::dod = No more variables left in this MIB View (It is past the end of the MIB tree)
但是如果使用下面的snmpwalk
snmpwalk -v3 -u usr-md5-none -l authNoPriv -A authkey1 127.0.0.1 .1.3.6
我们将得到输出
SNMPv2-MIB::sysDescr.0 = STRING: PySNMP engine version 4.4.11, Python 2.7.12 (v2.7.12:d33e0cf91556, Jun 27 2016, 15:24:40) [MSC v.1500 64 bit (AMD64)]
SNMPv2-MIB::sysObjectID.0 = OID: SNMPv2-SMI::enterprises.20408
DISMAN-EVENT-MIB::sysUpTimeInstance = Timeticks: (14) 0:00:00.14
SNMPv2-MIB::sysContact.0 = STRING:
SNMPv2-MIB::sysName.0 = STRING:
SNMPv2-MIB::sysLocation.0 = STRING:
SNMPv2-MIB::sysServices.0 = INTEGER: 0
SNMPv2-MIB::sysORLastChange.0 = Timeticks: (0) 0:00:00.00
SNMPv2-MIB::snmpInPkts.0 = Counter32: 10
SNMPv2-MIB::snmpOutPkts.0 = Counter32: 0
SNMPv2-MIB::snmpInBadVersions.0 = Counter32: 0
SNMPv2-MIB::snmpInBadCommunityNames.0 = Counter32: 0
SNMPv2-MIB::snmpInBadCommunityUses.0 = Counter32: 0
SNMPv2-MIB::snmpInASNParseErrs.0 = Counter32: 0
SNMPv2-MIB::snmpInTooBigs.0 = Counter32: 0
SNMPv2-MIB::snmpInNoSuchNames.0 = Counter32: 0
SNMPv2-MIB::snmpInBadValues.0 = Counter32: 0
SNMPv2-MIB::snmpInReadOnlys.0 = Counter32: 0
SNMPv2-MIB::snmpInGenErrs.0 = Counter32: 0
SNMPv2-MIB::snmpInTotalReqVars.0 = Counter32: 0
SNMPv2-MIB::snmpInTotalSetVars.0 = Counter32: 0
SNMPv2-MIB::snmpInGetRequests.0 = Counter32: 0
SNMPv2-MIB::snmpInGetNexts.0 = Counter32: 0
SNMPv2-MIB::snmpInGetResponses.0 = Counter32: 0
SNMPv2-MIB::snmpInTraps.0 = Counter32: 0
SNMPv2-MIB::snmpOutTooBigs.0 = Counter32: 0
SNMPv2-MIB::snmpOutNoSuchNames.0 = Counter32: 0
SNMPv2-MIB::snmpOutBadValues.0 = Counter32: 0
SNMPv2-MIB::snmpOutGenErrs.0 = Counter32: 0
SNMPv2-MIB::snmpOutSetRequests.0 = Counter32: 0
SNMPv2-MIB::snmpOutGetResponses.0 = Counter32: 0
SNMPv2-MIB::snmpOutTraps.0 = Counter32: 0
SNMPv2-MIB::snmpEnableAuthenTraps.0 = INTEGER: enabled(1)
SNMPv2-MIB::snmpSilentDrops.0 = Counter32: 0
SNMPv2-MIB::snmpProxyDrops.0 = Counter32: 0
因此,为了获得上下文名称相同的输出,我应该注意哪些更改
您的 MIB 已填充吗?这一行:
instrum.MibInstrumController(builder.MibBuilder()) # Managed Objects
创建一个空的 MIB 树并将其注册到
my-context
上下文名称下。除非您在代码中的其他位置填充该 MIB 树,否则这解释了为什么您没有得到任何响应。
如果您这样做:
instrum.MibInstrumController(builder.MibBuilder().loadModules())
pysnmp 搜索路径中的所有现有 MIB 模块都将加载并注册到这个新的 MIB 树中。
顺便说一句,整个 ContextEngineId 配置与 ContextName 无关。除非您出于某种原因需要它,否则您可以在代码中删除与特定 ContextEngineId 值相关的所有内容。