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 /examples/v3arch/asyncio/agent/ntforg/trap-v1.py | |
parent | d947278e9747fc5d0c198f78b30132b13eb90760 (diff) | |
download | pysnmp-2badd13fbc5a16806170deb432a1aeee03b74905.tar.gz |
initial support for asyncio network transport added
Diffstat (limited to 'examples/v3arch/asyncio/agent/ntforg/trap-v1.py')
-rw-r--r-- | examples/v3arch/asyncio/agent/ntforg/trap-v1.py | 132 |
1 files changed, 132 insertions, 0 deletions
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() |