Skip to content

Commit

Permalink
Ported more asyncore examples over.
Browse files Browse the repository at this point in the history
  • Loading branch information
lextm committed Sep 15, 2024
1 parent ae40490 commit f12bc76
Show file tree
Hide file tree
Showing 13 changed files with 519 additions and 70 deletions.
2 changes: 1 addition & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@

# Configuration for Intersphinx
intersphinx_mapping = {
"python": ("https://docs.python.org/3.8/", None),
"python": ("https://docs.python.org/3.9/", None),
"pyasn1": ("https://pyasn1.readthedocs.io/en/latest/", None),
"pysmi": ("https://docs.lextudio.com/pysmi/", None),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,24 @@
Advanced Notification Originator
--------------------------------

.. include:: /../../examples/hlapi/v3arch/asyncio/agent/ntforg/multiple-notifications-at-once.py
.. include:: /../../examples/hlapi/v3arch/asyncio/agent/ntforg/multiple-traps-at-once.py
:start-after: """
:end-before: """ #

.. literalinclude:: /../../examples/hlapi/v3arch/asyncio/agent/ntforg/multiple-notifications-at-once.py
.. literalinclude:: /../../examples/hlapi/v3arch/asyncio/agent/ntforg/multiple-traps-at-once.py
:start-after: """ #
:language: python

:download:`Download</../../examples/hlapi/v3arch/asyncio/agent/ntforg/multiple-notifications-at-once.py>` script.
:download:`Download</../../examples/hlapi/v3arch/asyncio/agent/ntforg/multiple-traps-at-once.py>` script.

.. include:: /../../examples/hlapi/v3arch/asyncio/agent/ntforg/multiple-informs-at-once.py
:start-after: """
:end-before: """ #

.. literalinclude:: /../../examples/hlapi/v3arch/asyncio/agent/ntforg/multiple-informs-at-once.py
:start-after: """ #
:language: python

:download:`Download</../../examples/hlapi/v3arch/asyncio/agent/ntforg/multiple-informs-at-once.py>` script.

See also: :doc:`library reference </docs/api-reference>`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
"""
Multiple concurrent notifications
+++++++++++++++++++++++++++++++++
Send multiple SNMP notifications at once using the following options:
* SNMPv2c and SNMPv3
* with community name 'public'
* over IPv4/UDP
* send INFORM notification
* to multiple Managers
* with TRAP ID 'coldStart' specified as a MIB symbol
* include managed object information specified as var-bind objects pair
Functionally similar to:
| $ snmpinform -v2c -c public demo.pysnmp.com 123 1.3.6.1.6.3.1.1.5.1
| $ snmpinform -v3 -u usr-md5-des -l authPriv -A authkey1 -X privkey1 demo.pysnmp.com 123 1.3.6.1.6.3.1.1.5.1
""" #
import asyncio
from pysnmp.hlapi.v3arch.asyncio import *


async def run():
# List of targets in the following format:
# ( ( authData, transportTarget ), ... )
TARGETS = (
# 1-st target (SNMPv2c over IPv4/UDP)
(
CommunityData("public"),
await UdpTransportTarget.create(("demo.pysnmp.com", 162)),
ContextData(),
),
# 2-nd target (SNMPv3 over IPv4/UDP)
(
UsmUserData("usr-md5-des", "authkey1", "privkey1"),
await UdpTransportTarget.create(("demo.pysnmp.com", 162)),
ContextData(),
),
)

snmpEngine = SnmpEngine()

for authData, transportTarget, contextData in TARGETS:
(
errorIndication,
errorStatus,
errorIndex,
varBindTable,
) = await sendNotification(
snmpEngine,
authData,
transportTarget,
contextData,
"inform", # NotifyType
NotificationType(ObjectIdentity("SNMPv2-MIB", "coldStart")).addVarBinds(
("1.3.6.1.2.1.1.1.0", "my name")
),
)

if errorIndication:
print("Notification not sent: %s" % errorIndication)
elif errorStatus:
print(
"Notification Receiver returned error: %s @%s"
% (errorStatus, errorIndex)
)
else:
print("Notification delivered:")
for name, val in varBindTable:
print(f"{name.prettyPrint()} = {val.prettyPrint()}")

snmpEngine.transportDispatcher.runDispatcher()


asyncio.run(run())

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
"""
Multiple concurrent notifications
+++++++++++++++++++++++++++++++++
Send multiple SNMP notifications at once using the following options:
* SNMPv2c and SNMPv3
* with community name 'public'
* over IPv4/UDP
* send TRAP notification
* to multiple Managers
* with TRAP ID 'coldStart' specified as a MIB symbol
* include managed object information specified as var-bind objects pair
Functionally similar to:
| $ snmptrap -v1 -c public demo.pysnmp.com 1.3.6.1.4.1.20408.4.1.1.2 demo.pysnmp.com 6 432 12345 1.3.6.1.2.1.1.1.0 s "my system"
| $ snmptrap -v2c -c public demo.pysnmp.com 123 1.3.6.1.6.3.1.1.5.1
""" #
import asyncio
from pysnmp.hlapi.v3arch.asyncio import *


async def run():
# List of targets in the following format:
# ( ( authData, transportTarget ), ... )
TARGETS = (
# 1-st target (SNMPv1 over IPv4/UDP)
(
CommunityData("public", mpModel=0),
await UdpTransportTarget.create(("demo.pysnmp.com", 162)),
ContextData(),
),
# 2-nd target (SNMPv2c over IPv4/UDP)
(
CommunityData("public"),
await UdpTransportTarget.create(("demo.pysnmp.com", 162)),
ContextData(),
),
)

snmpEngine = SnmpEngine()

for authData, transportTarget, contextData in TARGETS:
await sendNotification(
snmpEngine,
authData,
transportTarget,
contextData,
"trap", # NotifyType
NotificationType(ObjectIdentity("SNMPv2-MIB", "coldStart")).addVarBinds(
("1.3.6.1.2.1.1.1.0", "my name")
),
)

snmpEngine.transportDispatcher.runDispatcher()


asyncio.run(run())
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
"""
Multiple SNMP Engines
+++++++++++++++++++++
Send SNMP notifications in behalf of multiple independent SNMP engines
using the following options:
* with a single transport dispatcher and two independent SNMP engines
* SNMPv2c and SNMPv3
* with community name 'public' or USM username usr-md5-des
* over IPv4/UDP
* send INFORM notification
* to multiple Managers
* with TRAP ID 'coldStart' specified as a MIB symbol
* include managed object information specified as var-bind objects pair
Within this script we have a single asynchronous TransportDispatcher
and a single UDP-based transport serving two independent SNMP engines.
We use a single instance of AsyncNotificationOriginator with each of
SNMP Engines to communicate INFORM notification to remote systems.
When we receive a [response] message from remote system we use
a custom message router to choose what of the two SNMP engines
data packet should be handed over. The selection criteria we
employ here is based on peer's UDP port number. Other selection
criteria are also possible.
| $ snmpinform -v2c -c public demo.pysnmp.com:1162 123 1.3.6.1.6.3.1.1.5.1
| $ snmpinform -v3 -u usr-md5-des -l authPriv -A authkey1 -X privkey1 demo.pysnmp.com 123 1.3.6.1.6.3.1.1.5.1
""" #
import asyncio
from pysnmp.hlapi.v3arch.asyncio import *
from pysnmp.carrier.asyncio.dispatch import AsyncioDispatcher


async def run():
# List of targets in the following format:
# ( ( authData, transportTarget ), ... )
TARGETS = (
# 1-st target (SNMPv2c over IPv4/UDP)
(
CommunityData("public"),
await UdpTransportTarget.create(("demo.pysnmp.com", 162)), # TODO: 1162
ContextData(),
),
# 2-nd target (SNMPv3 over IPv4/UDP)
(
UsmUserData("usr-md5-des", "authkey1", "privkey1"),
await UdpTransportTarget.create(("demo.pysnmp.com", 162)),
ContextData(),
),
)

# Instantiate the single transport dispatcher object
transportDispatcher = AsyncioDispatcher()

# Setup a custom data routing function to select snmpEngine by transportDomain
transportDispatcher.registerRoutingCbFun(lambda td, ta, d: ta[1] % 3 and "A" or "B")

snmpEngineA = SnmpEngine()
snmpEngineA.registerTransportDispatcher(transportDispatcher, "A")

snmpEngineB = SnmpEngine()
snmpEngineB.registerTransportDispatcher(transportDispatcher, "B")

for authData, transportTarget, contextData in TARGETS:
# Pick one of the two SNMP engines
snmpEngine = (
transportTarget.getTransportInfo()[1][1] % 3 and snmpEngineA or snmpEngineB
)

errorIndication, errorStatus, errorIndex, varBinds = await sendNotification(
snmpEngine,
authData,
transportTarget,
contextData,
"inform", # NotifyType
NotificationType(ObjectIdentity("SNMPv2-MIB", "coldStart")).addVarBinds(
("1.3.6.1.2.1.1.1.0", "my name")
),
)

if errorIndication:
print(
"Notification for {} not sent: {}".format(
snmpEngine.snmpEngineID.prettyPrint(), errorIndication
)
)

elif errorStatus:
print(
"Notification Receiver returned error for request %s, "
"SNMP Engine: %s @%s"
% (snmpEngine.snmpEngineID.prettyPrint(), errorStatus, errorIndex)
)

else:
print(
"Notification for SNMP Engine %s delivered: "
% (snmpEngine.snmpEngineID.prettyPrint())
)

for name, val in varBinds:
print(f"{name.prettyPrint()} = {val.prettyPrint()}")

transportDispatcher.runDispatcher()


asyncio.run(run())
Loading

0 comments on commit f12bc76

Please sign in to comment.