Пример #1
0
    def main(self):
        self.set_proctitle(self.name)

        # Daemon like init
        self.debug_output = []
        self.modules_dir = modulesctx.get_modulesdir()
        self.modules_manager = ModulesManager('webui', self.find_modules_path(), [])
        self.modules_manager.set_modules(self.modules)
        # We can now output some previously silenced debug output
        self.do_load_modules()
        for inst in self.modules_manager.instances:
            f = getattr(inst, 'load', None)
            if f and callable(f):
                f(self)
        for s in self.debug_output:
            print s
        del self.debug_output

        # Check if the Bottle view dir really exist
        if not os.path.exists(bottle.TEMPLATE_PATH[0]):
            logger.error("[WebUI] The view path do not exist at %s" % bottle.TEMPLATE_PATH)
            sys.exit(2)

        # Check directories
        # We check if the photo directory exists. If not, try to create it
        for d in [self.photo_dir, self.share_dir, self.config_dir]:
            logger.debug("[WebUI] Checking dir: %s", d)
            if not os.path.exists(d):
                try:
                    os.mkdir(d)
                    logger.info("[WebUI] Created dir: %s", d)
                except Exception, exp:
                    logger.error("[WebUI] Dir creation failed: %s", exp)
Пример #2
0
    def main(self):
        self.set_proctitle(self.name)

        # Daemon like init
        self.debug_output = []
        self.modules_dir = modulesctx.get_modulesdir()
        self.modules_manager = ModulesManager('webui',
                                              self.find_modules_path(), [])
        self.modules_manager.set_modules(self.modules)
        # We can now output some previously silenced debug output
        self.do_load_modules()
        for inst in self.modules_manager.instances:
            f = getattr(inst, 'load', None)
            if f and callable(f):
                f(self)
        for s in self.debug_output:
            print s
        del self.debug_output

        # Check if the Bottle view dir really exist
        if not os.path.exists(bottle.TEMPLATE_PATH[0]):
            logger.error("[WebUI] The view path do not exist at %s" %
                         bottle.TEMPLATE_PATH)
            sys.exit(2)

        # Check directories
        # We check if the photo directory exists. If not, try to create it
        for d in [self.photo_dir, self.share_dir, self.config_dir]:
            logger.debug("[WebUI] Checking dir: %s", d)
            if not os.path.exists(d):
                try:
                    os.mkdir(d)
                    logger.info("[WebUI] Created dir: %s", d)
                except Exception, exp:
                    logger.error("[WebUI] Dir creation failed: %s", exp)
    def init_livestatus(self):
        self.livelogs = 'tmp/livelogs.db' + self.testid
        modconf = Module({'module_name': 'LiveStatus',
            'module_type': 'livestatus',
            'port': str(50000 + os.getpid()),
            'pnp_path': 'tmp/pnp4nagios_test' + self.testid,
            'host': '127.0.0.1',
            'socket': 'live',
            'name': 'test', #?
            'database_file': self.livelogs,
        })

        dbmodconf = Module({'module_name': 'LogStore',
            'module_type': 'logstore_sqlite',
            'use_aggressive_sql': "0",
            'database_file': self.livelogs,
            'archive_path': os.path.join(os.path.dirname(self.livelogs), 'archives'),
        })
        ####################################
        # !NOT! modconf.modules = [dbmodconf]
        ####################################
        self.livestatus_broker = LiveStatus_broker(modconf)
        self.livestatus_broker.create_queues()

        self.livestatus_broker.init()

        #--- livestatus_broker.main
        self.livestatus_broker.log = logger
        # this seems to damage the logger so that the scheduler can't use it
        #self.livestatus_broker.log.load_obj(self.livestatus_broker)
        self.livestatus_broker.debug_output = []
        self.livestatus_broker.modules_manager = ModulesManager('livestatus', modulesctx.get_modulesdir(), [])
        self.livestatus_broker.modules_manager.set_modules(self.livestatus_broker.modules)
        # We can now output some previouly silented debug ouput
        self.livestatus_broker.do_load_modules()
        for inst in self.livestatus_broker.modules_manager.instances:
            if inst.properties["type"].startswith('logstore'):
                f = getattr(inst, 'load', None)
                if f and callable(f):
                    f(self.livestatus_broker)  # !!! NOT self here !!!!
                break
        for s in self.livestatus_broker.debug_output:
            print "errors during load", s
        del self.livestatus_broker.debug_output
        self.livestatus_broker.add_compatibility_sqlite_module()
        self.livestatus_broker.rg = LiveStatusRegenerator()
        self.livestatus_broker.datamgr = datamgr
        datamgr.load(self.livestatus_broker.rg)
        self.livestatus_broker.query_cache = LiveStatusQueryCache()
        self.livestatus_broker.query_cache.disable()
        self.livestatus_broker.rg.register_cache(self.livestatus_broker.query_cache)
        #--- livestatus_broker.main

        #--- livestatus_broker.do_main
        self.livestatus_broker.db = self.livestatus_broker.modules_manager.instances[0]
        self.livestatus_broker.db.open()
        #--- livestatus_broker.do_main

        #--- livestatus_broker.manage_lql_thread
        self.livestatus_broker.livestatus = LiveStatus(self.livestatus_broker.datamgr, self.livestatus_broker.query_cache, self.livestatus_broker.db, self.livestatus_broker.pnp_path, self.livestatus_broker.from_q)
Пример #4
0
    def init_livestatus(self):
        self.livelogs = 'tmp/livelogs.db' + self.testid
        modconf = Module({
            'module_name': 'LiveStatus',
            'module_type': 'livestatus',
            'port': str(50000 + os.getpid()),
            'pnp_path': 'tmp/pnp4nagios_test' + self.testid,
            'host': '127.0.0.1',
            'socket': 'live',
            'name': 'test',  #?
            'database_file': self.livelogs,
        })

        dbmodconf = Module({
            'module_name':
            'LogStore',
            'module_type':
            'logstore_sqlite',
            'use_aggressive_sql':
            "0",
            'database_file':
            self.livelogs,
            'archive_path':
            os.path.join(os.path.dirname(self.livelogs), 'archives'),
        })
        ####################################
        # !NOT! modconf.modules = [dbmodconf]
        ####################################
        self.livestatus_broker = LiveStatus_broker(modconf)
        self.livestatus_broker.create_queues()

        self.livestatus_broker.init()

        #--- livestatus_broker.main
        self.livestatus_broker.log = logger
        # this seems to damage the logger so that the scheduler can't use it
        #self.livestatus_broker.log.load_obj(self.livestatus_broker)
        self.livestatus_broker.debug_output = []
        self.livestatus_broker.modules_manager = ModulesManager(
            'livestatus', modulesctx.get_modulesdir(), [])
        self.livestatus_broker.modules_manager.set_modules(
            self.livestatus_broker.modules)
        # We can now output some previouly silented debug ouput
        self.livestatus_broker.do_load_modules()
        for inst in self.livestatus_broker.modules_manager.instances:
            if inst.properties["type"].startswith('logstore'):
                f = getattr(inst, 'load', None)
                if f and callable(f):
                    f(self.livestatus_broker)  # !!! NOT self here !!!!
                break
        for s in self.livestatus_broker.debug_output:
            print "errors during load", s
        del self.livestatus_broker.debug_output
        self.livestatus_broker.add_compatibility_sqlite_module()
        self.livestatus_broker.rg = LiveStatusRegenerator()
        self.livestatus_broker.datamgr = datamgr
        datamgr.load(self.livestatus_broker.rg)
        self.livestatus_broker.query_cache = LiveStatusQueryCache()
        self.livestatus_broker.query_cache.disable()
        self.livestatus_broker.rg.register_cache(
            self.livestatus_broker.query_cache)
        #--- livestatus_broker.main

        #--- livestatus_broker.do_main
        self.livestatus_broker.db = self.livestatus_broker.modules_manager.instances[
            0]
        self.livestatus_broker.db.open()
        #--- livestatus_broker.do_main

        #--- livestatus_broker.manage_lql_thread
        self.livestatus_broker.livestatus = LiveStatus(
            self.livestatus_broker.datamgr, self.livestatus_broker.query_cache,
            self.livestatus_broker.db, self.livestatus_broker.pnp_path,
            self.livestatus_broker.from_q)
Пример #5
0
    def main(self, stand_alone=False):
        """
            Module main function
        """
        self.stand_alone = stand_alone
        if self.stand_alone:
            setproctitle(self.name)
        else:
            self.set_proctitle(self.name)

        # Modules management
        self.debug_output = []
        self.modules_dir = modulesctx.get_modulesdir()
        self.modules_manager = ModulesManager('webui',
                                              self.find_modules_path(), [])
        self.modules_manager.set_modules(self.modules)
        logger.info("[WebUI] modules %s", self.modules)

        # We can now output some previously silenced debug output
        self.do_load_modules()
        for inst in self.modules_manager.instances:
            f = getattr(inst, 'load', None)
            if f and callable(f):
                f(self)
        for s in self.debug_output:
            print s
        del self.debug_output
        logger.info("[WebUI] loaded modules %s", self.modules)

        # Check if the Bottle view dir really exist
        if not os.path.exists(bottle.TEMPLATE_PATH[0]):
            logger.error("[WebUI] The view path do not exist at %s",
                         bottle.TEMPLATE_PATH)
            sys.exit(2)

        # Load internal sub modules
        self.auth_module = AuthMetaModule(
            AuthMetaModule.find_modules(
                self.modules_manager.get_internal_instances()), self)
        self.prefs_module = PrefsMetaModule(
            PrefsMetaModule.find_modules(
                self.modules_manager.get_internal_instances()), self)
        self.logs_module = LogsMetaModule(
            LogsMetaModule.find_modules(
                self.modules_manager.get_internal_instances()), self)
        self.graphs_module = GraphsMetaModule(
            GraphsMetaModule.find_modules(
                self.modules_manager.get_internal_instances()), self)
        self.helpdesk_module = HelpdeskMetaModule(
            HelpdeskMetaModule.find_modules(
                self.modules_manager.get_internal_instances()), self)

        # Data manager
        self.datamgr = WebUIDataManager(self.rg, self.frontend,
                                        self.alignak_backend_objects)
        self.helper = helper

        # Check directories
        # We check if the photo directory exists. If not, try to create it
        for dir in [self.share_dir, self.photo_dir, self.config_dir]:
            logger.debug("[WebUI] Checking dir: %s", dir)
            if not os.path.exists(dir):
                try:
                    # os.mkdir(dir)
                    os.makedirs(dir, mode=0o777)
                    logger.info("[WebUI] Created directory: %s", dir)
                except Exception as e:
                    logger.error(
                        "[WebUI] Directory creation failed: %s, error: %s",
                        dir, str(e))
            else:
                logger.debug("[WebUI] Still existing directory: %s", dir)

        # Bottle objects
        self.request = bottle.request
        self.response = bottle.response

        try:
            # I register my exit function
            self.set_exit_handler()

            # First load the additional plugins so they will have the lead on URI routes
            if self.additional_plugins_dir:
                self.load_plugins(self.additional_plugins_dir)

            # Modules can also override some views if need
            for inst in self.modules_manager.instances:
                f = getattr(inst, 'get_webui_plugins_path', None)
                if f and callable(f):
                    mod_plugins_path = os.path.abspath(f(self))
                    self.load_plugins(mod_plugins_path)

            # Then look at the plugins into core and load all we can there
            core_plugin_dir = os.path.join(
                os.path.abspath(os.path.dirname(__file__)), 'plugins')
            self.load_plugins(core_plugin_dir)

            # Declare the whole app static files AFTER the plugin ones
            self.declare_common_static()

            # Mount Web UI application
            # if self.endpoint:
            # root_app.mount(self.endpoint, webui_app)
            # logger.info("[WebUI] root routes: %s", root_app.routes)
            # for route in webui_app.routes:
            # logger.info("[WebUI] route: %s", route)

            for route in webui_app.routes:
                logger.info("[WebUI] route: %s", route)

            # We will protect the operations on
            # the non read+write with a lock and
            # 2 int
            self.global_lock = threading.RLock()
            self.nb_readers = 0
            self.nb_writers = 0

            self.data_thread = None
            self.ls_thread = None

            # For alignak backend ...
            if self.frontend:
                # Launch the livestate thread ...
                self.ls_thread = threading.Thread(None, self.manage_ls_thread,
                                                  'lsthread')
                self.ls_thread.start()
            else:
                # Launch the data thread ...
                self.data_thread = threading.Thread(None,
                                                    self.manage_brok_thread,
                                                    'datathread')
                self.data_thread.start()
                # TODO: look for alive and killing

            logger.info("[WebUI] starting Web UI server on %s:%d ...",
                        self.host, self.port)
            bottle.TEMPLATES.clear()
            webui_app.run(host=self.host,
                          port=self.port,
                          server=self.http_backend,
                          **self.serveropts)
        except Exception as e:
            if self.stand_alone:
                logger.error("[WebUI] do_main exception: %s", str(e))
                logger.error("[WebUI] traceback: %s", traceback.format_exc())
                exit(1)
            else:
                msg = Message(id=0,
                              type='ICrash',
                              data={
                                  'name': self.get_name(),
                                  'exception': e,
                                  'trace': traceback.format_exc()
                              })
                self.from_q.put(msg)
                # wait 2 sec so we know that the broker got our message, and die
                time.sleep(2)
                raise