Ejemplo n.º 1
0
    def run(self, conf):

        name = 'stalker-agent'
        log_type = conf.get('log_type', 'syslog')
        log_file = conf.get('log_path', '/var/log/stalker/stalker-runner.log')
        if log_type == 'syslog':
            logger = get_syslogger(conf, name)
        else:
            logger = get_logger(name, log_path=log_file)

        def spawn_worker():
            sr = StalkerRunner(conf)
            while 1:
                try:
                    sr.start()
                except Exception as err:
                    logger.info(err)

        worker_count = int(conf.get('workers', '1'))

        def kill_children(*args):
            """Kills the entire process group."""
            logger.error('SIGTERM received')
            signal.signal(signal.SIGTERM, signal.SIG_IGN)
            running[0] = False
            os.killpg(0, signal.SIGTERM)

        def hup(*args):
            """Shuts down the server, but allow running requests to complete"""
            logger.error('SIGHUP received')
            signal.signal(signal.SIGHUP, signal.SIG_IGN)
            running[0] = False

        running = [True]
        signal.signal(signal.SIGTERM, kill_children)
        signal.signal(signal.SIGHUP, hup)
        children = []
        while running[0]:
            while len(children) < worker_count:
                pid = os.fork()
                if pid == 0:
                    signal.signal(signal.SIGHUP, signal.SIG_DFL)
                    signal.signal(signal.SIGTERM, signal.SIG_DFL)
                    spawn_worker()
                    logger.info('Child %d exiting normally' % os.getpid())
                    return
                else:
                    logger.info('Started child %s' % pid)
                    children.append(pid)
            try:
                pid, status = os.wait()
                if os.WIFEXITED(status) or os.WIFSIGNALED(status):
                    logger.error('Removing dead child %s' % pid)
                    if pid in children:
                        children.remove(pid)
            except OSError, err:
                if err.errno not in (errno.EINTR, errno.ECHILD):
                    raise
            except KeyboardInterrupt:
                break
Ejemplo n.º 2
0
 def __init__(self, conf):
     self.conf = conf
     self.name = 'stalker-runner-%d' % os.getpid()
     log_type = conf.get('log_type', 'syslog')
     log_file = conf.get('log_file', '/var/log/stalker/stalker-runner.log')
     if log_type == 'syslog':
         self.logger = get_syslogger(conf, self.name)
     else:
         self.logger = get_logger(self.name, log_path=log_file)
     self.pool = eventlet.GreenPool()
     self.check_key = conf.get('check_key', 'canhazstatus')
     redis_host = conf.get('redis_host', '127.0.0.1')
     redis_port = int(conf.get('redis_port', '6379'))
     redis_pass = conf.get('redis_password', '')
     redis_usock = conf.get('redis_socket', None)
     self.wq = conf.get('worker_id', 'worker1')
     self.rc = redis.Redis(redis_host, redis_port, password=redis_pass,
                           unix_socket_path=redis_usock)
     mongo_host = conf.get('mongo_host', '127.0.0.1')
     mongo_port = int(conf.get('mongo_port', '27017'))
     db_name = conf.get('db_name', 'stalkerweb')
     self.c = MongoClient(host=mongo_host, port=mongo_port)
     self.debug = False
     self.db = self.c[db_name]
     self.checks = self.db['checks']
     self.state_log = self.db['state_log']
     self.notifications = self.db['notifications']
     self.host_window = int(conf.get('host_flood_window', '60'))
     self.host_threshold = int(conf.get('host_flood_threshold', '5'))
     self.flood_window = int(conf.get('dc_flood_window', '120'))
     self.flood_threshold = int(conf.get('dc_flood_threshold', '100'))
     self.flap_window = int(conf.get('flap_window', '1200'))
     self.flap_threshold = int(conf.get('flap_threshold', '5'))
     self.alert_threshold = int(conf.get('alert_threshold', '3'))
     self.urlopen_timeout = int(conf.get('urlopen_timeout', '15'))
     self.notify_plugins = {}
     self._load_notification_plugins(conf)
     self.statsd = StatsdEvent(conf, self.logger, 'stalker_runner.')