diff options
author | elie <elie> | 2014-11-04 20:21:48 +0000 |
---|---|---|
committer | elie <elie> | 2014-11-04 20:21:48 +0000 |
commit | 2badd13fbc5a16806170deb432a1aeee03b74905 (patch) | |
tree | 8ed445ae7de8572557c5d6997839723fe546d08c | |
parent | d947278e9747fc5d0c198f78b30132b13eb90760 (diff) | |
download | pysnmp-2badd13fbc5a16806170deb432a1aeee03b74905.tar.gz |
initial support for asyncio network transport added
19 files changed, 1213 insertions, 1 deletions
@@ -20,6 +20,7 @@ Revision 4.2.6rc0 of pysnmp engine. Previously introduced non-standard APIs (like getting peer's transport endpoint which is not suggested in RFCs) will be gradually migrated to this new framework. +- Initial support for the asyncio network transport added. - Internal oneliner apps configuration cache moved from respective apps objects to [a singular] snmpEngine object. That would allow for better cache reuse and allow for a single app working with @@ -23,6 +23,7 @@ Randy Couey Brian Kyckelhahn Mark M Evans Filippo Giunchedi at Truelite Srl - +Matt Hooks +Zachary Lorusso Thanks to Python Software Foundation for granting financial support for the project. diff --git a/examples/v3arch/asyncio/agent/cmdrsp/v3-multiple-users.py b/examples/v3arch/asyncio/agent/cmdrsp/v3-multiple-users.py new file mode 100644 index 0000000..b41fc49 --- /dev/null +++ b/examples/v3arch/asyncio/agent/cmdrsp/v3-multiple-users.py @@ -0,0 +1,77 @@ +# +# Command Responder +# +# Listen and respond to SNMP GET/SET/GETNEXT/GETBULK queries with +# the following options: +# +# * SNMPv3 +# * with USM user 'usr-md5-des', auth: MD5, priv DES or +# with USM user 'usr-sha-none', auth: SHA, no privacy +# with USM user 'usr-sha-aes128', auth: SHA, priv AES +# * allow access to SNMPv2-MIB objects (1.3.6.1.2.1) +# * over IPv4/UDP, listening at 127.0.0.1:161 +# * using asyncio network transport (available since Python 3.4) +# +# Either of the following Net-SNMP's commands will walk this Agent: +# +# $ snmpwalk -v3 -u usr-md5-des -l authPriv -A authkey1 -X privkey1 localhost .1.3.6 +# $ snmpwalk -v3 -u usr-sha-none -l authNoPriv -a SHA -A authkey1 localhost .1.3.6 +# $ snmpwalk -v3 -u usr-sha-aes128 -l authPriv -a SHA -A authkey1 -x AES -X privkey1 localhost .1.3.6 +# +from pysnmp.entity import engine, config +from pysnmp.entity.rfc3413 import cmdrsp, context +from pysnmp.carrier.asyncio.dgram import udp +import asyncio + +# Get the event loop for this thread +loop = asyncio.get_event_loop() + +# Create SNMP engine with autogenernated engineID and pre-bound +# to socket transport dispatcher +snmpEngine = engine.SnmpEngine() + +# Transport setup + +# UDP over IPv4 +config.addTransport( + snmpEngine, + udp.domainName, + udp.UdpTransport().openServerMode(('127.0.0.1', 161)) +) + +# SNMPv3/USM setup + +# user: usr-md5-des, auth: MD5, priv DES +config.addV3User( + snmpEngine, 'usr-md5-des', + config.usmHMACMD5AuthProtocol, 'authkey1', + config.usmDESPrivProtocol, 'privkey1' +) +# user: usr-sha-none, auth: SHA, priv NONE +config.addV3User( + snmpEngine, 'usr-sha-none', + config.usmHMACSHAAuthProtocol, 'authkey1' +) +# user: usr-sha-none, auth: SHA, priv AES +config.addV3User( + snmpEngine, 'usr-sha-aes128', + config.usmHMACSHAAuthProtocol, 'authkey1', + config.usmAesCfb128Protocol, 'privkey1' +) + +# Allow full MIB access for each user at VACM +config.addVacmUser(snmpEngine, 3, 'usr-md5-des', 'authPriv', (1,3,6,1,2,1), (1,3,6,1,2,1)) +config.addVacmUser(snmpEngine, 3, 'usr-sha-none', 'authNoPriv', (1,3,6,1,2,1), (1,3,6,1,2,1)) +config.addVacmUser(snmpEngine, 3, 'usr-sha-aes128', 'authPriv', (1,3,6,1,2,1), (1,3,6,1,2,1)) + +# Get default SNMP context this SNMP engine serves +snmpContext = context.SnmpContext(snmpEngine) + +# 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) + +# Run asyncio main loop +loop.run_forever() diff --git a/examples/v3arch/asyncio/agent/ntforg/inform-v3.py b/examples/v3arch/asyncio/agent/ntforg/inform-v3.py new file mode 100644 index 0000000..f74018d --- /dev/null +++ b/examples/v3arch/asyncio/agent/ntforg/inform-v3.py @@ -0,0 +1,117 @@ +# +# Notification Originator +# +# Send SNMP INFORM notification using the following options: +# +# * SNMPv3 +# * with user 'usr-md5-none', auth: MD5, priv NONE +# * over IPv4/UDP +# * using asyncio network transport (available from Python 3.4) +# * to a Manager at 127.0.0.1:162 +# * send INFORM notification +# * with TRAP ID 'warmStart' specified as an OID +# * include managed object information 1.3.6.1.2.1.1.5.0 = 'system name' +# +from pysnmp.entity import engine, config +from pysnmp.entity.rfc3413 import context +from pysnmp.entity.rfc3413.asyncio import ntforg +from pysnmp.carrier.asyncio.dgram import udp +from pysnmp.proto import rfc1902 +import asyncio + +# Get the event loop for this thread +loop = asyncio.get_event_loop() + +# Create SNMP engine instance +snmpEngine = engine.SnmpEngine() + +# SNMPv3/USM setup + +# Add USM user +config.addV3User( + snmpEngine, 'usr-md5-none', + config.usmHMACMD5AuthProtocol, 'authkey1' +) +config.addTargetParams(snmpEngine, 'my-creds', 'usr-md5-none', 'authNoPriv') + +# Transport setup + +# +# Setup transport endpoint and bind it with security settings yielding +# a target name. Since Notifications could be sent to multiple Managers +# at once, more than one target entry may be configured (and tagged). +# +config.addTransport( + snmpEngine, + udp.domainName, + udp.UdpTransport().openClientMode() +) +config.addTargetAddr( + snmpEngine, 'my-nms', + udp.domainName, ('127.0.0.1', 162), + 'my-creds', + tagList='all-my-managers' +) + +# Specify what kind of notification should be sent (TRAP or INFORM), +# to what targets (chosen by tag) and what filter should apply to +# the set of targets (selected by tag) +config.addNotificationTarget( + snmpEngine, 'my-notification', 'my-filter', 'all-my-managers', 'inform' +) + +# Allow NOTIFY access to Agent's MIB by this SNMP model (3), securityLevel +# and SecurityName +config.addContext(snmpEngine, '') +config.addVacmUser(snmpEngine, 3, 'usr-md5-none', 'authNoPriv', (), (), (1,3,6)) + +# Create Notification Originator App instance. +ntfOrg = ntforg.NotificationOriginator() + + # Create default SNMP context where contextEngineId == SnmpEngineId +snmpContext = context.SnmpContext(snmpEngine) + +# Error/confirmation receiver +def cbFun(future): + snmpEngine, errorIndication, errorStatus, errorIndex, varBinds = future.result() + print('Notification status - %s' % ( + errorIndication and errorIndication or 'delivered' + ) + ) + + # This also terminates internal timer + config.delTransport( + snmpEngine, + udp.domainName + ) + + # Finish all scheduled tasks and end the loop + loop.stop() + +# Build and submit notification message to dispatcher +future = ntfOrg.sendVarBinds( + snmpEngine, + # Notification targets + 'my-notification', + # SNMP Context + snmpContext, + # contextName + '', + # notification name (SNMPv2-MIB::coldStart) + (1,3,6,1,6,3,1,1,5,1), + # notification objects instance index + None, + # additional var-binds: ( (oid, value), ... ) + [ ((1,3,6,1,2,1,1,5,0), rfc1902.OctetString('system name')) ] +) + +# Register error/response receiver function on future +future.add_done_callback(cbFun) + +print('Notification is scheduled to be sent') + +# Run asyncio main loop +loop.run_forever() + +# Clear the event loop +loop.close() diff --git a/examples/v3arch/asyncio/agent/ntforg/trap-v1.py b/examples/v3arch/asyncio/agent/ntforg/trap-v1.py new file mode 100644 index 0000000..2dd6f4a --- /dev/null +++ b/examples/v3arch/asyncio/agent/ntforg/trap-v1.py @@ -0,0 +1,132 @@ +# +# Notification Originator +# +# Send SNMP notification using the following options: +# +# * SNMPv1 +# * with community name 'public' +# * over IPv4/UDP +# * using asyncio network transport (available from Python 3.4) +# * to a Manager at 127.0.0.1:162 +# * send TRAP notification +# * with TRAP ID 'coldStart' specified as an OID +# * include managed objects information: +# * overriding Uptime value with 12345 +# * overriding Agent Address with '127.0.0.1' +# * overriding Enterprise OID with 1.3.6.1.4.1.20408.4.1.1.2 +# * include managed object information '1.3.6.1.2.1.1.1.0' = 'my system' +# +from pysnmp.entity import engine, config +from pysnmp.entity.rfc3413 import context +from pysnmp.entity.rfc3413.asyncio import ntforg +from pysnmp.carrier.asyncio.dgram import udp +from pysnmp.proto import rfc1902 +import asyncio + +# Get the event loop for this thread +loop = asyncio.get_event_loop() + +# Create SNMP engine instance +snmpEngine = engine.SnmpEngine() + +# SNMPv1 setup + +# SecurityName <-> CommunityName mapping +config.addV1System(snmpEngine, 'my-area', 'public') + +# Specify security settings per SecurityName (SNMPv1 -> 0) +config.addTargetParams(snmpEngine, 'my-creds', 'my-area', 'noAuthNoPriv', 0) + +# Transport setup +# +# Setup transport endpoint and bind it with security settings yielding +# a target name. Since Notifications could be sent to multiple Managers +# at once, more than one target entry may be configured (and tagged). +# + +# UDP/IPv4 +config.addTransport( + snmpEngine, + udp.domainName, + udp.UdpTransport().openClientMode() +) +config.addTargetAddr( + snmpEngine, 'my-nms-1', + udp.domainName, ('127.0.0.1', 162), + 'my-creds', + tagList='all-my-managers' +) + +# Specify what kind of notification should be sent (TRAP or INFORM), +# to what targets (chosen by tag) and what filter should apply to +# the set of targets (selected by tag) +config.addNotificationTarget( + snmpEngine, 'my-notification', 'my-filter', 'all-my-managers', 'trap' +) + +# Allow NOTIFY access to Agent's MIB by this SNMP model (1), securityLevel +# and SecurityName +config.addContext(snmpEngine, '') +config.addVacmUser(snmpEngine, 1, 'my-area', 'noAuthNoPriv', (), (), (1,3,6)) + +# Create Notification Originator App instance. +ntfOrg = ntforg.NotificationOriginator() + + # Create default SNMP context where contextEngineId == SnmpEngineId +snmpContext = context.SnmpContext(snmpEngine) + +# Prepare notification to be sent returning asyncio future object +future = ntfOrg.sendVarBinds( + snmpEngine, + # Notification targets + 'my-notification', + # SNMP Context + snmpContext, + # contextName + '', + # notification name: Generic Trap #6 (enterpriseSpecific) + # and Specific Trap 432 + '1.3.6.1.4.1.20408.4.1.1.2.0.432', + # notification objects instance index + None, + # additional var-binds holding SNMPv1 TRAP details + [ + # Uptime value with 12345 + (rfc1902.ObjectName('1.3.6.1.2.1.1.3.0'), + rfc1902.TimeTicks(12345)), + # Agent Address with '127.0.0.1' + (rfc1902.ObjectName('1.3.6.1.6.3.18.1.3.0'), + rfc1902.IpAddress('127.0.0.1')), + # Enterprise OID with 1.3.6.1.4.1.20408.4.1.1.2 + (rfc1902.ObjectName('1.3.6.1.6.3.1.1.4.3.0'), + rfc1902.ObjectName('1.3.6.1.4.1.20408.4.1.1.2')), + # managed object '1.3.6.1.2.1.1.1.0' = 'my system' + (rfc1902.ObjectName('1.3.6.1.2.1.1.1.0'), + rfc1902.OctetString('my system')) + ] +) + +print('Notification is scheduled to be sent') + +@asyncio.coroutine +def wait(future): + # a hack: wait for outgoing packet to leave us + yield from asyncio.sleep(1) + future.set_result(True) + +asyncio.async(wait(future)) + +# Run asyncio main loop +loop.run_until_complete(future) + +# This also terminates internal timer +config.delTransport( + snmpEngine, + udp.domainName +) + +# Finish all scheduled tasks and end the loop +loop.stop() + +# Clear the event loop +loop.close() diff --git a/examples/v3arch/asyncio/manager/cmdgen/get-v2c-custom-timeout.py b/examples/v3arch/asyncio/manager/cmdgen/get-v2c-custom-timeout.py new file mode 100644 index 0000000..61cefed --- /dev/null +++ b/examples/v3arch/asyncio/manager/cmdgen/get-v2c-custom-timeout.py @@ -0,0 +1,95 @@ +#
+# GET Command Generator
+#
+# Send a SNMP GET request
+# with SNMPv2c, community 'public'
+# using Asyncio framework for network transport
+# over IPv4/UDP
+# to an Agent at 195.218.195.228:161
+# wait 3 seconds for response, retry 5 times (plus one initial attempt)
+# for an OID in tuple form
+#
+# This script performs similar to the following Net-SNMP command:
+#
+# $ snmpget -v2c -c public -ObentU -r 5 -t 1 195.218.195.228 1.3.6.1.2.1.1.1.0
+#
+from pysnmp.carrier.asyncio.dgram import udp
+from pysnmp.entity.rfc3413.asyncio import cmdgen
+from pysnmp.entity import engine, config
+import asyncio
+
+# Get the event loop for this thread
+loop = asyncio.get_event_loop()
+
+# Create SNMP engine instance
+snmpEngine = engine.SnmpEngine()
+
+#
+# SNMPv2c setup
+#
+
+# SecurityName <-> CommunityName mapping
+config.addV1System(snmpEngine, 'my-area', 'public')
+
+# Specify security settings per SecurityName (SNMPv1 - 0, SNMPv2c - 1)
+config.addTargetParams(snmpEngine, 'my-creds', 'my-area', 'noAuthNoPriv', 1)
+
+#
+# Setup transport endpoint and bind it with security settings yielding
+# a target name
+#
+
+# UDP/IPv4
+config.addTransport(
+ snmpEngine,
+ udp.domainName,
+ udp.UdpTransport().openClientMode()
+)
+config.addTargetAddr(
+ snmpEngine, 'my-router',
+ udp.domainName, ('195.218.195.228', 161),
+ 'my-creds',
+ timeout=300, # in 1/100 sec
+ retryCount=5
+)
+
+# Error/response receiver
+def cbFun(future):
+ (snmpEngine, errorIndication, errorStatus, errorIndex, varBinds) = future.result()
+ if errorIndication:
+ print(errorIndication)
+ elif errorStatus:
+ print('%s at %s' % (
+ errorStatus.prettyPrint(),
+ errorIndex and varBinds[int(errorIndex)-1][0] or '?'
+ )
+ )
+ else:
+ for oid, val in varBinds:
+ print('%s = %s' % (oid.prettyPrint(), val.prettyPrint()))
+
+ # This also terminates internal timer
+ config.delTransport(
+ snmpEngine,
+ udp.domainName
+ )
+
+ # Finish all scheduled tasks and end the loop
+ loop.stop()
+
+# Prepare request to be sent, receive an asyncio future object
+future = cmdgen.GetCommandGenerator().sendVarBinds(
+ snmpEngine,
+ 'my-router',
+ None, '', # contextEngineId, contextName
+ ( ('1.3.6.1.2.1.1.1.0', None), ),
+)
+
+# Register error/response receiver function on future
+future.add_done_callback(cbFun)
+
+# Run asyncio main loop
+loop.run_forever()
+
+# Clear the event loop
+loop.close()
diff --git a/examples/v3arch/asyncio/manager/cmdgen/getbulk-v2c.py b/examples/v3arch/asyncio/manager/cmdgen/getbulk-v2c.py new file mode 100644 index 0000000..0b40bd0 --- /dev/null +++ b/examples/v3arch/asyncio/manager/cmdgen/getbulk-v2c.py @@ -0,0 +1,109 @@ +# +# GETBULK Command Generator +# +# Send a series of SNMP GETBULK requests +# with SNMPv2c, community 'public' +# using Asyncio framework for network transport +# over IPv4/UDP +# to an Agent at 195.218.195.228:161 +# with values non-repeaters = 0, max-repetitions = 25 +# for two OIDs in tuple form +# stop on end-of-mib condition for both OIDs +# +# This script performs similar to the following Net-SNMP command: +# +# $ snmpbulkwalk -v2c -c public -C n0 -C r25 -ObentU 195.218.195.228 1.3.6.1.2.1.1 1.3.6.1.4.1.1 +# +from pysnmp.entity import engine, config +from pysnmp.proto import rfc1905 +from pysnmp.entity.rfc3413.asyncio import cmdgen +from pysnmp.carrier.asyncio.dgram import udp +import asyncio + +# Get the event loop for this thread +loop = asyncio.get_event_loop() + +# Create SNMP engine instance +snmpEngine = engine.SnmpEngine() + +# +# SNMPv2c setup +# + +# SecurityName <-> CommunityName mapping +config.addV1System(snmpEngine, 'my-area', 'public') + +# Specify security settings per SecurityName (SNMPv1 - 0, SNMPv2c - 1) +config.addTargetParams(snmpEngine, 'my-creds', 'my-area', 'noAuthNoPriv', 1) + +# +# Setup transport endpoint and bind it with security settings yielding +# a target name +# + +# UDP/IPv4 +config.addTransport( + snmpEngine, + udp.domainName, + udp.UdpTransport().openClientMode() +) +config.addTargetAddr( + snmpEngine, 'my-router', + udp.domainName, ('195.218.195.228', 161), + 'my-creds' +) + +# Error/response receiver +def cbFun(cbCtx): + (snmpEngine, errorIndication, + errorStatus, errorIndex, varBindTable, status) = cbCtx.result() + if errorIndication: + print(errorIndication) + elif errorStatus: + print('%s at %s' % ( + errorStatus.prettyPrint(), + errorIndex and varBindTable[-1][int(errorIndex)-1][0] or '?' + ) + ) + else: + for varBindRow in varBindTable: + for oid, val in varBindRow: + print('%s = %s' % (oid.prettyPrint(), val.prettyPrint())) + + # Stop mainloop when we are done walking (optional) + for oid, val in varBindRow: + if not val.isSameTypeWith(rfc1905.endOfMibView): + # Re-create future for next GETNEXT iteration + future = asyncio.Future() + future.add_done_callback(cbFun) + + # This also indicates that we wish to continue walking + status['future'] = future + return + + # This also terminates internal timer + config.delTransport( + snmpEngine, + udp.domainName + ) + + # Stop mainloop on SNMP error (optional) + loop.stop() + +# Prepare request to be sent, receive an asyncio future object +future = cmdgen.BulkCommandGenerator().sendVarBinds( + snmpEngine, + 'my-router', + None, '', # contextEngineId, contextName + 0, 25, # non-repeaters, max-repetitions + ( ('1.3.6.1.2.1.1', None), ('1.3.6.1.4.1.1', None) ) +) + +# Register error/response receiver function on future +future.add_done_callback(cbFun) + +# Run asyncio main loop +loop.run_forever() + +# Clear the event loop +loop.close() diff --git a/examples/v3arch/asyncio/manager/cmdgen/getnext-v2c-from-specific-address.py b/examples/v3arch/asyncio/manager/cmdgen/getnext-v2c-from-specific-address.py new file mode 100644 index 0000000..792487c --- /dev/null +++ b/examples/v3arch/asyncio/manager/cmdgen/getnext-v2c-from-specific-address.py @@ -0,0 +1,108 @@ +# +# GETNEXT Command Generator +# +# Send a series of SNMP GETNEXT requests +# with SNMPv2c, community 'public' +# using Asyncio framework for network transport +# over IPv4/UDP +# to an Agent at 195.218.195.228:161 +# sending packets from any local interface (0.0.0.0), local port 61024 +# for two OIDs in tuple form +# stop on end-of-mib condition for both OIDs +# +# This script performs similar to the following Net-SNMP command: +# +# $ snmpwalk -v2c -c public -ObentU 195.218.195.228 1.3.6.1.2.1.1 1.3.6.1.4.1.1 +# +from pysnmp.entity import engine, config +from pysnmp.proto import rfc1905 +from pysnmp.entity.rfc3413.asyncio import cmdgen +from pysnmp.carrier.asyncio.dgram import udp +import asyncio + +# Get the event loop for this thread +loop = asyncio.get_event_loop() + +# Create SNMP engine instance +snmpEngine = engine.SnmpEngine() + +# +# SNMPv2c setup +# + +# SecurityName <-> CommunityName mapping +config.addV1System(snmpEngine, 'my-area', 'public') + +# Specify security settings per SecurityName (SNMPv1 - 0, SNMPv2c - 1) +config.addTargetParams(snmpEngine, 'my-creds', 'my-area', 'noAuthNoPriv', 1) + +# +# Setup transport endpoint and bind it with security settings yielding +# a target name +# + +# UDP/IPv4 +config.addTransport( + snmpEngine, + udp.domainName, + udp.UdpTransport().openClientMode(('0.0.0.0', 61024)) +) +config.addTargetAddr( + snmpEngine, 'my-router', + udp.domainName, ('195.218.195.228', 161), + 'my-creds' +) + +# Error/response receiver +def cbFun(future): + (snmpEngine, errorIndication, + errorStatus, errorIndex, varBindTable, status) = future.result() + if errorIndication: + print(errorIndication) + elif errorStatus: + print('%s at %s' % ( + errorStatus.prettyPrint(), + errorIndex and varBindTable[-1][int(errorIndex)-1][0] or '?' + ) + ) + else: + for varBindRow in varBindTable: + for oid, val in varBindRow: + print('%s = %s' % (oid.prettyPrint(), val.prettyPrint())) + + # Stop mainloop when we are done walking (optional) + for oid, val in varBindRow: + if not val.isSameTypeWith(rfc1905.endOfMibView): + # Re-create future for next GETNEXT iteration + future = asyncio.Future() + future.add_done_callback(cbFun) + + # This also indicates that we wish to continue walking + status['future'] = future + return + + # This also terminates internal timer + config.delTransport( + snmpEngine, + udp.domainName + ) + + # Stop mainloop on SNMP error (optional) + loop.stop() + +# Prepare request to be sent, receive an asyncio future object +future = cmdgen.NextCommandGenerator().sendVarBinds( + snmpEngine, + 'my-router', + None, '', # contextEngineId, contextName + ( ('1.3.6.1.2.1.1', None), ('1.3.6.1.2.1.11', None) ) +) + +# Register error/response receiver function on future +future.add_done_callback(cbFun) + +# Run asyncio main loop +loop.run_forever() + +# Clear the event loop +loop.close() diff --git a/examples/v3arch/asyncio/manager/ntfrcv/v2c-multiple-interfaces.py b/examples/v3arch/asyncio/manager/ntfrcv/v2c-multiple-interfaces.py new file mode 100644 index 0000000..e84edc5 --- /dev/null +++ b/examples/v3arch/asyncio/manager/ntfrcv/v2c-multiple-interfaces.py @@ -0,0 +1,71 @@ +# +# Notification Receiver +# +# Receive SNMP TRAP/INFORM messages with the following options: +# +# * SNMPv1/SNMPv2c +# * with SNMP community "public" +# * over IPv4/UDP, listening at 127.0.0.1:162 +# over IPv4/UDP, listening at 127.0.0.1:2162 +# * using Asyncio framework for network transport +# * print received data on stdout +# +# Either of the following Net-SNMP's commands will send notifications to this +# receiver: +# +# $ snmptrap -v2c -c public 127.0.0.1:162 123 1.3.6.1.6.3.1.1.5.1 1.3.6.1.2.1.1.5.0 s test +# $ snmpinform -v2c -c public 127.0.0.1:2162 123 1.3.6.1.6.3.1.1.5.1 +# +from pysnmp.entity import engine, config +from pysnmp.carrier.asyncio.dgram import udp +from pysnmp.entity.rfc3413 import ntfrcv +import asyncio + +# Get the event loop for this thread +loop = asyncio.get_event_loop() + +# Create SNMP engine with autogenernated engineID and pre-bound +# to socket transport dispatcher +snmpEngine = engine.SnmpEngine() + +# Transport setup + +# UDP over IPv4, first listening interface/port +config.addTransport( + snmpEngine, + udp.domainName + (1,), + udp.UdpTransport().openServerMode(('127.0.0.1', 162)) +) + +# UDP over IPv4, second listening interface/port +config.addTransport( + snmpEngine, + udp.domainName + (2,), + udp.UdpTransport().openServerMode(('127.0.0.1', 2162)) +) + +# SNMPv1/2c setup + +# SecurityName <-> CommunityName mapping +config.addV1System(snmpEngine, 'my-area', 'public') + +# Callback function for receiving notifications +def cbFun(snmpEngine, + stateReference, + contextEngineId, contextName, + varBinds, + cbCtx): + transportDomain, transportAddress = snmpEngine.msgAndPduDsp.getTransportInfo(stateReference) + print('Notification from %s, SNMP Engine %s, Context %s' % ( + transportAddress, contextEngineId.prettyPrint(), + contextName.prettyPrint() + ) + ) + for name, val in varBinds: + print('%s = %s' % (name.prettyPrint(), val.prettyPrint())) + +# Register SNMP Application at the SNMP engine +ntfrcv.NotificationReceiver(snmpEngine, cbFun) + +# Run asyncio main loop +loop.run_forever() diff --git a/pysnmp/carrier/asyncio/__init__.py b/pysnmp/carrier/asyncio/__init__.py new file mode 100644 index 0000000..ac0b2c0 --- /dev/null +++ b/pysnmp/carrier/asyncio/__init__.py @@ -0,0 +1 @@ +# This file is necessary to make this directory a package.
diff --git a/pysnmp/carrier/asyncio/base.py b/pysnmp/carrier/asyncio/base.py new file mode 100644 index 0000000..192343f --- /dev/null +++ b/pysnmp/carrier/asyncio/base.py @@ -0,0 +1,35 @@ +# +# Copyright (C) 2014, Zebra Technologies +# Authors: Matt Hooks <me@matthooks.com> +# Zachary Lorusso <zlorusso@gmail.com> +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# +# * Redistributions of source code must retain the above copyright notice, +# this list of conditions and the following disclaimer. +# +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER +# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF +# THE POSSIBILITY OF SUCH DAMAGE. +# +from pysnmp.carrier.asyncio.dispatch import AsyncioDispatcher +from pysnmp.carrier.base import AbstractTransport + +class AbstractAsyncioTransport(AbstractTransport): + protoTransportDispatcher = AsyncioDispatcher + """Base Asyncio Transport, to be used with AsyncioDispatcher""" + def __init__(self): + self._writeQ = [] diff --git a/pysnmp/carrier/asyncio/dgram/__init__.py b/pysnmp/carrier/asyncio/dgram/__init__.py new file mode 100644 index 0000000..ac0b2c0 --- /dev/null +++ b/pysnmp/carrier/asyncio/dgram/__init__.py @@ -0,0 +1 @@ +# This file is necessary to make this directory a package.
diff --git a/pysnmp/carrier/asyncio/dgram/base.py b/pysnmp/carrier/asyncio/dgram/base.py new file mode 100644 index 0000000..c45e358 --- /dev/null +++ b/pysnmp/carrier/asyncio/dgram/base.py @@ -0,0 +1,75 @@ +# +# Copyright (C) 2014, Zebra Technologies +# Authors: Matt Hooks <me@matthooks.com> +# Zachary Lorusso <zlorusso@gmail.com> +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# +# * Redistributions of source code must retain the above copyright notice, +# this list of conditions and the following disclaimer. +# +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER +# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF +# THE POSSIBILITY OF SUCH DAMAGE. +# +from pysnmp.carrier.asyncio.base import AbstractAsyncioTransport +from pysnmp.carrier import error +from pysnmp import debug +try: + import asyncio +except ImportError: + from pysnmp.error import PySnmpError + raise PySnmpError('The asyncio transport is not available') + +loop = asyncio.get_event_loop() + +class DgramAsyncioProtocol(asyncio.DatagramProtocol, AbstractAsyncioTransport): + """Base Asyncio datagram Transport, to be used with AsyncioDispatcher""" + transport = None + + def datagram_received(self, datagram, transportAddress): + if self._cbFun is None: + raise error.CarrierError('Unable to call cbFun') + else: + loop.call_soon(self._cbFun, self, transportAddress, datagram) + + def connection_made(self, transport): + self.transport = transport + debug.logger & debug.flagIO and debug.logger('connection_made: invoked') + while self._writeQ: + outgoingMessage, transportAddress = self._writeQ.pop(0) + debug.logger & debug.flagIO and debug.logger('connection_made: transportAddress %r outgoingMessage %s' % + (transportAddress, debug.hexdump(outgoingMessage))) + try: + self.transport.sendto(outgoingMessage, transportAddress) + except Exception as err: + raise error.CarrierError() from err + + def connection_lost(self, exc): + debug.logger & debug.flagIO and debug.logger('connection_lost: invoked') + + def sendMessage(self, outgoingMessage, transportAddress): + debug.logger & debug.flagIO and debug.logger('sendMessage: %s transportAddress %r outgoingMessage %s' % ( + (self.transport is None and "queuing" or "sending"), + transportAddress, debug.hexdump(outgoingMessage) + )) + if self.transport is None: + self._writeQ.append((outgoingMessage, transportAddress)) + else: + try: + self.transport.sendto(outgoingMessage, transportAddress) + except Exception as err: + raise error.CarrierError() from err diff --git a/pysnmp/carrier/asyncio/dgram/udp.py b/pysnmp/carrier/asyncio/dgram/udp.py new file mode 100644 index 0000000..d29981a --- /dev/null +++ b/pysnmp/carrier/asyncio/dgram/udp.py @@ -0,0 +1,63 @@ +# +# Copyright (C) 2014, Zebra Technologies +# Authors: Matt Hooks <me@matthooks.com> +# Zachary Lorusso <zlorusso@gmail.com> +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# +# * Redistributions of source code must retain the above copyright notice, +# this list of conditions and the following disclaimer. +# +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER +# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF +# THE POSSIBILITY OF SUCH DAMAGE. +# +from pysnmp.carrier.asyncio.dgram.base import DgramAsyncioProtocol +from pysnmp.carrier import error +try: + import asyncio +except ImportError: + from pysnmp.error import PySnmpError + raise PySnmpError('The asyncio transport is not available') + +loop = asyncio.get_event_loop() + +domainName = snmpUDPDomain = (1, 3, 6, 1, 6, 1, 1) + +class UdpAsyncioTransport(DgramAsyncioProtocol): + # AbstractAsyncioTransport API + + def openClientMode(self, iface=('0.0.0.0', 0)): + try: + c = loop.create_datagram_endpoint(lambda: self, local_addr=iface) + self._lport = asyncio.async(c) + except Exception as err: + raise error.CarrierError() from err + return self + + def openServerMode(self, iface=('0.0.0.0', 161)): + try: + c = loop.create_datagram_endpoint(lambda: self, local_addr=iface) + self._lport = asyncio.async(c) + except Exception as err: + raise error.CarrierError() from err + return self + + def closeTransport(self): + self._lport.cancel() + DgramAsyncioProtocol.closeTransport(self) + +UdpTransport = UdpAsyncioTransport diff --git a/pysnmp/carrier/asyncio/dispatch.py b/pysnmp/carrier/asyncio/dispatch.py new file mode 100644 index 0000000..7b0b72f --- /dev/null +++ b/pysnmp/carrier/asyncio/dispatch.py @@ -0,0 +1,78 @@ +# +# Copyright (C) 2014, Zebra Technologies +# Authors: Matt Hooks <me@matthooks.com> +# Zachary Lorusso <zlorusso@gmail.com> +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# +# * Redistributions of source code must retain the above copyright notice, +# this list of conditions and the following disclaimer. +# +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER +# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF +# THE POSSIBILITY OF SUCH DAMAGE. +# +from pysnmp.carrier.base import AbstractTransportDispatcher +try: + import asyncio +except ImportError: + from pysnmp.error import PySnmpError + raise PySnmpError('The asyncio transport is not available') + +loop = asyncio.get_event_loop() + +class AsyncioDispatcher(AbstractTransportDispatcher): + """AsyncioDispatcher based on asyncio event loop""" + def __init__(self, *args, **kwargs): + AbstractTransportDispatcher.__init__(self) + self.__transportCount = 0 + if 'timeout' in kwargs: + self.setTimerResolution(kwargs['timeout']) + self.loopingcall = None + + @asyncio.coroutine + def handle_timeout(self): + while True: + yield from asyncio.sleep(self.getTimerResolution()) + self.handleTimerTick(loop.time()) + + def runDispatcher(self, timeout=0.0): + if not loop.is_running(): + try: + loop.run_forever() + except KeyboardInterrupt: + raise + except Exception as err: + raise PySnmpError('event loop error') from err + + def registerTransport(self, tDomain, transport): + if self.loopingcall is None and self.getTimerResolution() > 0: + self.loopingcall = asyncio.async(self.handle_timeout()) + AbstractTransportDispatcher.registerTransport( + self, tDomain, transport + ) + self.__transportCount = self.__transportCount + 1 + + def unregisterTransport(self, tDomain): + t = AbstractTransportDispatcher.getTransport(self, tDomain) + if t is not None: + AbstractTransportDispatcher.unregisterTransport(self, tDomain) + self.__transportCount = self.__transportCount - 1 + + # The last transport has been removed, stop the timeout + if self.__transportCount == 0 and not self.loopingcall.done(): + self.loopingcall.cancel() + self.loopingcall = None diff --git a/pysnmp/entity/rfc3413/asyncio/__init__.py b/pysnmp/entity/rfc3413/asyncio/__init__.py new file mode 100644 index 0000000..ac0b2c0 --- /dev/null +++ b/pysnmp/entity/rfc3413/asyncio/__init__.py @@ -0,0 +1 @@ +# This file is necessary to make this directory a package.
diff --git a/pysnmp/entity/rfc3413/asyncio/cmdgen.py b/pysnmp/entity/rfc3413/asyncio/cmdgen.py new file mode 100644 index 0000000..8a127c2 --- /dev/null +++ b/pysnmp/entity/rfc3413/asyncio/cmdgen.py @@ -0,0 +1,178 @@ +# +# Copyright (C) 2014, Zebra Technologies +# Authors: Matt Hooks <me@matthooks.com> +# Zachary Lorusso <zlorusso@gmail.com> +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# +# * Redistributions of source code must retain the above copyright notice, +# this list of conditions and the following disclaimer. +# +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER +# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF +# THE POSSIBILITY OF SUCH DAMAGE. +# +from pysnmp.entity.rfc3413 import cmdgen +from pyasn1.compat.octets import null +from pysnmp.proto.api import v2c +try: + import asyncio +except ImportError: + from pysnmp.error import PySnmpError + raise PySnmpError('The asyncio transport is not available') + +class AbstractCommandGenerator: + commandGenerator = None + + def _cbFunWithFuture(self, snmpEngine, sendRequestHandle, errorIndication, + errorStatus, errorIndex, varBinds, cbCtx): + future = cbCtx + future.set_result( + (snmpEngine, errorIndication, errorStatus, errorIndex, varBinds) + ) + + def sendVarBinds(self, snmpEngine, targetName, + contextEngineId, contextName, varBinds): + future = asyncio.Future() + self.commandGenerator.sendVarBinds( + snmpEngine, + targetName, + contextEngineId, + contextName, + varBinds, + self._cbFunWithFuture, + future + ) + return future + +class GetCommandGenerator(AbstractCommandGenerator): + commandGenerator = cmdgen.GetCommandGenerator() + +class SetCommandGenerator(AbstractCommandGenerator): + commandGenerator = cmdgen.SetCommandGenerator() + +class AbstractNextCommandGenerator: + commandGenerator = None + + def _cbFunWithFuture(self, snmpEngine, sendRequestHandle, errorIndication, + errorStatus, errorIndex, varBinds, cbCtx): + targetName, contextEngineId, contextName, reqVarBinds, futureCtx = cbCtx + status = {} + + def _nextCbFunClosure(): + def _cbFun(future): + if not status.get('future'): + return + errorIndication, nextVarBinds = cmdgen.getNextVarBinds( + [ (v2c.ObjectIdentifier(x[0]), x[1]) for x in reqVarBinds ], + varBinds[-1] + ) + self.commandGenerator.sendVarBinds( + snmpEngine, + targetName, + contextEngineId, + contextName, + nextVarBinds, + self._cbFunWithFuture, + (targetName, contextEngineId, contextName, nextVarBinds, { 'future': status['future']}) + ) + return _cbFun + + future = futureCtx['future'] + # add our own callback sending request for next vars if needed + future.add_done_callback(_nextCbFunClosure()) + future.set_result( + (snmpEngine, errorIndication, errorStatus, errorIndex, varBinds, status) + ) + + def sendVarBinds(self, snmpEngine, targetName, + contextEngineId, contextName, varBinds): + future = asyncio.Future() + self.commandGenerator.sendVarBinds( + snmpEngine, + targetName, + contextEngineId, + contextName, + varBinds, + self._cbFunWithFuture, + (targetName, contextEngineId, contextName, varBinds, { 'future': future }) + ) + return future + +class NextCommandGeneratorSingleRun(AbstractNextCommandGenerator): + commandGenerator = cmdgen.NextCommandGeneratorSingleRun() + +class NextCommandGenerator(AbstractNextCommandGenerator): + commandGenerator = cmdgen.NextCommandGenerator() + +class AbstractBulkCommandGenerator: + commandGenerator = None + + def _cbFunWithFuture(self, snmpEngine, sendRequestHandle, errorIndication, + errorStatus, errorIndex, varBinds, cbCtx): + targetName, contextEngineId, contextName, nonRepeaters, maxRepetitions, reqVarBinds, futureCtx = cbCtx + status = {} + + def _nextCbFunClosure(): + def _cbFun(future): + if not status.get('future'): + return + errorIndication, nextVarBinds = cmdgen.getNextVarBinds( + [ (v2c.ObjectIdentifier(x[0]), x[1]) for x in reqVarBinds ], + varBinds[-1] + ) + self.commandGenerator.sendVarBinds( + snmpEngine, + targetName, + contextEngineId, + contextName, + nonRepeaters, + maxRepetitions, + nextVarBinds, + self._cbFunWithFuture, + (targetName, contextEngineId, contextName, nonRepeaters, maxRepetitions, nextVarBinds, { 'future': status['future']}) + ) + return _cbFun + + future = futureCtx['future'] + # add our own callback sending request for next vars if needed + future.add_done_callback(_nextCbFunClosure()) + future.set_result( + (snmpEngine, errorIndication, errorStatus, errorIndex, varBinds, status) + ) + + def sendVarBinds(self, snmpEngine, targetName, + contextEngineId, contextName, + nonRepeaters, maxRepetitions, varBinds): + future = asyncio.Future() + self.commandGenerator.sendVarBinds( + snmpEngine, + targetName, + contextEngineId, + contextName, + nonRepeaters, + maxRepetitions, + varBinds, + self._cbFunWithFuture, + (targetName, contextEngineId, contextName, nonRepeaters, maxRepetitions, varBinds, { 'future': future }) + ) + return future + +class BulkCommandGeneratorSingleRun(AbstractBulkCommandGenerator): + commandGenerator = cmdgen.BulkCommandGeneratorSingleRun() + +class BulkCommandGenerator(AbstractBulkCommandGenerator): + commandGenerator = cmdgen.BulkCommandGenerator() diff --git a/pysnmp/entity/rfc3413/asyncio/ntforg.py b/pysnmp/entity/rfc3413/asyncio/ntforg.py new file mode 100644 index 0000000..716e524 --- /dev/null +++ b/pysnmp/entity/rfc3413/asyncio/ntforg.py @@ -0,0 +1,66 @@ +# +# Copyright (C) 2014, Zebra Technologies +# Authors: Matt Hooks <me@matthooks.com> +# Zachary Lorusso <zlorusso@gmail.com> +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# +# * Redistributions of source code must retain the above copyright notice, +# this list of conditions and the following disclaimer. +# +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER +# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF +# THE POSSIBILITY OF SUCH DAMAGE. +# +from pysnmp.entity.rfc3413 import ntforg +from pyasn1.compat.octets import null +try: + import asyncio +except ImportError: + from pysnmp.error import PySnmpError + raise PySnmpError('The asyncio transport is not available') + +def _cbFunWithFuture(snmpEngine, sendRequestHandle, errorIndication, + errorStatus, errorIndex, varBinds, cbCtx): + cbCtx.set_result( + (snmpEngine, errorIndication, errorStatus, errorIndex, varBinds) + ) + +class NotificationOriginator: + def __init__(self): + self.notificationOriginator = ntforg.NotificationOriginator() + + def sendVarBinds(self, + snmpEngine, + notificationTarget, + snmpContext, + contextName, + notificationName, + instanceIndex, + additionalVarBinds=()): + future = asyncio.Future() + self.notificationOriginator.sendVarBinds( + snmpEngine, + notificationTarget, + snmpContext, + contextName, + notificationName, + instanceIndex, + additionalVarBinds, + _cbFunWithFuture, + future + ) + return future @@ -98,10 +98,13 @@ params.update( { 'pysnmp.carrier.asynsock.dgram', 'pysnmp.carrier.twisted', 'pysnmp.carrier.twisted.dgram', + 'pysnmp.carrier.asyncio', + 'pysnmp.carrier.asyncio.dgram', 'pysnmp.entity', 'pysnmp.entity.rfc3413', 'pysnmp.entity.rfc3413.oneliner', 'pysnmp.entity.rfc3413.twisted', + 'pysnmp.entity.rfc3413.asyncio', 'pysnmp.proto', 'pysnmp.proto.mpmod', 'pysnmp.proto.secmod', |