def loop(self): """ The main read loop. """ consul_config = self.config.consul_config consul = Consul(consul_config) services = consul.get_services() check_service_jobs = self.create_check_service_jobs(services) pool, pool_size = self.create_pool(self.config) while len(check_service_jobs) > 0: if len(check_service_jobs) >= pool_size: pool.map(check_service, [check_service_jobs.pop() for _ in range(pool_size)]) else: pool.map(check_service, [check_service_jobs.pop() for _ in range(len(check_service_jobs))]) time.sleep(self.config.sleep) pool.close() pool.join()
def check_service(check_service_job): """ Check the availability of a consul service and send stats to a Sink. :param check_service_job: Dictionary containing job specification :type check_service_job: dict[str, str, ConsulConfig, SinkConfig] :return: Integer return code :rtype: int """ try: service = check_service_job['service'] tag = check_service_job['tag'] consul_config = check_service_job['consul_config'] sink_config = check_service_job['sink_config'] except KeyError as e: log.error( "check_service | Missing key {e} in check_service_job dict".format( e=e)) raise e consul = Consul(consul_config) sink = Sink(sink_config) dc = consul.get_dc() log.debug('check_service | Service:{service} Tag:{tag} DC:{dc}'.format( service=service, tag=tag, dc=dc)) consul_health_service = consul.get_health_service(service, tag) ok, critical = get_node_status(consul_health_service) sink.ok_count(ok, service, dc, tag) sink.critical_count(critical, service, dc, tag) if ok + critical > 0: sink.ok_percent((ok / (ok + critical)) * 100, service, dc, tag) sink.critical_percent((critical / (ok + critical)) * 100, service, dc, tag) return 0