From a0fd414a38cc6cf7d342974eea9a0f8f9d55799a Mon Sep 17 00:00:00 2001 From: elie Date: Wed, 7 Jan 2015 10:28:09 +0000 Subject: first attempt to make SNMP Engine settings persistent across reboots --- CHANGES | 2 ++ pysnmp/entity/engine.py | 36 +++++++++++++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/CHANGES b/CHANGES index 034d18f..1ce6719 100644 --- a/CHANGES +++ b/CHANGES @@ -62,6 +62,8 @@ Revision 4.2.6rc0 startup time. - Multiple fixes to verify pyasn1 decoder.decode() return to withstand broken SNMP messages or its components. +- First attempt made to make some of SNMP Engine settings persistent + across reboots. - Fix to authoritative engine side snmpEngineID discovery procedure: respond with notInTimeWindows rather then with unsupportedSecurityLevel at time synchronization phase. diff --git a/pysnmp/entity/engine.py b/pysnmp/entity/engine.py index a497e48..6076e19 100644 --- a/pysnmp/entity/engine.py +++ b/pysnmp/entity/engine.py @@ -1,4 +1,6 @@ # SNMP engine +import os +import tempfile from pysnmp.proto.rfc3412 import MsgAndPduDispatcher from pysnmp.proto.mpmod.rfc2576 import SnmpV1MessageProcessingModel, \ SnmpV2cMessageProcessingModel @@ -8,6 +10,7 @@ from pysnmp.proto.secmod.rfc2576 import SnmpV1SecurityModel, \ from pysnmp.proto.secmod.rfc3414 import SnmpUSMSecurityModel from pysnmp.proto.acmod import rfc3415, void from pysnmp.entity import observer +from pysnmp import debug from pysnmp import error class SnmpEngine: @@ -50,9 +53,40 @@ class SnmpEngine: snmpEngineBoots, = self.msgAndPduDsp.mibInstrumController.mibBuilder.importSymbols('__SNMP-FRAMEWORK-MIB', 'snmpEngineBoots') snmpEngineBoots.syntax = snmpEngineBoots.syntax + 1 origSnmpEngineID, = self.msgAndPduDsp.mibInstrumController.mibBuilder.importSymbols('__SNMP-FRAMEWORK-MIB', 'snmpEngineID') + if snmpEngineID is not None: origSnmpEngineID.syntax = origSnmpEngineID.syntax.clone(snmpEngineID) - self.snmpEngineID = origSnmpEngineID.syntax + self.snmpEngineID = origSnmpEngineID.syntax + + debug.logger & debug.flagApp and debug.logger('SnmpEngine: using custom SNMP Engine ID: %s' % self.snmpEngineID.prettyPrint()) + + # Attempt to make some of snmp Engine settings persistent. + # This should probably be generalized as a non-volatile MIB store. + + persistentPath = os.path.join(tempfile.gettempdir(), '__pysnmp', self.snmpEngineID.prettyPrint()) + + debug.logger & debug.flagApp and debug.logger('SnmpEngine: using persistent directory: %s' % persistentPath) + + if not os.path.exists(persistentPath): + try: + os.makedirs(persistentPath) + except OSError: + return + + f = os.path.join(persistentPath, 'boots') + try: + snmpEngineBoots.syntax = snmpEngineBoots.syntax.clone(open(f).read()) + except: + pass + + snmpEngineBoots.syntax = snmpEngineBoots.syntax + 1 + + try: + open(f, 'w').write(snmpEngineBoots.syntax.prettyPrint()) + except: + pass + + debug.logger & debug.flagApp and debug.logger('SnmpEngine: stored SNMP Engine Boots: %s' % snmpEngineBoots.syntax.prettyPrint()) # Transport dispatcher bindings -- cgit v1.2.1