Beispiel #1
0
    def create_check_service_jobs(self, services):
        """

        :param services: A dict of services and tags
        :type services: dict

        :return: A list of check service jobs
        :rtype: dict
        """
        check_service_jobs = list()
        for name, tags in iteritems(services):
            log.debug("check_service_jobs | Name: {0} | Tags: {1}".format(
                name, tags
            ))
            for tag in tags:
                check_service_jobs.append(
                    {'service': name, 'tag': tag,
                     'consul_config': self.config.consul_config,
                     'sink_config': self.config.sink_config})

            check_service_jobs.append(
                {'service': name, 'tag': None,
                 'consul_config': self.config.consul_config,
                 'sink_config': self.config.sink_config})

        return check_service_jobs
Beispiel #2
0
    def __init__(self, pool_size=1, sleep=1, consul_config=None,
                 sink_config=None):
        """
        :param pool_size: Number of checks to run in parallel
        :type pool_size: int

        :param sleep: Time to sleep between checks in the main loop in seconds
        :type sleep: float

        :param consul_config: Consul Configuration object
        :type consul_config: ConsulConfig

        :param sink_config: Sink Configuration object
        :type sink_config: SinkConfig
        """
        self.pool_size = pool_size
        self.sleep = sleep
        if isinstance(consul_config, type(None)):
            log.debug("LiaisonConfig | Using default ConsulConfig")
            self.consul_config = ConsulConfig()
        elif isinstance(consul_config, ConsulConfig):
            self.consul_config = consul_config
        else:
            raise Exception(
                "LiaisonConfig | Bad consul_config parameter. Invalid type")

        if isinstance(sink_config, type(None)):
            log.debug("LiaisonConfig | Using default SinkConfig")
            self.sink_config = SinkConfig()
        elif isinstance(sink_config, SinkConfig):
            self.sink_config = sink_config
        else:
            raise Exception(
                "LiaisonConfig | Bad sink_config parameter. Invalid type")
Beispiel #3
0
def get_node_status(consul_health_service):
    """

    :param consul_health_service: A list of representation
    of the result of a query to /v1/health/service/<service>
    :type consul_health_service: list

    :return: number of nodes without critical checks,
        number of nodes with critical checks
    :rtype: float, float
    """
    nodes = dict()
    ok = 0.0
    critical = 0.0

    for node in consul_health_service:
        name = node['Node']['Node']

        if name not in nodes:
            nodes[name] = dict()
            nodes[name]['passing'] = 0
            nodes[name]['warning'] = 0
            nodes[name]['critical'] = 0

        for check in node['Checks']:
            log.debug("get_node_status | Node: {0} | Check: {1}".format(
                name, check
            ))
            if check['Status'] == 'passing':
                nodes[name]['passing'] += 1
            elif check['Status'] == 'warning':
                nodes[name]['warning'] += 1
            elif check['Status'] == 'critical':
                nodes[name]['critical'] += 1
            else:
                log.warning(
                    'get_node_status | Node {0} has check with unexpected'
                    ' status | Check: {1}'.format(name, check))
                continue

        if nodes[name]['critical'] > 0:
            critical += 1
        else:
            ok += 1

    return ok, critical
Beispiel #4
0
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