def parse_alert(self, context, storage_id, alert): """Parse alert data got from snmp trap server.""" access_info = db.access_info_get(context, storage_id) driver = self.driver_manager.get_driver(context, invoke_on_load=False, **access_info) return driver.parse_alert(context, alert)
def _get_driver_obj(self, context, cache_on_load=True, **kwargs): if not cache_on_load or not kwargs.get('storage_id'): if kwargs['verify']: ssl_utils.reload_certificate(kwargs['verify']) cls = self._get_driver_cls(**kwargs) return cls(**kwargs) if kwargs['storage_id'] in self.driver_factory: return self.driver_factory[kwargs['storage_id']] with self._instance_lock: if kwargs['storage_id'] in self.driver_factory: return self.driver_factory[kwargs['storage_id']] if kwargs['verify']: ssl_utils.reload_certificate(kwargs['verify']) access_info = copy.deepcopy(kwargs) storage_id = access_info.pop('storage_id') access_info.pop('verify') if access_info: cls = self._get_driver_cls(**kwargs) driver = cls(**kwargs) else: access_info = db.access_info_get(context, storage_id).to_dict() access_info['verify'] = kwargs.get('verify') cls = self._get_driver_cls(**access_info) driver = cls(**access_info) self.driver_factory[storage_id] = driver return driver
def get_access_info(context, storage_id): access_info = db.access_info_get(context, storage_id) access_info_dict = access_info.to_dict() for access in constants.ACCESS_TYPE: if access_info.get(access): access_info[access]['password'] = cryptor.decode( access_info[access]['password']) return access_info_dict
def update(self, req, id, body): """Update storage access information.""" ctxt = req.environ.get('delfin.context') access_info = db.access_info_get(ctxt, id) access_info.update(body) access_info = self.driver_api.update_access_info(ctxt, access_info) return self._view_builder.show(access_info)
def update(self, req, id, body): """Update storage access information.""" ctxt = req.environ.get('delfin.context') access_info = db.access_info_get(ctxt, id) for access in constants.ACCESS_TYPE: if not body.get(access): body[access] = None access_info.update(body) access_info = self.driver_api.update_access_info(ctxt, access_info) return self._view_builder.show(access_info)
def _get_driver_obj(self, context, cache_on_load=True, **kwargs): if not cache_on_load or not kwargs.get('storage_id'): if kwargs['verify']: ssl_utils.reload_certificate(kwargs['verify']) cls = self._get_driver_cls(**kwargs) return cls(**kwargs) if kwargs['storage_id'] in self.driver_factory: return self.driver_factory[kwargs['storage_id']] with self._instance_lock: if kwargs['storage_id'] in self.driver_factory: return self.driver_factory[kwargs['storage_id']] if kwargs['verify']: ssl_utils.reload_certificate(kwargs['verify']) access_info = copy.deepcopy(kwargs) storage_id = access_info.pop('storage_id') access_info.pop('verify') if access_info: cls = self._get_driver_cls(**kwargs) driver = cls(**kwargs) else: access_info = db.access_info_get(context, storage_id).to_dict() access_info_dict = copy.deepcopy(access_info) remove_fields = [ 'created_at', 'updated_at', 'storage_id', 'storage_name', 'extra_attributes' ] # Remove unrelated query fields for field in remove_fields: if access_info_dict.get(field): access_info_dict.pop(field) for access in constants.ACCESS_TYPE: if access_info_dict.get(access): access_info_dict.pop(access) access_info_list = db.access_info_get_all( context, filters=access_info_dict) for _access_info in access_info_list: if _access_info['storage_id'] in self.driver_factory: driver = self.driver_factory[ _access_info['storage_id']] driver.add_storage(access_info) self.driver_factory[storage_id] = driver return driver access_info['verify'] = kwargs.get('verify') cls = self._get_driver_cls(**access_info) driver = cls(**access_info) self.driver_factory[storage_id] = driver return driver
def update(self, req, id, body): """Update storage access information.""" ctxt = req.environ.get('delfin.context') access_info = db.access_info_get(ctxt, id) for access in constants.ACCESS_TYPE: if access_info.get(access): access_info[access]['password'] = cryptor.decode( access_info[access]['password']) if body.get(access): access_info[access].update(body[access]) access_info = self.driver_api.update_access_info(ctxt, access_info) return self._view_builder.show(access_info)
def show(self, req, id): """Show access information by storage id.""" ctxt = req.environ['delfin.context'] access_info = db.access_info_get(ctxt, id) return self._view_builder.show(access_info)
def get_alert_sources(self, context, storage_id): access_info = db.access_info_get(context, storage_id) driver = self.driver_manager.get_driver(context, cache_on_load=False, **access_info) return driver.get_alert_sources(context)
def validate_connectivity(ctxt, alert_source): # Fill optional parameters with default values if not set in input if not alert_source.get('port'): alert_source['port'] = constants.DEFAULT_SNMP_CONNECT_PORT if not alert_source.get('context_name'): alert_source['context_name'] = None if not alert_source.get('retry_num'): alert_source['retry_num'] = constants.DEFAULT_SNMP_RETRY_NUM if not alert_source.get('expiration'): alert_source['expiration'] = constants.DEFAULT_SNMP_EXPIRATION_TIME if CONF.snmp_validation_enabled is False: return alert_source storage_id = alert_source.get('storage_id') access_info = db.access_info_get(ctxt, storage_id) access_info = dict(access_info) if access_info.get('model') not in constants.SNMP_SUPPORTED_MODELS: return alert_source cmd_gen = cmdgen.CommandGenerator() version = alert_source.get('version') # Connect to alert source through snmp get to check the configuration try: target = cmdgen.UdpTransportTarget( (alert_source['host'], alert_source['port']), timeout=alert_source['expiration'], retries=alert_source['retry_num']) target.setLocalAddress((CONF.my_ip, 0)) if version.lower() == 'snmpv3': # Register engine observer to get engineId, # Code reference from: http://snmplabs.com/pysnmp/ observer_context = {} cmd_gen.snmpEngine.observer.registerObserver( lambda e, p, v, c: c.update(securityEngineId=v[ 'securityEngineId']), 'rfc3412.prepareDataElements:internal', cbCtx=observer_context) auth_key = None if alert_source['auth_key']: auth_key = encodeutils.to_utf8( cryptor.decode(alert_source['auth_key'])) privacy_key = None if alert_source['privacy_key']: privacy_key = encodeutils.to_utf8( cryptor.decode(alert_source['privacy_key'])) auth_protocol = None privacy_protocol = None if alert_source['auth_protocol']: auth_protocol = constants.AUTH_PROTOCOL_MAP.get( alert_source['auth_protocol'].lower()) if alert_source['privacy_protocol']: privacy_protocol = constants.PRIVACY_PROTOCOL_MAP.get( alert_source['privacy_protocol'].lower()) engine_id = alert_source.get('engine_id') if engine_id: engine_id = OctetString.fromHexString(engine_id) error_indication, __, __, __ = cmd_gen.getCmd( cmdgen.UsmUserData(alert_source['username'], authKey=auth_key, privKey=privacy_key, authProtocol=auth_protocol, privProtocol=privacy_protocol, securityEngineId=engine_id), target, constants.SNMP_QUERY_OID, ) if 'securityEngineId' in observer_context: engine_id = observer_context.get('securityEngineId') alert_source['engine_id'] = binascii.hexlify( engine_id.asOctets()).decode() else: community_string = encodeutils.to_utf8( cryptor.decode(alert_source['community_string'])) error_indication, __, __, __ = cmd_gen.getCmd( cmdgen.CommunityData( community_string, contextName=alert_source['context_name']), target, constants.SNMP_QUERY_OID, ) cmd_gen.snmpEngine.transportDispatcher.closeDispatcher() if not error_indication: return alert_source # Prepare exception with error_indication msg = six.text_type(error_indication) except Exception as e: msg = six.text_type(e) # Since validation occur error, raise exception LOG.error("Configuration validation failed with alert source for " "reason: %s." % msg) raise exception.SNMPConnectionFailed(msg)