pysnmp: 在使用sendNotification发送SNMP陷阱时,如何将SysUpTimeInstance设置为非0值

huangapple go评论54阅读模式
英文:

pysnmp: How to set SysUpTimeInstance to something else than 0 when using sendNotification for sending snmp trap

问题

我有一些可以发送 SNMP 陷阱用于警报等的设备。我需要模拟这些设备以进行测试。测试工具的输出必须与设备的输出完全相同,但我需要控制值等。我已经使用 pysnmp 创建了一个 Python 脚本,并使用 sendNotification 进行了设置。看起来工作正常。但是,DISMAN-EVENT-MIB::sysUpTimeInstance 会被 sendNotification 自动发送,并且总是为 0。我该如何将此值设置为其他值?我知道可以在 addVarBinds 中放置一个额外的对象,但这只会提供具有另一个值的额外对象。初始的 sysUpTimeInstance 仍然是 0。
pysnmp 4.4.12
pysnmp-pyasn1 1.1.3
pysnmp-pysmi 1.1.10
pysnmplib 5.0.21

from pysnmp.hlapi import *

iterator7 = sendNotification(
    SnmpEngine(),
    CommunityData('public', mpModel=1),
    UdpTransportTarget(('localhost', 162)),
    ContextData(),
    'trap',
     NotificationType(ObjectIdentity('NET-SNMP-EXAMPLES-MIB', 'netSnmpExampleNotification')).addVarBinds(ObjectType(ObjectIdentity('NET-SNMP-EXAMPLES-MIB','netSnmpExampleHeartbeatRate'), 1))
)

errorIndication, errorStatus, errorIndex, varBinds = next(iterator7)

if errorIndication:
    print(errorIndication)

上面使用的对象仅为示例。我有另一个包含正确对象的 MIB 文件。
上述示例将导致接收到以下内容:
DISMAN-EVENT-MIB::sysUpTimeInstance = Timeticks: (0) 0:00:00.00 SNMPv2-MIB::snmpTrapOID.0 = OID: NET-SNMP-EXAMPLES-MIB::netSnmpExampleNotification NET-SNMP-EXAMPLES-MIB::netSnmpExampleHeartbeatRate = INTEGER: 1
我正在使用 snmptrapd 接收 SNMP 陷阱。

英文:

I have some equipment that can send snmp traps for alarms etc. I need to simulate this equipment for testing purpose. The output from the tester must be exactly like the output from the equipment but I need to control values etc. I have created a Python script using pysnmp and sendNotification for this. It seems to work fine. However the DISMAN-EVENT-MIB::sysUpTimeInstance is send automaticaly by sendNotification and the uptime is always 0. How can I set this value to something else? I know I can put an extra object in the addVarBinds but this will just give an extra object with another value. The initial sysUpTimeInstance is still 0.
pysnmp 4.4.12
pysnmp-pyasn1 1.1.3
pysnmp-pysmi 1.1.10
pysnmplib 5.0.21

from pysnmp.hlapi import *

iterator7 = sendNotification(
    SnmpEngine(),
    CommunityData('public', mpModel=1),
    UdpTransportTarget(('localhost', 162)),
    ContextData(),
    'trap',
     NotificationType(ObjectIdentity('NET-SNMP-EXAMPLES-MIB', 'netSnmpExampleNotification')).addVarBinds(ObjectType(ObjectIdentity('NET-SNMP-EXAMPLES-MIB','netSnmpExampleHeartbeatRate'), 1))
)

errorIndication, errorStatus, errorIndex, varBinds = next(iterator7)

if errorIndication:
    print(errorIndication)

The object used in the above are just examples. I have a another MIB file with the correct objects.
The above example will result in the following received:
DISMAN-EVENT-MIB::sysUpTimeInstance = Timeticks: (0) 0:00:00.00 SNMPv2-MIB::snmpTrapOID.0 = OID: NET-SNMP-EXAMPLES-MIB::netSnmpExampleNotification NET-SNMP-EXAMPLES-MIB::netSnmpExampleHeartbeatRate = INTEGER: 1
I am receiving snmp traps with snmptrapd

答案1

得分: 0

如果您正在使用pysnmp-lextudio包,以下代码应该有效,

import asyncio
from pysnmp.hlapi.asyncio import *

async def run():
    snmpEngine = SnmpEngine()
    # 示例演示如何更新sysUpTime
    mibBuilder = snmpEngine.msgAndPduDsp.mibInstrumController.mibBuilder
    sysUpTime, = mibBuilder.importSymbols('__SNMPv2-MIB', 'sysUpTime')
    sysUpTime.syntax = TimeTicks(12345)  # 将运行时间设置为12345

    trap_result = await sendNotification(
        snmpEngine,
        CommunityData('public', mpModel=1),
        UdpTransportTarget(('localhost', 162)),
        ContextData(),
        "trap",
        NotificationType(ObjectIdentity('NET-SNMP-EXAMPLES-MIB', 'netSnmpExampleNotification')).addVarBinds(ObjectType(ObjectIdentity('NET-SNMP-EXAMPLES-MIB','netSnmpExampleHeartbeatRate'), 1))
    )
    errorIndication, errorStatus, errorIndex, varBinds = await trap_result
    if errorIndication:
        print(errorIndication)

    snmpEngine.transportDispatcher.closeDispatcher()

asyncio.run(run())

如果您使用其他pysnmp包,不能保证此代码适用。

英文:

If you are using pysnmp-lextudio package, the following code should work,

import asyncio
from pysnmp.hlapi.asyncio import *

async def run():
    snmpEngine = SnmpEngine()
    # Example of how you might update sysUpTime
    mibBuilder = snmpEngine.msgAndPduDsp.mibInstrumController.mibBuilder
    sysUpTime, = mibBuilder.importSymbols('__SNMPv2-MIB', 'sysUpTime')
    sysUpTime.syntax = TimeTicks(12345)  # Set uptime to 12345

    trap_result = await sendNotification(
        snmpEngine,
        CommunityData('public', mpModel=1),
        UdpTransportTarget(('localhost', 162)),
        ContextData(),
        "trap",
        NotificationType(ObjectIdentity('NET-SNMP-EXAMPLES-MIB', 'netSnmpExampleNotification')).addVarBinds(ObjectType(ObjectIdentity('NET-SNMP-EXAMPLES-MIB','netSnmpExampleHeartbeatRate'), 1))
    )
    errorIndication, errorStatus, errorIndex, varBinds = await trap_result
    if errorIndication:
        print(errorIndication)

    snmpEngine.transportDispatcher.closeDispatcher()


asyncio.run(run())

No guarantee if you are using other pysnmp packages.

huangapple
  • 本文由 发表于 2023年7月20日 20:09:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76729696.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定