def setUp(self): output = StringIO() self.logger = Logger.get('test', output, OutputStream) store = MongoStore.get_default() self.collection = store.get_collection(name='default_test') self.mongo_collection = MongoCollection( collection=self.collection, logger=self.logger ) # Cleanup self.tearDown() self.manager = ActionManager( logger=self.logger, mongo_collection=self.mongo_collection ) self.id_ = 'testid' self.action = { "_id": self.id_, "type": "pbehavior", "fields": ["Resource"], "regex": ".*wine.*", "parameters": { "author": "Matho", "name": "Salammbo", "reason": "Madness", "type": "Mercenary War", "rrule": "" } }
def get_canopsis_version(): """ Get Canopsis version. :returns: ``200 OK`` if success or ``404 Not Found`` if Canopsis version info not found or ``400 Bad Request`` if database error. """ try: store = MongoStore.get_default() collection = \ store.get_collection(name=CanopsisVersionManager.COLLECTION) document = CanopsisVersionManager(collection).\ find_canopsis_version_document() except PyMongoError: return gen_json_error( {"description": "can not retrieve the canopsis version from " "database, contact your administrator."}, HTTP_ERROR) if not document: return gen_json_error( {"description": "canopsis version info not found."}, HTTP_NOT_FOUND) return gen_json( document, allowed_keys=[CanopsisVersionManager.VERSION_FIELD])
def setUp(self): output = StringIO() self.logger = Logger.get('test', output, OutputStream) store = MongoStore.get_default() self.collection = store.get_collection(name='default_test') self.mongo_collection = MongoCollection(collection=self.collection, logger=self.logger) # Cleanup self.tearDown() self.manager = ActionManager(logger=self.logger, mongo_collection=self.mongo_collection) self.id_ = 'testid' self.action = { "_id": self.id_, "hook": None, "type": "pbehavior", "fields": ["Resource"], "regex": ".*wine.*", "parameters": { "author": "Matho", "name": "Salammbo", "reason": "Madness", "type": "Mercenary War", "rrule": "" }, "delay": "" }
def setUp(self): super(TestReader, self).setUp() mongo = MongoStore.get_default() collection = mongo.get_collection("default_testpbehavior") pb_coll = MongoCollection(collection) self.logger = Logger.get('alertsreader', '/tmp/null') conf = Configuration.load(PBehaviorManager.CONF_PATH, Ini) self.pbehavior_manager = PBehaviorManager(config=conf, logger=self.logger, pb_collection=pb_coll) self.reader = AlertsReader(config=conf, logger=self.logger, storage=self.manager.alerts_storage, pbehavior_manager=self.pbehavior_manager) self.reader._alarm_fields = { 'properties': { 'connector': { 'stored_name': 'v.ctr' }, 'component': { 'stored_name': 'v.cpt' }, 'entity_id': { 'stored_name': 'd' } } }
def __init__(self, logger): self.logger = logger self.db_store = MongoStore.get_default() self.cache_store = RedisStore.get_default() self.amqp_url, self.amqp_exchange = AmqpConnection.parse_conf() self.ts_client = InfluxDBClient.from_configuration(self.logger)
def fill(self, init=None, yes=False, reinit_auth=False): self.__put_canopsis_document() tools = [] for module in self.modules: try: migrationcls = lookup(module) except ImportError as err: self.logger.error( 'Impossible to load module "{0}": {1}'.format(module, err)) continue migrationtool = migrationcls() migrationtool.logger.addHandler(self.loghandler) tools.append(migrationtool) coll = None if init is None: store = MongoStore.get_default() store.authenticate() coll = MongoCollection(store.get_collection(self.FLAG_COLLECTION)) data = coll.find_one({"_id": self.FLAG_COLLECTION}) if data is None: print("Database not intialized. Initializing...") init = True else: print("Database already intialized. Updating...") init = False if init is None and reinit_auth is False: data = { "_id": "initialized", "at": str(time.strftime("%a, %d %b %Y %H:%M:%S +0000")) } print("The canopsis initialization flag did not exist in the " "database. So canopsinit will (re?)initialized the " "database. Meaning, it may delete some important data " "from canopsis database. If you still want to initialize " "the database, call the same command with the " "`--authorize-reinit` flag. Or if you do not want to " "initialize the database, add the document `{0}` in the {1} " "collections.".format(data, self.FLAG_COLLECTION)) exit(1) for tool in tools: if init: tool.init(yes=yes) else: tool.update(yes=yes) if init is True: coll.insert({ "_id": self.FLAG_COLLECTION, "at": str(time.strftime("%a, %d %b %Y %H:%M:%S +0000")) })
def __init__(self, logger): self.logger = logger self.db_store = MongoStore.get_default() self.cache_store = RedisStore.get_default() self.amqp_url, self.amqp_exchange = AmqpConnection.parse_conf() self.ts_client = InfluxDBClient.from_configuration(self.logger)
def __init__(self, *args, **kargs): super(engine, self).__init__(*args, **kargs) self.mg_store = MongoStore.get_default() self.collection = MongoCollection(self.mg_store.get_collection("object")) self.name = kargs['name'] self.drop_event_count = 0 self.pass_event_count = 0 self.__load_rules()
def provide_default_basics(cls): """ Provide mongo collection. ! Do not use in tests ! :rtype: `~.common.collection.MongoCollection`. """ store = MongoStore.get_default() return (MongoCollection(store.get_collection(cls.COLLECTION)), )
def provide_default_basics(cls): """ Provide mongo collection. ! Do not use in tests ! :rtype: `~.common.collection.MongoCollection`. """ store = MongoStore.get_default() return (MongoCollection(store.get_collection(cls.COLLECTION)), )
def __init__(self, *args, **kargs): super(engine, self).__init__(*args, **kargs) self.mg_store = MongoStore.get_default() self.collection = MongoCollection( self.mg_store.get_collection("object")) self.name = kargs['name'] self.drop_event_count = 0 self.pass_event_count = 0 self.__load_rules()
def __put_canopsis_version_document(self): """ Put Canopsis version document if a ``__canopsis_version`` attribute is set. """ if self.__canopsis_version: store = MongoStore.get_default() collection = \ store.get_collection(CanopsisVersionManager.COLLECTION) CanopsisVersionManager(collection)\ .put_canopsis_version_document(self.__canopsis_version)
def default_collection(cls): """ Returns the default collection for the manager. ! Do not use in tests ! :rtype: canopsis.common.collection.MongoCollection """ store = MongoStore.get_default() collection = store.get_collection(name=cls.COLLECTION) return MongoCollection(collection)
def default_collection(cls): """ Returns the default collection for the manager. ! Do not use in tests ! :rtype: canopsis.common.collection.MongoCollection """ store = MongoStore.get_default() collection = store.get_collection(name=cls.COLLECTION) return MongoCollection(collection)
def __put_canopsis_document(self): """ Put Canopsis version document if a ``__canopsis_version`` attribute is set. """ if self.__canopsis_version: store = MongoStore.get_default() collection = \ store.get_collection(CanopsisVersionManager.COLLECTION) CanopsisVersionManager(collection)\ .put_canopsis_document(self.__canopsis_edition, self.__canopsis_stack, self.__canopsis_version)
def provide_default_basics(cls, logger): """ Returns the default collection for the manager. ! Do not use in tests ! :rtype: (canopsis.common.collection.MongoCollection, canopsis.common.amqp.AmqpPublisher) """ store = MongoStore.get_default() collection = store.get_collection(name=cls.COLLECTION) amqp_pub = AmqpPublisher(get_default_amqp_conn(), logger) return (MongoCollection(collection), amqp_pub)
def provide_default_basics(cls): """ Provide logger, config, storages... ! Do not use in tests ! :rtype: Union[logging.Logger, canopsis.common.collection.MongoCollection] """ store = MongoStore.get_default() collection = store.get_collection(name=cls.COLLECTION) return (Logger.get('action', cls.LOG_PATH), MongoCollection(collection))
def provide_default_basics(cls): """Provide logger and collection. ! Do not use in tests ! :rtype: Tuple[logging.Logger, canopsis.common.collection.MongoCollection] """ logger = Logger.get('dynamic_infos', cls.LOG_PATH) store = MongoStore.get_default() collection = store.get_collection(name=cls.COLLECTION) mongo_collection = MongoCollection(collection) return (logger, mongo_collection)
def provide_default_basics(cls): """ Provide logger, config, storages... ! Do not use in tests ! :rtype: Union[logging.Logger, canopsis.common.collection.MongoCollection] """ logger = Logger.get('ticketapi', cls.LOG_PATH) store = MongoStore.get_default() collection = store.get_collection(name=cls.ACTION_COLLECTION) mongo_collection = MongoCollection(collection) return (logger, mongo_collection)
def setUp(self): mongo = MongoStore.get_default() collection = mongo.get_collection("default_testpbehavior") pb_coll = MongoCollection(collection) entities_storage = Middleware.get_middleware_by_uri( 'storage-default-testentities://') logger = Logger.get('test_pb', None, output_cls=OutputNull) conf = Configuration.load(PBehaviorManager.CONF_PATH, Ini) self.pbm = PBehaviorManager(config=conf, logger=logger, pb_collection=pb_coll) self.context = ContextGraph(logger) self.context.ent_storage = entities_storage self.pbm.context = self.context
def __init__(self, logger, pb_storage): """ :param dict config: configuration :param pb_storage: PBehavior Storage object """ super(PBehaviorManager, self).__init__() kwargs = {"logger": logger} self.context = singleton_per_scope(ContextGraph, kwargs=kwargs) self.logger = logger self.pb_storage = pb_storage self.pb_store = MongoCollection( MongoStore.get_default().get_collection('default_pbehavior')) self.currently_active_pb = set()
def provide_default_basics(cls): """ Provide logger, config, storages... ! Do not use in tests ! :rtype: Union[logging.Logger, canopsis.common.collection.MongoCollection] """ logger = Logger.get('ticketapi', cls.LOG_PATH) store = MongoStore.get_default() collection = store.get_collection(name=cls.ACTION_COLLECTION) mongo_collection = MongoCollection(collection) return (logger, mongo_collection)
def provide_default_basics(cls): """ Provide the default configuration and logger objects for MetaAlarmRuleManager. Do not use those defaults for tests. :return: config, logger, storage :rtype: Union[dict, logging.Logger, canopsis.storage.core.Storage] """ logger = Logger.get('metaalarmrule', cls.LOG_PATH) mongo = MongoStore.get_default() collection = mongo.get_collection(cls.MA_RULE_COLLECTION) mongo_collection = MongoCollection(collection) return logger, mongo_collection
def __init__(self, config, logger, pb_storage): """ :param dict config: configuration :param pb_storage: PBehavior Storage object """ super(PBehaviorManager, self).__init__() kwargs = {"logger": logger} self.context = singleton_per_scope(ContextGraph, kwargs=kwargs) self.logger = logger self.pb_storage = pb_storage self.config = config self.config_data = self.config.get(self.PBH_CAT, {}) self.default_tz = self.config_data.get("default_timezone", "Europe/Paris") # this line allow us to raise an exception pytz.UnknownTimeZoneError, # if the timezone defined in the pbehabior configuration file is wrong pytz.timezone(self.default_tz) self.pb_store = MongoCollection(MongoStore.get_default().get_collection('default_pbehavior')) self.currently_active_pb = set()
def _connect(self, *args, **kwargs): result = None from canopsis.confng import Configuration, Ini mongo_cfg = Configuration.load(MongoStore.CONF_PATH, Ini)[MongoStore.CONF_CAT] self._user = mongo_cfg['user'] self._pwd = mongo_cfg['pwd'] self._host = mongo_cfg['host'] self._db = mongo_cfg['db'] self._port = int(mongo_cfg['port']) self._replicaset = mongo_cfg.get('replicaset') self._read_preference = getattr( ReadPreference, mongo_cfg.get('read_preference', 'SECONDARY_PREFERRED'), ReadPreference.SECONDARY_PREFERRED) result = MongoStore.get_default() if True: self._database = result.client if result.authenticated: self.logger.debug( 'Already connected and authenticated on {}:{} /rs:{}'. format(self._host, self._port, self._replicaset)) else: try: result.authenticate() self.logger.info("Connected on {}:{} /rs:{}".format( self._host, self._port, self._replicaset)) except PyMongoError as ex: self.logger.error( 'Impossible to authenticate {} on {}:{} /rs:{}'.format( self._user, self._host, self._port, self._replicaset)) self.disconnect() result = None self._conn = result return result
def connect(self): if self.connected: return True try: self.conn = MongoStore.get_default() except PyMongoError as exc: self.logger.error('Old storage connection failure: {}'.format(exc)) return False self.db = self.conn.get_database(self.mongo_db) try: self.gridfs_namespace = CONFIG.get("master", "gridfs_namespace") except Exception: pass self.connected = True self.logger.debug("Connected %s" % id(self))
def get_version(): store = MongoStore.get_default() version_collection = \ store.get_collection(name=CanopsisVersionManager.COLLECTION) document = CanopsisVersionManager(version_collection). \ find_canopsis_document() if document is not None: return { CanopsisVersionManager.EDITION_FIELD: document.get(CanopsisVersionManager.EDITION_FIELD, ""), CanopsisVersionManager.STACK_FIELD: document.get(CanopsisVersionManager.STACK_FIELD, ""), CanopsisVersionManager.VERSION_FIELD: document.get( CanopsisVersionManager.VERSION_FIELD, "") } return { CanopsisVersionManager.EDITION_FIELD: "", CanopsisVersionManager.STACK_FIELD: "", CanopsisVersionManager.VERSION_FIELD: "" }
def connect(self): if self.connected: return True try: self.conn = MongoStore.get_default() except PyMongoError as exc: self.logger.error('Old storage connection failure: {}'.format(exc)) return False self.db = self.conn.client try: self.gridfs_namespace = CONFIG.get("master", "gridfs_namespace") except Exception: pass self.connected = True self.logger.debug("Connected %s" % id(self))
def __init__(self, config, logger, pb_storage): """ :param dict config: configuration :param pb_storage: PBehavior Storage object """ super(PBehaviorManager, self).__init__() kwargs = {"logger": logger} self.context = singleton_per_scope(ContextGraph, kwargs=kwargs) self.logger = logger self.pb_storage = pb_storage self.config = config self.config_data = self.config.get(self.PBH_CAT, {}) self.default_tz = self.config_data.get("default_timezone", "Europe/Paris") # this line allow us to raise an exception pytz.UnknownTimeZoneError, # if the timezone defined in the pbehabior configuration file is wrong pytz.timezone(self.default_tz) self.pb_store = MongoCollection( MongoStore.get_default().get_collection('default_pbehavior')) self.currently_active_pb = set()
def setUp(self): mongo = MongoStore.get_default() collection = mongo.get_collection("test_dynamic_infos") self.dynamic_coll = MongoCollection(collection) logger = Logger.get('test_dynamic_infos', None, output_cls=OutputNull) self.dynamic_infos = DynamicInfosManager( logger=logger, mongo_collection=self.dynamic_coll) self.dynamic_coll.drop() self.dynamic_infos_doc = { "_id": "rule2", "name": "Test", "author": "billy", "creation_date": 1576260000, "last_modified_date": 1576260000, "description": "Freedom !", "infos": [{ "name": "info", "value": "value" }, { "name": "info2", "value": "value2" }], "entity_patterns": [{ "_id": "cpu/billys-laptop" }], "alarm_patterns": [{ "v": { "state": { "val": 3 } } }] }
def update_canopsis_properties(): try: doc = request.json except ValueError: return gen_json_error( {'description': 'invalid JSON'}, HTTP_ERROR ) store = MongoStore.get_default() version_collection = store.get_collection( name=CanopsisVersionManager.COLLECTION) try: ok = check_values( ws, doc.get("edition"), doc.get("stack")) if ok: success = CanopsisVersionManager(version_collection). \ put_canopsis_document( doc.get("edition"), doc.get("stack"), None) if not success: return gen_json_error({'description': 'failed to update edition/stack'}, HTTP_ERROR) return gen_json({}) else: err = 'Invalid value(s).' ws.logger.error(err) return gen_json_error( {'description': err}, HTTP_ERROR ) except CollectionError as ce: ws.logger.error('Update edition/stack error: {}'.format(ce)) return gen_json_error( {'description': 'Error while updating edition/stack values'}, HTTP_ERROR )
def __init__(self, logger): self.logger = logger self.db_store = MongoStore.get_default() self.cache_store = RedisStore.get_default() self.amqp_url, self.amqp_exchange = AmqpConnection.parse_conf() self.ts_client = InfluxDBClient.from_configuration(self.logger) parser = Configuration.load(CONF_PATH, Ini) section = parser.get(ConfName.SECT_HC) self.check_amqp_limit_size = int( section.get(ConfName.CHECK_AMQP_LIMIT_SIZE, "")) self.check_amqp_queues = section.get(ConfName.CHECK_AMQP_QUEUES, "").split(",") self.check_collections = section.get(ConfName.CHECK_COLLECTIONS, "").split(",") self.check_engines_list = section.get(ConfName.CHECK_ENGINES, "").split(",") self.check_ts_db = section.get(ConfName.CHECK_TS_DB, "") self.check_webserver = section.get(ConfName.CHECK_WEBSERVER, "") self.systemctl_engine_prefix = section.get( ConfName.SYSTEMCTL_ENGINE_PREFIX, "")
def _create_mongo_conn(): store = MongoStore.get_default() return MongoCollection(store.get_collection(collection))
def __init__(self, logger): self.logger = logger self.view_collection = MongoCollection( MongoStore.get_default().get_collection(VIEWS_COLLECTION)) self.group_collection = MongoCollection( MongoStore.get_default().get_collection(GROUPS_COLLECTION))
def setUp(self): self.logger = logging.getLogger('alerts') self.alerts_storage = Middleware.get_middleware_by_uri( 'storage-periodical-testalarm://' ) self.config_storage = Middleware.get_middleware_by_uri( 'storage-default-testconfig://' ) self.config_storage.put_element( element={ '_id': 'test_config', 'crecord_type': 'statusmanagement', 'bagot_time': 3600, 'bagot_freq': 10, 'stealthy_time': 300, 'restore_event': True, 'auto_snooze': False, 'snooze_default_time': 300, }, _id='test_config' ) self.filter_storage = Middleware.get_middleware_by_uri( 'storage-default-testalarmfilter://' ) self.context_graph_storage = Middleware.get_middleware_by_uri( 'storage-default-testentities://' ) self.cg_manager = ContextGraph(self.logger) self.cg_manager.ent_storage = self.context_graph_storage self.watcher_manager = Watcher() conf = Configuration.load(Alerts.CONF_PATH, Ini) filter_ = {'crecord_type': 'statusmanagement'} self.config_data = EtherealData( collection=MongoCollection(self.config_storage._backend), filter_=filter_) self.event_publisher = Mock(spec=StatEventPublisher) mongo = MongoStore.get_default() collection = mongo.get_collection("default_testpbehavior") pb_collection = MongoCollection(collection) logger = Logger.get('test_pb', None, output_cls=OutputNull) config = Configuration.load(PBehaviorManager.CONF_PATH, Ini) self.pbm = PBehaviorManager(config=config, logger=logger, pb_collection=pb_collection) self.manager = Alerts(config=conf, logger=self.logger, alerts_storage=self.alerts_storage, config_data=self.config_data, filter_storage=self.filter_storage, context_graph=self.cg_manager, watcher=self.watcher_manager, event_publisher=self.event_publisher, pbehavior=self.pbm)
def _connect(self, *args, **kwargs): result = MongoStore.get_default() self._database = result.client self._conn = result return result
from canopsis.alarms.adapters import AlarmAdapter from canopsis.alarms.services import AlarmService from canopsis.alerts.manager import Alerts from canopsis.alerts.reader import AlertsReader from canopsis.task.core import register_task from canopsis.watcher.manager import Watcher from canopsis.common.mongo_store import MongoStore # import this module so tasks are registered in canopsis.tasks.core import canopsis.alerts.tasks as __alerts_tasks work_alerts_manager = Alerts(*Alerts.provide_default_basics()) beat_alerts_manager = Alerts(*Alerts.provide_default_basics()) alertsreader_manager = AlertsReader(*AlertsReader.provide_default_basics()) mongo_store = MongoStore.get_default() @register_task def event_processing(engine, event, alertsmgr=None, **kwargs): """ AMQP Event processing. """ if alertsmgr is None: alertsmgr = work_alerts_manager encoded_event = {} for key, value in event.items(): try: key = key.encode('utf-8') except UnicodeError:
def fill(self, init=None, yes=False, reinit_auth=False): self.__put_canopsis_version_document() tools = [] for module in self.modules: try: migrationcls = lookup(module) except ImportError as err: self.logger.error( 'Impossible to load module "{0}": {1}'.format( module, err ) ) continue migrationtool = migrationcls() migrationtool.logger.addHandler(self.loghandler) tools.append(migrationtool) coll = None if init is None: store = MongoStore.get_default() store.authenticate() coll = MongoCollection(store.get_collection(self.FLAG_COLLECTION)) data = coll.find_one({"_id": self.FLAG_COLLECTION}) if data is None: print("Database not intialized. Initializing...") init = True else: print("Database already intialized. Updating...") init = False if init is None and reinit_auth is False: data = { "_id": "initialized", "at": str(time.strftime("%a, %d %b %Y %H:%M:%S +0000")) } print("The canopsis initialization flag did not exist in the " "database. So canopsinit will (re?)initialized the " "database. Meaning, it may delete some important data " "from canopsis database. If you still want to initialize " "the database, call the same command with the " "`--authorize-reinit` flag. Or if you do not want to " "initialize the database, add the document `{0}` in the {1} " "collections.".format(data, self.FLAG_COLLECTION)) exit(1) for tool in tools: if init: tool.init(yes=yes) else: tool.update(yes=yes) if init is True: coll.insert({"_id": self.FLAG_COLLECTION, "at": str(time.strftime( "%a, %d %b %Y %H:%M:%S +0000"))})
def __init__(self, logger): self.logger = logger self.rule_collection = MongoCollection( MongoStore.get_default().get_collection(RULE_COLLECTION))
from canopsis.alarms.adapters import AlarmAdapter from canopsis.alarms.services import AlarmService from canopsis.alerts.manager import Alerts from canopsis.alerts.reader import AlertsReader from canopsis.task.core import register_task from canopsis.watcher.manager import Watcher from canopsis.common.mongo_store import MongoStore # import this module so tasks are registered in canopsis.tasks.core import canopsis.alerts.tasks as __alerts_tasks work_alerts_manager = Alerts(*Alerts.provide_default_basics()) beat_alerts_manager = Alerts(*Alerts.provide_default_basics()) alertsreader_manager = AlertsReader(*AlertsReader.provide_default_basics()) mongo_store = MongoStore.get_default() @register_task def event_processing(engine, event, alertsmgr=None, **kwargs): """ AMQP Event processing. """ if alertsmgr is None: alertsmgr = work_alerts_manager encoded_event = {} for key, value in event.items(): try: key = key.encode('utf-8')
from canopsis.common.converters import mongo_filter, id_filter from canopsis.common.utils import get_rrule_freq from canopsis.pbehavior.manager import PBehaviorManager from canopsis.stat.manager import StatManager from canopsis.webcore.utils import gen_json, gen_json_error, HTTP_NOT_FOUND from pymongo.errors import PyMongoError alarm_manager = Alerts(*Alerts.provide_default_basics()) alarmreader_manager = AlertsReader(*AlertsReader.provide_default_basics()) context_manager = alarm_manager.context_manager pbehavior_manager = PBehaviorManager( *PBehaviorManager.provide_default_basics()) WATCHER_COLLECTION = "default_entities" mongo = MongoStore.get_default() collection = mongo.get_collection(WATCHER_COLLECTION) mongo_collection = MongoCollection(collection) DEFAULT_LIMIT = 120 DEFAULT_START = '0' DEFAULT_SORT = False DEFAULT_PB_TYPES = [] DEFAULT_DIRECTION = "ASC" class TileColor(FastEnum): PAUSE = "pause" OK = "ok" MINOR = "minor"