def __init__(self, logger, config, storage, pbehavior_manager): """ :param logger: a logger object :param config: a confng instance :param storage: a storage instance :param pbehavior_manager: a pbehavior manager instance """ self.logger = logger self.config = config self.alarm_storage = storage self.alarm_collection = MongoCollection(self.alarm_storage._backend) self.pbehavior_manager = pbehavior_manager self.pbh_filter = None category = self.config.get(self.CATEGORY, {}) self.expiration = int(category.get('expiration', DEFAULT_EXPIRATION)) self.opened_truncate = cfg_to_bool(category.get('opened_truncate', DEFAULT_OPENED_TRUNC)) self.opened_limit = int(category.get('opened_limit', DEFAULT_OPENED_LIMIT)) self.resolved_truncate = cfg_to_bool(category.get('resolved_truncate', DEFAULT_RESOLVED_TRUNC)) self.resolved_limit = int(category.get('resolved_limit', DEFAULT_RESOLVED_LIMIT)) self.count_cache = {} self.grammar = join(root_path, self.GRAMMAR_FILE) self.has_active_pbh = None
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 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 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, collection): """ :param `~.logger.Logger` logger: object. :param `~.common.collection.MongoCollection` collection: object. """ self.__logger = logger self.__collection = MongoCollection(collection)
def setUp(self): self.storage = Middleware.get_middleware_by_uri( 'mongodb-default-testsession://') self.collection = MongoCollection(self.storage._backend) self.manager = Session(collection=self.collection) self.user = '******'
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 setUp(self): output = StringIO() self.logger = Logger.get('test', output, OutputStream) self.storage = Middleware.get_middleware_by_uri( 'storage-default-testmongocollection://') self.collection = MongoCollection(collection=self.storage._backend, logger=self.logger) self.id_ = 'testid'
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 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 get_backend(self, namespace=None): self.check_connected() if not namespace: namespace = self.namespace try: backend = self.backend[namespace] self.logger.debug("Use %s collection" % namespace) return backend except Exception: self.backend[namespace] = MongoCollection(self.conn.get_collection(namespace)) self.logger.debug("Connected to %s collection." % namespace) return self.backend[namespace]
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 _get_backend(self, backend=None, *args, **kwargs): """ Get a reference to a specific backend where name is input backend. If input backend is None, self.backend is used. :param backend: backend name. If None, self.backend is used. :type backend: basestring :returns: backend reference. :raises: NotImplementedError .. seealso: DataBase.set_backend(self, backend) """ if backend is None: raise Exception('none backend') return MongoCollection(self._conn.get_collection(backend))
def setUp(self): self.storage = Middleware.get_middleware_by_uri( 'mongodb-default-testsession://') self.collection = MongoCollection(self.storage._backend) self.manager = Session(collection=self.collection) self.user = '******' self.id_beaker_session = 'cm9vdF8xNTc2MDY1MzY2' self.path = [ "view/da7ac9b9-db1c-4435-a1f2-edb4d6be4db8", "view-tab_edd5855b-54f1-4c51-9550-d88c2da60768" ] self.path_bis = [ "view/da7ac9b9-db1c-4435-a1f2-edb4d6be4db8", "view-tab_edd5855b-54f1-4c51-azerty" ]
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) 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)
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 provide_default_basics(cls): """ Provide logger, config, storages... ! Do not use in tests ! :rtype: Union[canopsis.confng.simpleconf.Configuration logging.Logger, canopsis.storage.core.Storage, canopsis.common.ethereal_data.EtherealData, canopsis.storage.core.Storage, canopsis.context_graph.manager.ContextGraph, canopsis.watcher.manager.Watcher] """ config = Configuration.load(Alerts.CONF_PATH, Ini) conf_store = Configuration.load(MongoStore.CONF_PATH, Ini) mongo = MongoStore(config=conf_store) config_collection = MongoCollection( mongo.get_collection(name=cls.CONFIG_COLLECTION)) filter_ = {'crecord_type': 'statusmanagement'} config_data = EtherealData(collection=config_collection, filter_=filter_) logger = Logger.get('alerts', cls.LOG_PATH) alerts_storage = Middleware.get_middleware_by_uri( cls.ALERTS_STORAGE_URI ) filter_storage = Middleware.get_middleware_by_uri( cls.FILTER_STORAGE_URI ) context_manager = ContextGraph(logger) watcher_manager = Watcher() pbehavior_manager = PBehaviorManager(*PBehaviorManager.provide_default_basics()) amqp_pub = AmqpPublisher(get_default_amqp_conn(), logger) event_publisher = StatEventPublisher(logger, amqp_pub) return (config, logger, alerts_storage, config_data, filter_storage, context_manager, watcher_manager, event_publisher, pbehavior_manager)
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 check_db(self): """ Check if database service is available. :rtype: ServiceState """ existing_cols = self.db_store.client.collection_names() for collection_name in self.CHECK_COLLECTIONS: # Existence test if collection_name not in existing_cols: msg = 'Missing collection {}'.format(collection_name) return ServiceState(message=msg) # Read test collection = self.db_store.get_collection(name=collection_name) mongo_collection = MongoCollection(collection) try: mongo_collection.find({}, limit=1) except Exception as exc: return ServiceState(message='Find error: {}'.format(exc)) return ServiceState()
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 __init__(self, collection): """ :param collection: `pymongo.collection.Collection` object. """ self.__collection = MongoCollection(collection)
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" MAJOR = "major" CRITICAL = "critical"
def __init__(self, logger): self.logger = logger self.rule_collection = MongoCollection( MongoStore.get_default().get_collection(RULE_COLLECTION))