Пример #1
0
def test_config_parser():
    elastic_check = ESCheck(CHECK_NAME, {}, {})
    instance = {
        "username": "******",
        "password": "******",
        "is_external": "yes",
        "url": "http://foo.bar",
        "tags": ["a", "b:c"],
    }
    c = elastic_check.get_instance_config(instance)
    assert c.username == "user"
    assert c.password == "pass"
    assert c.admin_forwarder is False
    assert c.cluster_stats is True
    assert c.url == "http://foo.bar"
    assert c.tags == ["url:http://foo.bar", "a", "b:c"]
    assert c.timeout == elastic_check.DEFAULT_TIMEOUT
    assert c.service_check_tags == ["host:foo.bar", "port:None", "a", "b:c"]

    instance = {"url": "http://192.168.42.42:12999", "timeout": 15}
    c = elastic_check.get_instance_config(instance)
    assert c.username is None
    assert c.password is None
    assert c.cluster_stats is False
    assert c.url == "http://192.168.42.42:12999"
    assert c.tags == ["url:http://192.168.42.42:12999"]
    assert c.timeout == 15
    assert c.service_check_tags == ["host:192.168.42.42", "port:12999"]

    instance = {
        "username": "******",
        "password": "******",
        "url": "https://foo.bar:9200",
        "ssl_verify": "true",
        "ssl_cert": "/path/to/cert.pem",
        "ssl_key": "/path/to/cert.key",
        "admin_forwarder": "1"
    }
    c = elastic_check.get_instance_config(instance)
    assert c.username == "user"
    assert c.password == "pass"
    assert c.admin_forwarder is True
    assert c.cluster_stats is False
    assert c.url == "https://foo.bar:9200"
    assert c.tags == ["url:https://foo.bar:9200"]
    assert c.timeout == elastic_check.DEFAULT_TIMEOUT
    assert c.service_check_tags == ["host:foo.bar", "port:9200"]
    assert c.ssl_verify == "true"
    assert c.ssl_cert == "/path/to/cert.pem"
    assert c.ssl_key == "/path/to/cert.key"
Пример #2
0
def test_check(aggregator, spin_up_elastic):
    elastic_check = ESCheck(CHECK_NAME, {}, {})
    default_tags = ["url:http://{0}:{1}".format(HOST, PORT)]

    for _ in xrange(2):
        try:
            elastic_check.check(CONFIG)
        except Exception:
            time.sleep(1)

    elastic_check.check(CONFIG)

    expected_metrics = dict(ESCheck.STATS_METRICS)
    ESCheck.CLUSTER_HEALTH_METRICS.update(ESCheck.CLUSTER_PENDING_TASKS)
    expected_metrics.update(ESCheck.CLUSTER_HEALTH_METRICS)

    instance = elastic_check.get_instance_config(CONFIG)
    es_version = elastic_check._get_es_version(instance)

    assert es_version == get_es_version()

    if es_version < [5, 0, 0]:
        expected_metrics.update(ESCheck.ADDITIONAL_METRICS_PRE_5_0_0)

    if es_version >= [0, 90, 5]:
        expected_metrics.update(ESCheck.ADDITIONAL_METRICS_POST_0_90_5)
        if es_version >= [0, 90, 10]:
            expected_metrics.update(ESCheck.JVM_METRICS_POST_0_90_10)
        else:
            expected_metrics.update(ESCheck.JVM_METRICS_PRE_0_90_10)
    else:
        expected_metrics.update(ESCheck.ADDITIONAL_METRICS_PRE_0_90_5)
        expected_metrics.update(ESCheck.JVM_METRICS_PRE_0_90_10)

    if es_version >= [1, 0, 0]:
        expected_metrics.update(ESCheck.ADDITIONAL_METRICS_POST_1_0_0)

    if es_version < [2, 0, 0]:
        expected_metrics.update(ESCheck.ADDITIONAL_METRICS_PRE_2_0)
        if es_version >= [0, 90, 5]:
            expected_metrics.update(
                ESCheck.ADDITIONAL_METRICS_POST_0_90_5_PRE_2_0)
        # Currently has issues in test framework
        # if es_version >= [1, 0, 0]:
        # expected_metrics.update(ESCheck.ADDITIONAL_METRICS_1_x)

    if es_version >= [1, 3, 0]:
        expected_metrics.update(ESCheck.ADDITIONAL_METRICS_POST_1_3_0)

    if es_version >= [1, 4, 0]:
        expected_metrics.update(ESCheck.ADDITIONAL_METRICS_POST_1_4_0)

    if es_version >= [1, 5, 0]:
        expected_metrics.update(ESCheck.ADDITIONAL_METRICS_POST_1_5_0)

    if es_version >= [1, 6, 0]:
        expected_metrics.update(ESCheck.ADDITIONAL_METRICS_POST_1_6_0)

    if es_version >= [2, 0, 0]:
        expected_metrics.update(ESCheck.ADDITIONAL_METRICS_POST_2_0)

    if es_version >= [2, 1, 0]:
        expected_metrics.update(ESCheck.ADDITIONAL_METRICS_POST_2_1)

    if os.environ.get("DD_ELASTIC_LOCAL_HOSTNAME"):
        local_hostname = os.environ.get("DD_ELASTIC_LOCAL_HOSTNAME")
    elif es_version < [2, 0, 0]:
        local_hostname = socket.gethostname()
    else:
        local_hostname = '127.0.0.1'

    contexts = [(CONF_HOSTNAME, default_tags + TAGS),
                (local_hostname, default_tags + TAGS)]

    stats_keys = (set(expected_metrics.keys()) -
                  set(ESCheck.CLUSTER_HEALTH_METRICS.keys()) -
                  set(ESCheck.CLUSTER_PENDING_TASKS.keys()))

    # index_writer_max_memory metric was removed in v5
    deprecated_metrics = [
        'elasticsearch.indices.segments.index_writer_max_memory_in_bytes'
    ]
    for m_name, desc in set(expected_metrics.iteritems()):
        for hostname, m_tags in contexts:
            m_tags = m_tags + CLUSTER_TAG
            if (m_name in ESCheck.CLUSTER_HEALTH_METRICS
                    and hostname == local_hostname):
                hostname = CONF_HOSTNAME

            if m_name in stats_keys:
                m_tags = m_tags + [u"node_name:batman"]

            m_tags.sort()
            if desc[0] == "gauge":
                aggregator.metrics(m_name)
                if es_version < [2, 0, 0]:
                    aggregator.assert_metric(m_name, tags=m_tags)
                elif not (m_name in deprecated_metrics):
                    aggregator.assert_metric(m_name)

    good_sc_tags = ['host:{0}'.format(HOST), 'port:{0}'.format(PORT)]
    aggregator.assert_service_check(
        'elasticsearch.can_connect',
        status=ESCheck.OK,  # OK
        tags=good_sc_tags + TAGS)

    # Assert service metadata
    # self.assertServiceMetadata(['version'], count=3)
    # FIXME: 0.90.13 returns randomly a red status instead of yellow,
    # so we don't do a coverage test for it
    # Remove me when we stop supporting 0.90.x (not supported anymore by ES)
    if get_es_version() != [0, 90, 13]:
        # Warning because elasticsearch status should be yellow, according to
        # http://chrissimpson.co.uk/elasticsearch-yellow-cluster-status-explained.html
        aggregator.assert_service_check('elasticsearch.cluster_health')