Skip to content

Commit

Permalink
Cleanup of initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
sushanthakumar committed Apr 20, 2020
1 parent e759dcc commit 7f2c38e
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 74 deletions.
24 changes: 13 additions & 11 deletions dolphin/alert_manager/constants.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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'
85 changes: 40 additions & 45 deletions dolphin/alert_manager/trap_receiver.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -25,47 +23,47 @@
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,
udp.domainName,
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(
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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)
Expand All @@ -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()
Expand Down
23 changes: 5 additions & 18 deletions dolphin/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 7f2c38e

Please sign in to comment.