From f849e7306a11b5858725c312ec56b096b7d7eda3 Mon Sep 17 00:00:00 2001
From: elie
-The MibVariable object is used on input to -allow symbolic MIB table columns specification. + The MibVariable object is used on input to + allow symbolic MIB table columns specification.
@@ -1081,8 +1081,8 @@ for as long as possible within user applicatin.
-The following method of NotificationOriginator class instance -implements specific notifications types. +All notifications are sent by in invocation of the following method: + @@ -1116,9 +1116,54 @@ unconfirmed notification or "inform" for a confirmed one.The notificationType parameter indicates the kind of event to notify Manager about in form of SMI NOTIFICATION-TYPE object -name. For instance, (('SNMPv2-MIB', 'coldStart'),) or (1,3,6,1,6,3,1,1,5,1) -is a value of coldStart notification type as defined in SNMPv2-MIB module. - +name. Either +ObjectIdentifier class +instance, its initialization value (like '1.3.6.1.6.3.1.1.5.1') or +MibVariable object can be used on input to +allow MIB symbols references. +For example, '1.3.6.1.6.3.1.1.5.1' or MibVariable('SNMPv2-MIB', 'coldStart') +specify SNMPv2-MIB::coldStart type of trap. + + ++When sending SNMP v1 traps, the notificationType +parameter encodes both Generic and Specific trap numbers +hardwired into SNMP v1 TRAP PDU, but missing in SNMP v2c TRAP and INFORM +PDUs. + + +
The *varBinds input parameter is a tuple of Managed @@ -1133,11 +1178,31 @@ The sendNotification method returns an in getCmd method. ++When sending SNMP traps to a SNMPv1 system, PDU parameters +that are present in SNMPv1 PDU but are missing in SNMPv2c PDU +are mapped one to another via special Managed Objects Inctance +values in *varBinds. + + +
-The following code sends SNMP TRAP message: +The following code sends SNMP v2c TRAP message:
|
+The following code sends SNMP v1 TRAP message: +
++from pysnmp.entity.rfc3413.oneliner import ntforg +from pysnmp.proto import rfc1902 + +ntfOrg = ntforg.NotificationOriginator() + +errorIndication = ntfOrg.sendNotification( + ntforg.CommunityData('public', mpModel=0), + ntforg.UdpTransportTarget(('localhost', 162)), + 'trap', + '1.3.6.1.4.1.20408.4.1.1.2.0.432', + ('1.3.6.1.2.1.1.3.0', 12345), + ('1.3.6.1.6.3.18.1.3.0', '127.0.0.1'), + ('1.3.6.1.6.3.1.1.4.3.0', '1.3.6.1.4.1.20408.4.1.1.2'), + ('1.3.6.1.2.1.1.1.0', rfc1902.OctetString('my system')) +) + +if errorIndication: + print('Notification not sent: %s' % errorIndication) ++ |
Create an asynchronous SNMP Command Generator object.
+ ++Although instantiation of this class is cheap, in the course of its further +use, SNMP engine configuration is built and maintained though methods +invocation. +Therefore it is advised to keep and reuse CommandGenerator instance +(or snmpEngine instance if passed as an initializer) +for as long as possible within user applicatin. +
+ @@ -1220,7 +1332,7 @@ Prepare SNMP GET request to be dispatched. Return theThe cbFun parameter is a reference to a callable object -(such as Python function) that takes the following parameters: +(such as a Python function) having the following signature:
-If cbFun has no more requests pending and want to complete, -it must return a true value. Otherwise, it returns false. -
The authData, transportTarget, varNames, lookupNames and @@ -1278,6 +1387,71 @@ matching subsequent response to this request. +
+The following code performs multiple, simultaneous SNMP GET operations +against distinct Agents identified by their transport addresses. +Authentication information and queried Managed Objects Instances used in +this example are the same for all targets. So the GET operation is performed: +
++from pysnmp.entity.rfc3413.oneliner import cmdgen + +def cbFun(sendRequestHandle, errorIndication, errorStatus, errorIndex, varBinds, cbCtx): + if errorIndication: + print(errorIndication) + return + if errorStatus: + print('%s at %s' % (errorStatus.prettyPrint(), + errorIndex and varBinds[int(errorIndex)-1] or '?') + ) + return + + for oid, val in varBinds: + if val is None: + print(oid.prettyPrint()) + else: + print('%s = %s' % (oid.prettyPrint(), val.prettyPrint())) + +cmdGen = cmdgen.AsynCommandGenerator() + +for transportTarget in ( cmdgen.UdpTransportTarget(('127.0.0.1', 161)), + cmdgen.UdpTransportTarget(('127.0.0.2', 161)), + cmdgen.UdpTransportTarget(('127.0.0.3', 161)) ): + cmdGen.getCmd( + cmdgen.CommunityData('public'), + cmdgen.UdpTransportTarget(('localhost', 161)), + ( cmdgen.MibVariable('SNMPv2-MIB', 'sysDescr', 0), + cmdgen.MibVariable('SNMPv2-MIB', 'sysLocation', 0) ), + # User-space callback function and its context + (cbFun, None), + lookupNames=True, lookupValues=True + ) + +cmdGen.snmpEngine.transportDispatcher.runDispatcher() ++ |
+It is trivial to modify the above code to make it using different +SNMP versions, credentials and query different Managed Objects Instances +per each target. +
+ ++All queries are made in parallel, so with default timeout and retries +settings, the above code will terminate in 6 seconds regardless of +Agents avialability and responsiveness. +
+@@ -1384,8 +1562,8 @@ Managed Objects Instances names.
The cbFun and cbCtx parameters -have the same meaning as in -AsynCommandGenerator.asyncGetCmd method. +have the same meaning as in +AsynCommandGenerator.asyncNextCmd method.
-- cgit v1.2.1