1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
|
import asyncio
import pytest
from pysnmp.hlapi.v3arch.asyncio import *
from pysnmp.proto.api import v2c
from tests.manager_context import MANAGER_PORT, ManagerContextManager
@pytest.mark.asyncio
async def test_send_trap_enterprise_specific():
async with ManagerContextManager() as (_, message_count):
with SnmpEngine() as snmpEngine:
(
errorIndication,
errorStatus,
errorIndex,
varBinds,
) = await send_notification(
snmpEngine,
CommunityData("public", mpModel=0),
await UdpTransportTarget.create(("localhost", MANAGER_PORT)),
ContextData(),
"trap",
NotificationType(
ObjectIdentity("1.3.6.1.4.1.20408.4.1.1.2.432")
).add_varbinds(
(v2c.apiTrapPDU.sysUpTime, TimeTicks(12345)),
("1.3.6.1.2.1.1.1.0", OctetString("my system")),
(v2c.apiTrapPDU.snmpTrapAddress, IpAddress("127.0.0.1")),
(
ObjectIdentity("SNMPv2-MIB", "snmpTrapOID", 0),
ObjectIdentifier("1.3.6.1.4.1.20408.4.1.1.2.432"),
),
),
)
await asyncio.sleep(1)
assert message_count == [1]
@pytest.mark.asyncio
async def test_send_trap_generic():
async with ManagerContextManager() as (_, message_count):
with SnmpEngine() as snmpEngine:
(
errorIndication,
errorStatus,
errorIndex,
varBinds,
) = await send_notification(
snmpEngine,
CommunityData("public", mpModel=0),
await UdpTransportTarget.create(("localhost", MANAGER_PORT)),
ContextData(),
"trap",
NotificationType(ObjectIdentity("1.3.6.1.6.3.1.1.5.2"))
.load_mibs("SNMPv2-MIB")
.add_varbinds(
(
"1.3.6.1.6.3.1.1.4.3.0",
"1.3.6.1.4.1.20408.4.1.1.2",
), # IMPORTANT: MIB is needed to resolve str to correct type.
("1.3.6.1.2.1.1.1.0", OctetString("my system")),
),
)
await asyncio.sleep(1)
assert message_count == [1]
@pytest.mark.asyncio
async def test_send_trap_custom_mib():
async with ManagerContextManager() as (_, message_count):
with SnmpEngine() as snmpEngine:
mibBuilder = snmpEngine.get_mib_builder()
(sysUpTime,) = mibBuilder.import_symbols("__SNMPv2-MIB", "sysUpTime")
sysUpTime.syntax = TimeTicks(12345)
(
errorIndication,
errorStatus,
errorIndex,
varBinds,
) = await send_notification(
snmpEngine,
CommunityData("public", mpModel=0),
await UdpTransportTarget.create(("localhost", MANAGER_PORT)),
ContextData(),
"trap",
NotificationType(
ObjectIdentity(
"NET-SNMP-EXAMPLES-MIB", "netSnmpExampleNotification"
)
).add_varbinds(
ObjectType(
ObjectIdentity(
"NET-SNMP-EXAMPLES-MIB", "netSnmpExampleHeartbeatRate"
),
1,
)
),
)
await asyncio.sleep(1)
assert message_count == [1]
|