summaryrefslogtreecommitdiff
path: root/examples/v3arch/asyncio/agent/ntforg/trap-v1.py
diff options
context:
space:
mode:
authorelie <elie>2014-11-16 16:14:37 +0000
committerelie <elie>2014-11-16 16:14:37 +0000
commitedff85c75a5ba51c6824f6d09ec55986682db335 (patch)
treec0216f83de60b93ef9c24ad801a47e2a14308b6e /examples/v3arch/asyncio/agent/ntforg/trap-v1.py
parent481200dc56925509a54b89c28d054ba28f4e437c (diff)
downloadpysnmp-edff85c75a5ba51c6824f6d09ec55986682db335.tar.gz
asyncio-backed SNMP Applications APIs redesigned for better usability in
form of coroutines
Diffstat (limited to 'examples/v3arch/asyncio/agent/ntforg/trap-v1.py')
-rw-r--r--examples/v3arch/asyncio/agent/ntforg/trap-v1.py120
1 files changed, 65 insertions, 55 deletions
diff --git a/examples/v3arch/asyncio/agent/ntforg/trap-v1.py b/examples/v3arch/asyncio/agent/ntforg/trap-v1.py
index 2dd6f4a..1406e32 100644
--- a/examples/v3arch/asyncio/agent/ntforg/trap-v1.py
+++ b/examples/v3arch/asyncio/agent/ntforg/trap-v1.py
@@ -69,64 +69,74 @@ config.addNotificationTarget(
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
+def snmpOperation(snmpEngine, target, snmpContext, contextName,
+ notificationName, instanceIndex, additionalVarBinds):
+ future = ntforg.NotificationOriginator().sendVarBinds(
+ snmpEngine,
+ target,
+ snmpContext,
+ contextName,
+ notificationName,
+ instanceIndex,
+ additionalVarBinds
+ )
+
+ # We know we are sending TRAP which will never produce any response.
+ # Therefore we simulate successful response here.
+ future.set_result((snmpEngine, None, 0, 0, ()))
+
+ ( snmpEngine,
+ errorIndication,
+ errorStatus,
+ errorIndex,
+ varBinds ) = yield from future
+
+ if errorIndication:
+ print(errorIndication)
+
+ # This also terminates internal timer
+ config.delTransport(
+ snmpEngine,
+ udp.domainName
+ )
+
+# Initiate sending SNMP message
+asyncio.async(
+ snmpOperation(
+ snmpEngine,
+ # Notification targets
+ 'my-notification',
+ # Default SNMP context where contextEngineId == SnmpEngineId
+ context.SnmpContext(snmpEngine),
+ # 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'))
+ ]
+ )
)
-# Finish all scheduled tasks and end the loop
-loop.stop()
+# Since SNMP TRAP's are unacknowledged, there's nothing to wait for. So we
+# simulate other loop activities by sleep()'ing.
+loop.run_until_complete(asyncio.sleep(1))
# Clear the event loop
loop.close()