简体   繁体   English

Pysnmp无法从snmp陷阱解析OID

[英]Pysnmp can't resolve OID's from snmp trap

I'm trying to resolve the OIDs that are received on an SNMP Trap from an HP switch stack but they only resolve down to a certain level and stop. 我正在尝试解决从HP交换机堆栈在SNMP陷阱上收到的OID,但它们只能解析到特定级别并停止。 It's like the HP MIBs are not being loaded. 就像没有加载HP MIB。 It's unclear from all the documentation I can find on pysnmp if this is the appropriate way to add custom MIBs and resolve OIDs from a trap. 从pysnmp上可以找到的所有文档中尚不清楚,这是否是添加自定义MIB并从陷阱解析OID的适当方法。 MIBs can be downloaded here . MIB可以在这里下载。

from pysnmp.entity import engine, config
from pysnmp.carrier.asyncore.dgram import udp
from pysnmp.smi import view, builder, rfc1902
from pysnmp.entity.rfc3413 import ntfrcv, mibvar

# Create SNMP engine with autogenernated engineID and pre-bound
# to socket transport dispatcher
snmpEngine = engine.SnmpEngine()
build = snmpEngine.getMibBuilder()
build.addMibSources(builder.DirMibSource("C:/Users/t/Documents/mibs"))
viewer = view.MibViewController(build)

# Transport setup

# UDP over IPv4, first listening interface/port
config.addTransport(
    snmpEngine,
    udp.domainName + (1,),
    udp.UdpTransport().openServerMode(('0.0.0.0', 162))
)

# SNMPv1/2c setup

# SecurityName <-> CommunityName mapping
config.addV1System(snmpEngine, '????', 'public')


# Callback function for receiving notifications
# noinspection PyUnusedLocal,PyUnusedLocal,PyUnusedLocal
def cbFun(snmpEngine, stateReference, contextEngineId, contextName, varBinds, cbCtx):
    print('Notification from ContextEngineId "%s", ContextName "%s"' % (contextEngineId.prettyPrint(),
                                                                        contextName.prettyPrint()))
    for name, val in varBinds:
        print(name)
        symbol = rfc1902.ObjectIdentity(name).resolveWithMib(viewer).getMibSymbol()
        print(symbol[1])



# Register SNMP Application at the SNMP engine
ntfrcv.NotificationReceiver(snmpEngine, cbFun)

snmpEngine.transportDispatcher.jobStarted(1)  # this job would never finish

# Run I/O dispatcher which would receive queries and send confirmations
try:
    snmpEngine.transportDispatcher.runDispatcher()
except:
    snmpEngine.transportDispatcher.closeDispatcher()
    raise

Output upon receiving a trap: 收到陷阱后输出:

Notification from ContextEngineId "0x80004fb8056ed891e8", ContextName ""
1.3.6.1.2.1.1.3.0
sysUpTime
1.3.6.1.6.3.1.1.4.1.0
snmpTrapOID
1.3.6.1.6.3.18.1.3.0
snmpTrapAddress
1.3.6.1.6.3.18.1.4.0
snmpTrapCommunity
1.3.6.1.6.3.1.1.4.3.0
snmpTrapEnterprise
1.3.6.1.4.1.11.2.14.11.5.1.7.1.29.1.9
enterprises
1.3.6.1.4.1.11.2.14.11.5.1.7.1.29.1.0.1
enterprises
1.3.6.1.4.1.11.2.14.11.5.1.7.1.29.1.0.2
enterprises
1.3.6.1.4.1.11.2.14.11.5.1.7.1.29.1.0.3
enterprises
1.3.6.1.4.1.11.2.14.11.5.1.7.1.29.1.0.4
enterprises
1.3.6.1.4.1.11.2.14.11.5.1.7.1.29.1.0.5
enterprises

As you can see many distinct OIDs just resolve to "enterprises". 如您所见,许多独特的OID只是解析为“企业”。 I am using pysnmp 4.4.4. 我正在使用pysnmp 4.4.4。

Yes, it seems that only the core MIBs are loaded. 是的,似乎只加载了核心MIB。

If you want to follow this quite low-level path, then you need to pre-compile all your ASN.1 MIBs (those you pulled from HPE site) with the mibdump tool into pysnmp format. 如果要遵循这个相当低级的路径,则需要使用mibdump工具将所有ASN.1 MIB(从HPE站点拉出的MIB)预编译为pysnmp格式。 Then put those *.py files into some directory and point pysnmp to it through build.addMibSources(builder.DirMibSource()) call. 然后将这些* .py文件放入某个目录,并通过build.addMibSources(builder.DirMibSource())调用将build.addMibSources(builder.DirMibSource())指向该目录。

Also, make sure to pre-load up all those MIBs at once on startup by invoking build.loadModules() (w/o arguments). 另外,请确保在启动时通过调用build.loadModules() (不带参数)一次预加载所有这些MIB。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM