def __init__(self, actions_path=None, users_path=None, roles_path=None, *args, **kwargs): super(RightsModule, self).__init__(*args, **kwargs) self.logger = Logger.get('migrationtool', MigrationModule.LOG_PATH) self.config = Configuration.load(RightsModule.CONF_PATH, Ini) conf = self.config.get(self.CATEGORY, {}) self.manager = Rights() if actions_path is not None: actions_path = actions_path else: actions_path = conf.get('actions_path', DEFAULT_ACTIONS_PATH) self.actions_path = os.path.expanduser(actions_path) if users_path is not None: users_path = users_path else: users_path = conf.get('users_path', DEFAULT_USERS_PATH) self.users_path = os.path.expanduser(users_path) if roles_path is not None: roles_path = roles_path else: roles_path = conf.get('roles_path', DEFAULT_ROLES_PATH) self.roles_path = os.path.expanduser(roles_path)
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 __init__(self, config=None, logger=None): if logger is None: self.logger = Logger.get(self.LOG_NAME, self.LOG_PATH, output_cls=OutputFile) else: self.logger = logger if config is None: self.config = Configuration.load(CONF_PATH, Ini) else: self.config = config self.obj_storage = Middleware.get_middleware_by_uri( 'storage-default://', table='schemas') section = self.config.get(ConfName.SECT_GCTX) self._event_types = section[ConfName.EVENT_TYPES] self._extra_fields = section[ConfName.EXTRA_FIELDS] section = self.config.get(ConfName.SECT_FILTER) self._schema_id = section[ConfName.SCHEMA_ID] self.reload_schema()
def get_middleware_by_uri(uri, table=None): """ table overrides data_scope """ protocol, data_type, data_scope = parse_scheme(uri) storage = None if protocol == 'mongodb' or protocol == 'storage': if data_type == 'periodical': from canopsis.mongo.periodical import MongoPeriodicalStorage as msc else: from canopsis.mongo.core import MongoStorage as msc storage = msc() else: raise Exception('Unknown storage: {}'.format(protocol)) storage.protocol = protocol storage.data_type = data_type if table is None else None storage.data_scope = data_scope if table is None else None storage.table = table storage.logger = Logger.get( 'storage-{}'.format(protocol), 'var/log/storage-{}.log'.format(protocol) ) storage._connect() storage._backend = storage._get_backend(backend=storage.get_table()) return storage
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)
def __init__(self, storage=DEFAULT_STORAGE, *args, **kwargs): super(MongoOIDsModule, self).__init__(*args, **kwargs) self.logger = Logger.get('migrationmodule', MigrationModule.LOG_PATH) self.storage = Middleware.get_middleware_by_uri(storage) self.storage.connect()
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 test_logger_memory(self): output = StringIO() logger = Logger.get('memory', output, OutputStream, level=logging.INFO, memory=True, memory_capacity=2, memory_flushlevel=logging.CRITICAL) # under max capacity logger.info('1') output.seek(0) lines = len(output.readlines()) self.assertEqual(lines, 0) # max capacity logger.info('2') output.seek(0) lines = len(output.readlines()) self.assertEqual(lines, 2) # under max capacity but flush level logger.critical('3') output.seek(0) lines = len(output.readlines()) self.assertEqual(lines, 3)
def test_logger_dedup(self): output = StringIO() log1 = Logger.get('dedup', output, OutputStream) log2 = Logger.get('dedup', output, OutputStream) log1.info('first_line') log2.info('second_line') log1.info('third_line') log2.info('fourth_line') output.seek(0) log_lines = output.readlines() self.assertEqual(len(log_lines), 4)
def __init__(self, *args, **kwargs): super(ViewsModule, self).__init__(*args, **kwargs) self.logger = Logger.get('migrationmodule', MigrationModule.LOG_PATH) self.storage = Middleware.get_middleware_by_uri('storage-default://', table='object') self.rights = Rights()
def __init__( self, actions_path=None, users_path=None, roles_path=None, *args, **kwargs ): super(RightsModule, self).__init__(*args, **kwargs) self.logger = Logger.get('migrationtool', MigrationModule.LOG_PATH) self.config = Configuration.load(RightsModule.CONF_PATH, Ini) conf = self.config.get(self.CATEGORY, {}) self.manager = Rights() if actions_path is not None: actions_path = actions_path else: actions_path = conf.get('actions_path', DEFAULT_ACTIONS_PATH) self.actions_path = os.path.expanduser(actions_path) if users_path is not None: users_path = users_path else: users_path = conf.get('users_path', DEFAULT_USERS_PATH) self.users_path = os.path.expanduser(users_path) if roles_path is not None: roles_path = roles_path else: roles_path = conf.get('roles_path', DEFAULT_ROLES_PATH) self.roles_path = os.path.expanduser(roles_path)
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 __init__(self, *args, **kwargs): super(IndexesModule, self).__init__(*args, **kwargs) self.logger = Logger.get('migrationmodule', MigrationModule.LOG_PATH) self.storage = get_storage( account=Account(user='******', group='root'), namespace='object' )
def setUp(self): logger = Logger.get("", None, output_cls=OutputNull) setattr(process, 'LOGGER', logger) self.conf_file = "etc/context_graph/manager.conf" self.category = "CONTEXTGRAPH" self.extra_fields = "extra_fields" self.authorized_info_keys = "authorized_info_keys" self.gctx_man = ContextGraph(logger) setattr(process, 'context_graph_manager', self.gctx_man)
def provide_default_basics(cls): """ Provide logger, config, storages... ! Do not use in tests ! :rtype: (logging.Logger) """ logger = Logger.get('healthcheck', cls.LOG_PATH) return (logger, )
def test_logger_unicode_level(self): output = StringIO() logger = Logger.get('unicode', output, OutputStream, level='info') logger.info('fline') output.seek(0) log_lines = output.readlines() self.assertEqual(len(log_lines), 1)
def __init__(self, modules=None): self.logger = Logger.get('migrationtool', self.LOG_PATH) self.config = Configuration.load(MigrationTool.CONF_PATH, Json) conf = self.config.get(self.CATEGORY, {}) if modules is None: self.modules = conf.get('modules', DEFAULT_MODULES) self.loghandler = StreamHandler() self.logger.addHandler(self.loghandler)
def provide_default_basics(cls): """ Provide logger, config, storages... ! Do not use in tests ! :rtype: (logging.Logger) """ logger = Logger.get('healthcheck', cls.LOG_PATH) return (logger,)
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 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)
def __init__(self, json_path=None, *args, **kwargs): super(JSONLoaderModule, self).__init__(*args, **kwargs) self.logger = Logger.get('migrationmodule', MigrationModule.LOG_PATH) if json_path is not None: self.json_path = json_path else: self.json_path = os.path.expanduser(DEFAULT_JSON_PATH) self.storage = get_storage(account=Account(user='******', group='root'), namespace='object')
def test_logger_stream(self): output = StringIO() logger = Logger.get('stream', output, OutputStream) logger.info('first_line') logger.info('second_line') output.seek(0) log_lines = output.readlines() self.assertEqual(len(log_lines), 2)
def __init__(self, modules=None, canopsis_version=None): self.logger = Logger.get('migrationtool', self.LOG_PATH) self.config = Configuration.load(MigrationTool.CONF_PATH, Json) conf = self.config.get(self.CATEGORY, {}) if modules is None: self.modules = conf.get('modules', DEFAULT_MODULES) self.loghandler = StreamHandler() self.logger.addHandler(self.loghandler) self.__canopsis_version = canopsis_version
def __init__(self, collections=None, *args, **kwargs): super(PurgeModule, self).__init__(*args, **kwargs) self.logger = Logger.get('migrationmodule', MigrationModule.LOG_PATH) self.config = Configuration.load(PurgeModule.CONF_PATH, Json) conf = self.config.get(self.CATEGORY, {}) self.storage = Storage(account=Account(user='******', group='root')) if collections is not None: self.collections = collections else: self.collections = conf.get('collections', DEFAULT_COLLECTIONS)
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 __init__(self, ask_timeout=None, version_info=None): self.logger = Logger.get('migrationmodule', self.LOG_PATH) self.config = Configuration.load(MigrationModule.CONF_PATH, Json) conf = self.config.get(self.CATEGORY, {}) self.ask_timeout = int(conf.get('ask_timeout', DEFAULT_ASK_TIMEOUT)) if ask_timeout is not None: self.ask_timeout = ask_timeout self.version_info = os.path.expanduser( conf.get('version_info', DEFAULT_VERSION_INFO)) if version_info is not None: self.version_info = os.path.expanduser(version_info)
def __init__(self, collection, logger=None): """ :param pymongo.collection.Collection collection: mongo Collection obj :param logging.Logger logger: python logger instance. If None, a new logger is provided. """ self.collection = collection self._hr = MongoStore.hr if logger is not None: self.logger = logger else: self.logger = Logger.get(LOG_NAME, LOG_PATH)
def provide_default_basics(cls): """ Provide the default configuration and logger objects for PBehaviorManager. Do not use those defaults for tests. :return: config, logger, storage :rtype: Union[dict, logging.Logger, canopsis.storage.core.Storage] """ logger = Logger.get(cls.LOG_NAME, cls.LOG_PATH) pb_storage = Middleware.get_middleware_by_uri(cls.PB_STORAGE_URI) return logger, pb_storage
def __init__(self, json_path=None, *args, **kwargs): super(JSONLoaderModule, self).__init__(*args, **kwargs) self.logger = Logger.get('migrationmodule', MigrationModule.LOG_PATH) if json_path is not None: self.json_path = json_path else: self.json_path = os.path.expanduser(DEFAULT_JSON_PATH) self.storage = get_storage( account=Account(user='******', group='root'), namespace='object' )
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 provide_default_basics(cls): """ provide default basics """ config = Configuration.load( os.path.join(root_path, cls.CONF_PATH), Ini).get(cls.CONF_SECTION) redis_host = config.get('host', cls.DEFAULT_DB_HOST) redis_port = int(config.get('port', cls.DEFAULT_DB_PORT)) redis_db_num = int(config.get('dbnum', cls.DEFAULT_DB_NUM)) redlock = Redlock( [{'host': redis_host, 'port': redis_port, 'db': redis_db_num}]) logger = Logger.get('lock', cls.LOG_PATH) return (logger, redlock)
def provide_default_basics(cls): """ Provide the default configuration and logger objects for PBehaviorManager. Do not use those defaults for tests. :return: config, logger, storage :rtype: Union[dict, logging.Logger, canopsis.storage.core.Storage] """ logger = Logger.get(cls.LOG_NAME, cls.LOG_PATH) pb_storage = Middleware.get_middleware_by_uri(cls.PB_STORAGE_URI) config = Configuration.load(PBehaviorManager.CONF_PATH, Ini) return config, logger, pb_storage
def test_logger_file(self): tmpf = tempfile.NamedTemporaryFile(delete=False) tmpf.close() logger = Logger.get('file', tmpf.name, OutputFile) logger.info('first_line') with open(tmpf.name, 'r') as fh: content = fh.readlines() self.assertEqual(len(content), 1) self.assertTrue(content[0][:-1].endswith('first_line')) os.unlink(tmpf.name)
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=None, *args, **kwargs): super(engine, self).__init__(*args, **kwargs) if config is None: config = Configuration.load(self.CONF_PATH, Ini) section = config.get(self.TASK_CONF) self._thd_warn_s = section.get(self.THD_WARN_S) * 60 self._thd_crit_s = section.get(self.THD_CRIT_S) * 60 self.logger = Logger.get(self.LOG_NAME, self.LOG_PATH, output_cls=OutputFile) # self.importer = ContextGraphImport(logger=self.logger) self.report_manager = Manager()
def get_default_app(logger=None, webconf=None, amqp_conn=None, amqp_pub=None): if webconf is None: webconf = Configuration.load(WebServer.CONF_PATH, Ini) if logger is None: logger = Logger.get('webserver', WebServer.LOG_FILE) if amqp_conn is None: amqp_conn = get_default_amqp_connection() if amqp_pub is None: amqp_pub = AmqpPublisher(amqp_conn, logger) # Declare WSGI application ws = WebServer(config=webconf, logger=logger, amqp_pub=amqp_pub).init_app() app = ws.application return app
def provide_default_basics(cls): """ provide default basics """ config = Configuration.load(os.path.join(root_path, cls.CONF_PATH), Ini).get(cls.CONF_SECTION) redis_host = config.get('host', cls.DEFAULT_DB_HOST) redis_port = int(config.get('port', cls.DEFAULT_DB_PORT)) redis_db_num = int(config.get('dbnum', cls.DEFAULT_DB_NUM)) redlock = Redlock([{ 'host': redis_host, 'port': redis_port, 'db': redis_db_num }]) logger = Logger.get('lock', cls.LOG_PATH) return (logger, redlock)
def __init__(self, amqp_pub=None): """ :param amqp_pub canopsis.common.amqp.AmqpPublisher: """ self.logger = Logger.get('watcher', LOG_PATH) self.watcher_storage = Middleware.get_middleware_by_uri( 'mongodb-default-watcher://') self.alert_storage = Middleware.get_middleware_by_uri( 'mongodb-periodical-alarm://') self.sla_storage = Middleware.get_middleware_by_uri( 'storage-default-sla://') self.context_graph = ContextGraph(self.logger) self.pbehavior_manager = PBehaviorManager( *PBehaviorManager.provide_default_basics()) self.amqp_pub = amqp_pub if amqp_pub is None: self.amqp_pub = AmqpPublisher(get_default_amqp_conn(), self.logger)
def __init__(self, amqp_pub=None): """ :param amqp_pub canopsis.common.amqp.AmqpPublisher: """ self.logger = Logger.get('watcher', LOG_PATH) self.watcher_storage = Middleware.get_middleware_by_uri( 'mongodb-default-watcher://') self.alert_storage = Middleware.get_middleware_by_uri( 'mongodb-periodical-alarm://') self.sla_storage = Middleware.get_middleware_by_uri( 'storage-default-sla://') self.context_graph = ContextGraph(self.logger) self.pbehavior_manager = PBehaviorManager( *PBehaviorManager.provide_default_basics() ) self.amqp_pub = amqp_pub if amqp_pub is None: self.amqp_pub = AmqpPublisher(get_default_amqp_conn(), self.logger)
def provide_default_basics(cls): """ Provide logger, config, storages... ! Do not use in tests ! :rtype: Union[logging.Logger, canospis.confng.simpleconf.Configuration, canopsis.storage.core.Storage, canopsis.pbehavior.manager.PBehaviorManager] """ logger = Logger.get('alertsreader', cls.LOG_PATH) conf = Configuration.load(Alerts.CONF_PATH, Ini) alerts_storage = Middleware.get_middleware_by_uri( Alerts.ALERTS_STORAGE_URI ) pbm = PBehaviorManager(*PBehaviorManager.provide_default_basics()) return (logger, conf, alerts_storage, pbm)
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)
def setUp(self): super(TestReader, self).setUp() self.pb_storage = Middleware.get_middleware_by_uri( PBehaviorManager.PB_STORAGE_URI ) self.logger = Logger.get('alertsreader', '/tmp/null') conf = Configuration.load(PBehaviorManager.CONF_PATH, Ini) self.pbehavior_manager = PBehaviorManager(config=conf, logger=self.logger, pb_storage=self.pb_storage) 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'} } }
# -*- coding: utf-8 -*- """Module in chage of defining the graph context and updating it when it's needed.""" from __future__ import unicode_literals from canopsis.task.core import register_task from canopsis.context_graph.manager import ContextGraph from canopsis.logger import Logger LOGGER = Logger.get('context_graph', 'var/log/context_graph.log') context_graph_manager = ContextGraph(LOGGER) cache = set() def update_cache(): """Update the entity cache "cache" """ global cache cache = context_graph_manager.get_all_entities_id() def check_type(entities, expected): """Raise TypeError if the type of the entities entities does not match the expected type. :param entities: the entities to check. :type entities: a dict :param expected: the expected type. :type expected: a string :raises TypeError: if the entity does not match the expected one.
# GNU Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public License # along with Canopsis. If not, see <http://www.gnu.org/licenses/>. # --------------------------------- import validictory import traceback from canopsis.logger import Logger from canopsis.old.storage import get_storage from canopsis.old.account import Account schema_logger = Logger.get('schema', 'var/log/schema.log') class NoSchemaError(Exception): def __init__(self, schema_id, *args, **kwargs): super(NoSchemaError, self).__init__(*args, **kwargs) self.schema_id = schema_id def __str__(self): return 'Schema {0} not found in database'.format(self.schema_id) def __unicode__(self): return u'Schema {0} not found in database'.format(self.schema_id)