示例#1
0
文件: daemon.py 项目: qiq/smoker
    def run(self):
        """
        Run daemon
         * change effective uid/gid
         * start thread for each check
         * start webserver
        """
        lg.info("Starting daemon")

        # Change effective UID/GID
        if self.conf.has_key('uid') and self.conf.has_key('gid'):
            if os.geteuid != self.conf['uid'] and os.getegid != self.conf[
                    'gid']:
                try:
                    os.setegid(self.conf['gid'])
                    os.seteuid(self.conf['uid'])
                except TypeError as e:
                    lg.error(
                        "Config parameters uid/gid have to be integers: %s" %
                        e)
                except OSError as e:
                    lg.error("Can't switch effective UID/GID to %s/%s: %s" %
                             (self.conf['uid'], self.conf['gid'], e))
                    lg.exception(e)
                    self._shutdown(exitcode=1)
        else:
            lg.info("Not changing effective UID/GID, keeping %s/%s" %
                    (os.geteuid(), os.getegid()))

        if not isinstance(self.conf['bind_port'], int):
            lg.error("Config parameter bind_port has to be integer")

        # Initialize plugin manager
        config = {}

        for key in ['plugins', 'templates', 'actions']:
            try:
                config[key] = self.conf[key]
            except KeyError as e:
                lg.warn("Config section not found: %s" % e)

        # Check we have some plugins configured
        if not config['plugins']:
            lg.error('No configured plugins')
            self._shutdown(exitcode=1)

        try:
            self.pluginmgr = PluginManager(**config)
        except Exception as e:
            lg.error("Can't initialize PluginManager")
            lg.exception(e)
            self._shutdown(exitcode=1)

        try:
            self.pluginmgr.start()
        except Exception as e:
            lg.error("Can't start PluginManager")
            lg.exception(e)
            self._shutdown(exitcode=1)

        lg.info("Starting webserver on %(bind_host)s:%(bind_port)s" %
                self.conf)
        try:
            self.server = ThreadedHTTPServer(
                (self.conf['bind_host'], self.conf['bind_port']), HTTPHandler,
                self)
            try:
                self.server.serve_forever()
            except select.error as e:
                # Suppress exception during shutdown
                # (4, 'Interrupted system call')
                pass
        except KeyboardInterrupt:
            lg.info("Interrupted")
        except Exception as e:
            lg.error("Can't start HTTP server: %s" % e)
            lg.exception(e)
            self._shutdown(exitcode=1)
示例#2
0
    def run(self):
        """
        Run daemon
         * change effective uid/gid
         * start thread for each check
         * start webserver
        """
        lg.info("Starting daemon")

        # Change effective UID/GID
        if 'uid' in self.conf and 'gid' in self.conf:
            if os.geteuid != self.conf['uid'] and os.getegid != self.conf[
                    'gid']:
                try:
                    os.setegid(self.conf['gid'])
                    os.seteuid(self.conf['uid'])
                except TypeError as e:
                    lg.error(
                        "Config parameters uid/gid have to be integers: %s" %
                        e)
                except OSError as e:
                    lg.error("Can't switch effective UID/GID to %s/%s: %s" %
                             (self.conf['uid'], self.conf['gid'], e))
                    lg.exception(e)
                    self._shutdown(exitcode=1)
        else:
            lg.info("Not changing effective UID/GID, keeping %s/%s" %
                    (os.geteuid(), os.getegid()))

        if not isinstance(self.conf['bind_port'], int):
            lg.error("Config parameter bind_port has to be integer")

        # Initialize plugin manager
        config = {}

        for key in ['plugins', 'templates', 'actions']:
            try:
                config[key] = self.conf[key]
            except KeyError as e:
                lg.warn("Config section not found: %s" % e)

        # Check we have some plugins configured
        if not config['plugins']:
            lg.error('No configured plugins')
            self._shutdown(exitcode=1)

        if 'nr_concurrent_plugins' in self.conf:
            config['semaphore_count'] = self.conf['nr_concurrent_plugins']

        try:
            self.pluginmgr = PluginManager(**config)
        except Exception as e:
            lg.error("Can't initialize PluginManager")
            lg.exception(e)
            self._shutdown(exitcode=1)

        lg.info("Starting webserver on %(bind_host)s:%(bind_port)s" %
                self.conf)
        try:
            self.server = RestServer(self)
            self.server.start()
        except Exception as e:
            lg.error("Can't start HTTP server: %s" % e)
            lg.exception(e)
            self._shutdown(exitcode=1)

        # Catch SIGINT and SIGTERM if supported
        if hasattr(signal, 'SIGINT'):
            signal.signal(signal.SIGINT, self._shutdown)

        if hasattr(signal, 'SIGTERM'):
            signal.signal(signal.SIGTERM, self._shutdown)

        if hasattr(signal, 'SIGHUP'):
            signal.signal(signal.SIGHUP, self._reopen_logfiles)

        self._watchdog()