From 7f2c38e19246434be8eb6fb55ec8878ea0f9ca27 Mon Sep 17 00:00:00 2001 From: sushanthakumar Date: Tue, 21 Apr 2020 00:41:06 +0530 Subject: [PATCH] Cleanup of initial commit --- dolphin/alert_manager/constants.py | 24 ++++---- dolphin/alert_manager/trap_receiver.py | 85 ++++++++++++-------------- dolphin/service.py | 23 ++----- 3 files changed, 58 insertions(+), 74 deletions(-) diff --git a/dolphin/alert_manager/constants.py b/dolphin/alert_manager/constants.py index 6b22b5290..549f93026 100644 --- a/dolphin/alert_manager/constants.py +++ b/dolphin/alert_manager/constants.py @@ -1,17 +1,16 @@ -# Copyright 2013 OpenStack Foundation -# All Rights Reserved. +# Copyright 2018 The OpenSDS Authors. # -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at # -# http://www.apache.org/licenses/LICENSE-2.0 +# http:#www.apache.org/licenses/LICENSE-2.0 # -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. # Default values for trap receiver ip address and port @@ -27,3 +26,6 @@ SNMP_V3_AUTH_PROTOCOL="usmHMACMD5AuthProtocol" SNMP_V3_PRIV_PROTOCOL="usmDESPrivProtocol" SNMP_ENGINE_ID="800000d30300000e112245" + +# Temporary mib lod dir. This mechanism to be changed later +MIB_DIR_PATH = '/usr/local/lib/python3.6/dist-packages/pysnmp/smi/mibs' diff --git a/dolphin/alert_manager/trap_receiver.py b/dolphin/alert_manager/trap_receiver.py index 2fc736bad..411b1987a 100644 --- a/dolphin/alert_manager/trap_receiver.py +++ b/dolphin/alert_manager/trap_receiver.py @@ -1,21 +1,19 @@ -# Copyright (c) 2014 NetApp Inc. -# All Rights Reserved. +# Copyright 2018 The OpenSDS Authors. # -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at # -# http://www.apache.org/licenses/LICENSE-2.0 +# http:#www.apache.org/licenses/LICENSE-2.0 # -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. -""" +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +""" **trap receiver** - """ from oslo_log import log @@ -25,35 +23,35 @@ from pysnmp.proto.api import v2c from pysnmp.smi import builder, view, rfc1902, error -from dolphin import manager from dolphin.alert_manager import constants LOG = log.getLogger(__name__) -#Currently one mib file is loaded, All files to be loaded -LOAD_MIB_MODULE = 'SNMPv2-MIB' +# Currently static mib file list is loaded, logic to be changed to load all mib file +MIB_LOAD_LIST = ['SNMPv2-MIB','IF_MIB'] -class TrapReceiver(manager.Manager): - """Trap receiver functions""" +class TrapReceiver(object): - def __init__(self,receiverIpAddr=None, *args, **kwargs): - super(TrapReceiver, self).__init__(*args, **kwargs) + """Trap receiver functions""" + def __init__(self, mibViewController=None, snmpEngine=None): + self.mibViewController = mibViewController + self.snmpEngine = snmpEngine - def _mib_builder(self, load_mibs_list): + # Loads the list of mib files provided + def _mib_builder(self): mibBuilder = builder.MibBuilder() try: - global mibViewController - mibViewController = view.MibViewController(mibBuilder) - if load_mibs_list: - _mibs = LOAD_MIB_MODULE.split(",") - mibBuilder.loadModules(*_mibs) + self.mibViewController = view.MibViewController(mibBuilder) + + mib_list = MIB_LOAD_LIST + if (len(mib_list) > 0): + mibBuilder.loadModules_new(*mib_list) + except error.MibNotFoundError as excep: - LOG.info(" {} Mib Not Found!".format(excep)) + LOG.exception("Mib load failed") + # Configures the transport parameters for the snmp engine def _add_transport(self): - """ - :return: - """ try: config.addTransport( self.snmpEngine, @@ -61,11 +59,11 @@ def _add_transport(self): udp.UdpTransport().openServerMode((constants.DEF_TRAP_RECV_ADDR, int(constants.DEF_TRAP_RECV_PORT))) ) except Exception as e: - LOG.info("{} Port Binding Failed the Provided Port {} is in Use".format(e, self.receiverPort)) + LOG.exception("Port binding failed the provided port is in use") + # Callback function to process the incoming trap def _cbFun(self, stateReference, contextEngineId, contextName, varBinds, cbCtx): - global mibViewController LOG.info("####################### NEW Notification #######################") execContext = self.snmpEngine.observer.getExecutionContext('rfc3412.receiveMessage:request') LOG.info( @@ -74,16 +72,13 @@ def _cbFun(self, stateReference, contextEngineId, contextName, contextName.prettyPrint(), execContext['securityModel'], execContext['securityName'])) for oid, val in varBinds: output = rfc1902.ObjectType(rfc1902.ObjectIdentity(oid), val).resolveWithMib( - mibViewController).prettyPrint() + self.mibViewController).prettyPrint() LOG.info(output) - def _configure_snmp_userpara(self): - """ - :return: - """ - - COMMUNITYSTRING = constants.SNMP_COMMUNITY_STR - config.addV1System(self.snmpEngine, COMMUNITYSTRING, COMMUNITYSTRING) + # Configures snmp v2 and v3 user parameters + def _snmp_v2v3_config(self): + community_str = constants.SNMP_COMMUNITY_STR + config.addV1System(self.snmpEngine, community_str, community_str) __authProtocol = { 'usmHMACMD5AuthProtocol': config.usmHMACMD5AuthProtocol, 'usmHMACSHAAuthProtocol': config.usmHMACSHAAuthProtocol, @@ -94,8 +89,6 @@ def _configure_snmp_userpara(self): 'usmNoAuthProtocol': config.usmNoAuthProtocol, 'usmNoPrivProtocol': config.usmNoPrivProtocol } - - config.addV3User( self.snmpEngine, userName=constants.SNMP_USM_USER, authKey=constants.SNMP_V3_AUTHKEY, privKey=constants.SNMP_V3_PRIVKEY, @@ -108,14 +101,15 @@ def _configure_snmp_userpara(self): return + # Triggers the snmp trap receiver def start_trap_receiver(self): snmpEngine = engine.SnmpEngine() self.snmpEngine = snmpEngine - # Load all the mibs - self._mib_builder(LOAD_MIB_MODULE) + # Load all the mibs and do snmp config + self._mib_builder() - self._configure_snmp_userpara() + self._snmp_v2v3_config() # Register callback for notification receiver ntfrcv.NotificationReceiver(snmpEngine, self._cbFun) @@ -131,6 +125,7 @@ def start_trap_receiver(self): snmpEngine.transportDispatcher.closeDispatcher() raise + # Stops the snmp trap receiver def stop_trap_receiver(self): if self.snmpEngine: self.snmpEngine.transportDispatcher.closeDispatcher() diff --git a/dolphin/service.py b/dolphin/service.py index 798c9bc26..1a4c54350 100644 --- a/dolphin/service.py +++ b/dolphin/service.py @@ -220,26 +220,13 @@ class AlertMngrService(service.Service): """Service object for triggering trap receiver functionalities. """ - def __init__(self, host=None, manager=None, service_name=None, receiverIpAddr=None, receiverPort=None, coordination=False, - *args, **kwargs): + def __init__(self): super(AlertMngrService, self).__init__() - if not rpc.initialized(): - rpc.init(CONF) - if not host: - host = CONF.host - if not manager: - manager = constants.TRAP_RECEIVER_CLASS - self.host = host - self.snmpEngine = None - self.manager_class_name = manager - self.service_name = service_name - self.receiverIpAddr = getattr(CONF, 'trap_receiver_addr', constants.DEF_TRAP_RECV_ADDR) - self.receiverPort = getattr(CONF, 'trap_receiver_port', constants.DEF_TRAP_RECV_PORT) - manager_class = importutils.import_class(self.manager_class_name) - self.manager = manager_class(host=self.host, - receiverIpAddr=self.receiverIpAddr, - *args, **kwargs) + self.manager_class_name = constants.TRAP_RECEIVER_CLASS + manager_class = importutils.import_class(constants.TRAP_RECEIVER_CLASS) + self.manager = manager_class() + def start(self): """Trigger trap receiver creation""" self.manager.start_trap_receiver()