Ejemplo n.º 1
0
    def __init__(self, options={}):
        super(BasicAlarmLinkBuilder, self).__init__(options=options)
        self.logger = Logger.get('context-graph', LOG_PATH)

        conf_store = Configuration.load(MongoStore.CONF_PATH, Ini)
        mongo = MongoStore(config=conf_store)
        self.alerts_collection = mongo.get_collection(name=ALERTS_COLLECTION)
Ejemplo n.º 2
0
    def __init__(self, options={}):
        super(BasicAlarmLinkBuilder, self).__init__(options=options)
        self.logger = Logger.get('context-graph', LOG_PATH)

        conf_store = Configuration.load(MongoStore.CONF_PATH, Ini)
        mongo = MongoStore(config=conf_store)
        self.alerts_collection = mongo.get_collection(name=ALERTS_COLLECTION)
Ejemplo n.º 3
0
    def setUp(self):
        self.logger = logging.getLogger()
        self.logger.setLevel(logging.DEBUG)

        self.at_storage = Middleware.get_middleware_by_uri(
            'storage-default-testassociativetable://'
        )
        self.at_manager = AssociativeTableManager(
            collection=self.at_storage._backend,
            logger=self.logger
        )

        self.config = self.at_manager.create('test_hlm')
        self.config.set('basic_link_builder', {})
        self.at_manager.save(self.config)

        self.htl_manager = HypertextLinkManager(config=self.config.get_all(),
                                                logger=self.logger)

        self.entity = {
            '_id': 'april/oneil',
            'type': 'resource',
            'name': 'ntm',
            'depends': [],
            'impact': [],
            'measurements': {},
            'infos': {
                'location': 'technodrome'
            }
        }

        self.alarm = {
            '_id': 'krang',
            'd': 'april/oneil',
            't': 0,
            'v': {
                'connector': 'Engine',
                'connector_name': 'JENKINS',
                'component': 'oneil',
                'resource': 'april',
                'state': {
                    'a': 'Splinter',
                    '_t': 'stateinc',
                    'm': 'Possède la pensée juste, alors seulement tu pourras recevoir les dons de la force, du savoir et de la paix.',
                    't': 1,
                    'val': 0
                },
                'output': "Quatre tortues d'enfer, dans la ville",
                'display_name': 'TN-TN-TN',
            }
        }

        conf_store = Configuration.load(MongoStore.CONF_PATH, Ini)
        mongo = MongoStore(config=conf_store)
        self.alerts_collection = mongo.get_collection(name=ALERTS_COLLECTION)

        self.alerts_collection.insert(self.alarm)
Ejemplo n.º 4
0
    def setUp(self):
        self.logger = logging.getLogger()
        self.logger.setLevel(logging.DEBUG)

        self.at_storage = Middleware.get_middleware_by_uri(
            'storage-default-testassociativetable://')
        self.at_manager = AssociativeTableManager(
            collection=self.at_storage._backend, logger=self.logger)

        self.config = self.at_manager.create('test_hlm')
        self.config.set('basic_link_builder', {})
        self.at_manager.save(self.config)

        self.htl_manager = HypertextLinkManager(config=self.config.get_all(),
                                                logger=self.logger)

        self.entity = {
            '_id': 'april/oneil',
            'type': 'resource',
            'name': 'ntm',
            'depends': [],
            'impact': [],
            'measurements': {},
            'infos': {
                'location': 'technodrome'
            }
        }

        self.alarm = {
            '_id': 'krang',
            'd': 'april/oneil',
            't': 0,
            'v': {
                'connector': 'Engine',
                'connector_name': 'JENKINS',
                'component': 'oneil',
                'resource': 'april',
                'state': {
                    'a': 'Splinter',
                    '_t': 'stateinc',
                    'm':
                    'Possède la pensée juste, alors seulement tu pourras recevoir les dons de la force, du savoir et de la paix.',
                    't': 1,
                    'val': 0
                },
                'output': "Quatre tortues d'enfer, dans la ville",
                'display_name': 'TN-TN-TN',
            }
        }

        conf_store = Configuration.load(MongoStore.CONF_PATH, Ini)
        mongo = MongoStore(config=conf_store)
        self.alerts_collection = mongo.get_collection(name=ALERTS_COLLECTION)

        self.alerts_collection.insert(self.alarm)
Ejemplo n.º 5
0
    def provide_default_basics(cls):
        """
            provide default basics
        """
        conf_store = Configuration.load(MongoStore.CONF_PATH, Ini)

        mongo = MongoStore(config=conf_store)
        lock_collection = mongo.get_collection(name=cls.LOCK_COLLECTION)

        logger = Logger.get('lock', cls.LOG_PATH)

        return (logger, lock_collection)
Ejemplo n.º 6
0
    def provide_default_basics(cls):
        """
            provide default basics
        """
        conf_store = Configuration.load(MongoStore.CONF_PATH, Ini)

        mongo = MongoStore(config=conf_store)
        lock_collection = mongo.get_collection(name=cls.LOCK_COLLECTION)

        logger = Logger.get('lock', cls.LOG_PATH)

        return (logger, lock_collection)
Ejemplo n.º 7
0
    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'
                }
            }
        }
Ejemplo n.º 8
0
    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)
Ejemplo n.º 9
0
    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"))
            })
Ejemplo n.º 10
0
    def _run_command(self, command, table=None, *args, **kwargs):
        """Run a specific command on a given backend.

        :param str command: command to run.
        :param str table: table to use. self table if None.
        """

        result = None

        try:
            if table is None:
                table = self.get_table()
            # get pymongo raw collection
            backend = self._get_backend(backend=table).collection
            backend_command = getattr(backend, command)
            result = MongoStore.hr(backend_command, *args, **kwargs)

        except NetworkTimeout:
            self.logger.warning(
                'Try to run command {0}({1}) on {2} attempts left'
                .format(command, kwargs, backend))

        except (OperationFailure, CollectionError) as of:
            self.logger.error(u'{0} during running command {1}({2}) of in {3}'
                .format(of, command, kwargs, backend))

        return result
Ejemplo n.º 11
0
    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": ""
            }
        }
Ejemplo n.º 12
0
    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])
Ejemplo n.º 13
0
    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)
Ejemplo n.º 14
0
    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": ""
        }
Ejemplo n.º 15
0
    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()
Ejemplo n.º 16
0
    def setUp(self):
        config = configparser.RawConfigParser()
        config.read(os.path.join(root_path, DEFAULT_CONF_FILE))
        self.conf = {
            MongoStore.CONF_CAT: {
                "host": config["DATABASE"]["host"],
                "port": config["DATABASE"]["port"],
                'db': config["DATABASE"]["db"],
                'user': config["DATABASE"]["user"],
                'pwd': config["DATABASE"]["pwd"]
            }
        }
        self.client = MongoStore(config=self.conf)
        self.collection = self.client.get_collection('any_collection')

        self.ed = EtherealData(collection=self.collection,
                               filter_={},
                               timeout=2)
Ejemplo n.º 17
0
    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)), )
Ejemplo n.º 18
0
    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)), )
Ejemplo n.º 19
0
    def drop(self, table=None):

        if table is None:
            collections = MongoStore.hr(self._database.collection_names,
                                        include_system_collections=False)
            for collection_name in collections:
                self.drop(table=collection_name)

        else:
            self._get_backend(backend=table).remove()
Ejemplo n.º 20
0
    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()
Ejemplo n.º 21
0
 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)
Ejemplo n.º 22
0
    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)
Ejemplo n.º 23
0
    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)
Ejemplo n.º 24
0
 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)
Ejemplo n.º 25
0
    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)
Ejemplo n.º 26
0
    def drop(self, table=None):

        if table is None:
            collections = MongoStore.hr(
                self._database.collection_names,
                include_system_collections=False
            )
            for collection_name in collections:
                self.drop(table=collection_name)

        else:
            self._get_backend(backend=table).remove()
Ejemplo n.º 27
0
    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()

        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)
Ejemplo n.º 28
0
    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))
Ejemplo n.º 29
0
    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)
Ejemplo n.º 30
0
    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)
Ejemplo n.º 31
0
    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)
Ejemplo n.º 32
0
    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
Ejemplo n.º 33
0
    def size(self, table=None, criteria=None, *args, **kwargs):

        result = 0

        _backend = self._get_backend(backend=table)

        try:
            result = MongoStore.hr(self._database.command, "collstats", _backend)['size']

        except Exception as ex:
            self.logger.warning(
                "Impossible to read Collection Size: {0}".format(ex))
            result = None

        return result
Ejemplo n.º 34
0
    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()
Ejemplo n.º 35
0
    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)
Ejemplo n.º 36
0
    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
Ejemplo n.º 37
0
    def setUpClass(cls):
        cls.db_name = 'canopsis'
        cls.collection_name = 'test_mongostorage'

        config = configparser.RawConfigParser()
        config.read(os.path.join(root_path, DEFAULT_CONF_FILE))
        cls.conf = {
            MongoStore.CONF_CAT: {
                "host": config["DATABASE"]["host"],
                "port": config["DATABASE"]["port"],
                'db': config["DATABASE"]["db"],
                'user': config["DATABASE"]["user"],
                'pwd': config["DATABASE"]["pwd"]
            }
        }

        cls.ms = MongoStore(config=cls.conf)
        cls.collection = cls.ms.get_collection(cls.collection_name)
Ejemplo n.º 38
0
    def setUp(self):
        config = configparser.RawConfigParser()
        config.read(os.path.join(root_path, DEFAULT_CONF_FILE))
        self.conf = {
            MongoStore.CONF_CAT: {
                "host": config["DATABASE"]["host"],
                "port": config["DATABASE"]["port"],
                'db': config["DATABASE"]["db"],
                'user': config["DATABASE"]["user"],
                'pwd': config["DATABASE"]["pwd"]
            }
        }
        self.client = MongoStore(config=self.conf)
        self.collection = self.client.get_collection('any_collection')

        self.ed = EtherealData(collection=self.collection,
                               filter_={},
                               timeout=2)
Ejemplo n.º 39
0
 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()
Ejemplo n.º 40
0
    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
Ejemplo n.º 41
0
    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))
Ejemplo n.º 42
0
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: ""
    }
Ejemplo n.º 43
0
    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))
Ejemplo n.º 44
0
 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()
Ejemplo n.º 45
0
    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
                    }
                }
            }]
        }
Ejemplo n.º 46
0
class TestEtherealData(unittest.TestCase):

    def setUp(self):
        config = configparser.RawConfigParser()
        config.read(os.path.join(root_path, DEFAULT_CONF_FILE))
        self.conf = {
            MongoStore.CONF_CAT: {
                "host": config["DATABASE"]["host"],
                "port": config["DATABASE"]["port"],
                'db': config["DATABASE"]["db"],
                'user': config["DATABASE"]["user"],
                'pwd': config["DATABASE"]["pwd"]
            }
        }
        self.client = MongoStore(config=self.conf)
        self.collection = self.client.get_collection('any_collection')

        self.ed = EtherealData(collection=self.collection,
                               filter_={},
                               timeout=2)

    def tearDown(self):
        """Teardown"""
        self.collection.drop()

    def test_get_set(self):
        self.assertIsNone(self.ed.get('mario'))
        self.ed.set('mario', 'bros')
        self.assertEqual(self.ed.get('mario'), 'bros')

    def test_cache(self):
        self.ed.set('sonic', 'hedgehog')
        self.assertEqual(self.ed.get('sonic'), 'hedgehog')

        self.collection.update({}, {'sonic': 'tails'})
        self.assertEqual(self.ed.get('sonic'), 'hedgehog')

        sleep(3)
        self.assertEqual(self.ed.get('sonic'), 'tails')
Ejemplo n.º 47
0
 def __init__(self, logger):
     self.logger = logger
     self.rule_collection = MongoCollection(
         MongoStore.get_default().get_collection(RULE_COLLECTION))
Ejemplo n.º 48
0
 def wrapped(*args, **kwargs):
     """
     Wrapper implementation
     """
     return MongoStore.hr(func, *args, **kwargs)
Ejemplo n.º 49
0
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:
Ejemplo n.º 50
0
 def _connect(self, *args, **kwargs):
     result = MongoStore.get_default()
     self._database = result.client
     self._conn = result
     return result
Ejemplo n.º 51
0
 def __getattr__(self, name):
     return MongoStore.hr(self.__pymongo_getattr__, name)
Ejemplo n.º 52
0
    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"))})