From a05357c2daf85a8f9bd53cdc9ac1110ececfee95 Mon Sep 17 00:00:00 2001 From: elie Date: Tue, 11 Oct 2005 19:25:16 +0000 Subject: initial revision --- examples/v3arch/manager/ntfrcv.py | 37 ++++++++++++++ examples/v3arch/oneliner/agent/ntforg.py | 16 ++++++ pysnmp/entity/rfc3413/ntfrcv.py | 87 ++++++++++++++++++++++++++++++++ 3 files changed, 140 insertions(+) create mode 100644 examples/v3arch/manager/ntfrcv.py create mode 100644 examples/v3arch/oneliner/agent/ntforg.py create mode 100644 pysnmp/entity/rfc3413/ntfrcv.py diff --git a/examples/v3arch/manager/ntfrcv.py b/examples/v3arch/manager/ntfrcv.py new file mode 100644 index 0000000..b4cd5a3 --- /dev/null +++ b/examples/v3arch/manager/ntfrcv.py @@ -0,0 +1,37 @@ +"""Notification Receiver Application (TRAP/INFORM PDU)""" +from pysnmp.entity import engine, config +from pysnmp.carrier.asynsock.dgram import udp +from pysnmp.entity.rfc3413 import ntfrcv + +# Create SNMP engine with autogenernated engineID and pre-bound +# to socket transport dispatcher +snmpEngine = engine.SnmpEngine() + +# Setup transport endpoint +config.addSocketTransport( + snmpEngine, + udp.domainName, + udp.UdpSocketTransport().openServerMode(('127.0.0.1', 1162)) + ) + +# v1/2 setup +config.addV1System(snmpEngine, 'test-agent', 'public') + +# v3 setup +config.addV3User( + snmpEngine, 'test-user', + config.usmHMACMD5AuthProtocol, 'authkey1', + config.usmDESPrivProtocol, 'privkey1' + ) + +# Callback function for receiving notifications +def cbFun(snmpEngine, + contextEngineID, contextName, + varBinds, + cbCtx): + print contextEngineID, contextName, varBinds + +# Apps registration +ntfrcv.NotificationReceiver(snmpEngine, cbFun) +snmpEngine.transportDispatcher.jobStarted(1) # this job would never finish +snmpEngine.transportDispatcher.runDispatcher() diff --git a/examples/v3arch/oneliner/agent/ntforg.py b/examples/v3arch/oneliner/agent/ntforg.py new file mode 100644 index 0000000..a5576f1 --- /dev/null +++ b/examples/v3arch/oneliner/agent/ntforg.py @@ -0,0 +1,16 @@ +from pysnmp.entity.rfc3413.oneliner import ntforg +from pysnmp.proto.api import v2c + +errorIndication = ntforg.NotificationOriginator().sendNotification( + # SNMP v1 + ntforg.CommunityData('test-manager', 'public', 0), + # SNMP v2 +# ntforg.CommunityData('test-manager', 'public'), + # SNMP v3 +# ntforg.UsmUserData('test-manager', 'authkey1', 'privkey1'), + ntforg.UdpTransportTarget(('localhost', 1162)), + ('SNMPv2-MIB', 'coldStart')#, ((1,3,6,1,2,1,1,3,0), v2c.Integer(32)) + ) + +if errorIndication: + print errorIndication diff --git a/pysnmp/entity/rfc3413/ntfrcv.py b/pysnmp/entity/rfc3413/ntfrcv.py new file mode 100644 index 0000000..f8b256b --- /dev/null +++ b/pysnmp/entity/rfc3413/ntfrcv.py @@ -0,0 +1,87 @@ +from pysnmp.proto import rfc3411, error +from pysnmp.proto.api import v1, v2c # backend is always SMIv2 compliant +from pysnmp.proto.proxy import rfc2576 + +# 3.4 +class NotificationReceiver: + pduTypes = ( + v1.TrapPDU.tagSet, + v2c.SNMPv2TrapPDU.tagSet, + v2c.InformRequestPDU.tagSet + ) + + def __init__(self, snmpEngine, cbFun, cbCtx=None): + snmpEngine.msgAndPduDsp.registerContextEngineId( + '', self.pduTypes, self.processPdu # '' is a wildcard + ) + self.__cbFun = cbFun + self.__cbCtx = cbCtx + + def close(self, snmpEngine): + snmpEngine.msgAndPduDsp.unregisterContextEngineId( + self.snmpContext.contextEngineId, self.pduTypes + ) + + def processPdu( + self, + snmpEngine, + messageProcessingModel, + securityModel, + securityName, + securityLevel, + contextEngineID, + contextName, + pduVersion, + PDU, + maxSizeResponseScopedPDU, + stateReference + ): + + # Agent-side API complies with SMIv2 + if messageProcessingModel == 0: + PDU = rfc2576.v1ToV2(PDU) + + errorStatus = 'noError'; errorIndex = 0 + varBinds = v2c.apiPDU.getVarBinds(PDU) + + # 3.4 + if rfc3411.confirmedClassPDUs.has_key(PDU.tagSet): + # 3.4.1 --> no-op + + # 3.4.2 + v2c.apiPDU.setErrorStatus(rspPDU, errorStatus) + v2c.apiPDU.setErrorIndex(rspPDU, errorIndex) + v2c.apiPDU.setVarBinds(rspPDU, varBinds) + + # Agent-side API complies with SMIv2 + if messageProcessingModel == 0: + rspPDU = rfc2576.v2ToV1(rspPDU) + + # 3.4.3 + try: + snmpEngine.msgAndPduDsp.returnResponsePdu( + snmpEngine, + messageProcessingModel, + securityModel, + securityName, + securityLevel, + contextEngineID, + contextName, + pduVersion, + rspPDU, + maxSizeResponseScopedPDU, + stateReference, + statusInformation + ) + except error.StatusInformation: + snmpSilentDrops, = snmpEngine.msgAndPduDsp.mibInstrumController.mibBuilder.importSymbols('SNMPv2-MIB', 'snmpSilentDrops') + snmpSilentDrops.syntax = snmpSilentDrops.syntax + 1 + + elif rfc3411.unconfirmedClassPDUs.has_key(PDU.tagSet): + pass + else: + raise error.ProtocolError('Unexpected PDU class %s' % PDU.tagSet) + + self.__cbFun( + self.snmpEngine, contextEngineID, contextName, varBinds, cbCtx + ) -- cgit v1.2.1