From fe6e192c8a6fec7333b6824df71d58177ccab74c Mon Sep 17 00:00:00 2001 From: chenxi Date: Sat, 30 Jan 2021 10:21:55 +0800 Subject: [PATCH 1/3] feat: suport for multi instances parse --- elastalert/util.py | 22 ++++++++++++++++++++-- tests/util_test.py | 9 ++++++++- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/elastalert/util.py b/elastalert/util.py index bbb0600ff..4c5cff412 100644 --- a/elastalert/util.py +++ b/elastalert/util.py @@ -343,8 +343,10 @@ def build_es_conn_config(conf): parsed_conf['es_password'] = None parsed_conf['aws_region'] = None parsed_conf['profile'] = None - parsed_conf['es_host'] = os.environ.get('ES_HOST', conf['es_host']) - parsed_conf['es_port'] = int(os.environ.get('ES_PORT', conf['es_port'])) + es_host = os.environ.get('ES_HOST', conf['es_host']) + es_port = int(os.environ.get('ES_PORT', conf['es_port'])) + parsed_conf['es_host'] = parse_host(es_host, es_port) + parsed_conf['es_port'] = es_port parsed_conf['es_url_prefix'] = '' parsed_conf['es_conn_timeout'] = conf.get('es_conn_timeout', 20) parsed_conf['send_get_body_as'] = conf.get('es_send_get_body_as', 'GET') @@ -460,3 +462,19 @@ def should_scrolling_continue(rule_conf): stop_the_scroll = 0 < max_scrolling <= rule_conf.get('scrolling_cycle') return not stop_the_scroll + +def parse_host(host, port=9200): + """ + Convet host str like "host1:port1, host2:port2" to list + + :param host str: hostnames (separated with comma ) or single host name + :param port: default to 9200 + :return: list of hosts + """ + if "," in host: + host_list = host.split(",") + host_list = [x.strip() for x in host_list] + return host_list + else: + return ["{host}:{port}".format(host=host, port=port)] + diff --git a/tests/util_test.py b/tests/util_test.py index 55a2f9c8f..b3c0706ad 100644 --- a/tests/util_test.py +++ b/tests/util_test.py @@ -6,7 +6,7 @@ import pytest from dateutil.parser import parse as dt -from elastalert.util import add_raw_postfix +from elastalert.util import add_raw_postfix, parse_host from elastalert.util import format_index from elastalert.util import lookup_es_key from elastalert.util import parse_deadline @@ -228,3 +228,10 @@ def test_should_scrolling_continue(): assert should_scrolling_continue(rule_before_first_run) is True assert should_scrolling_continue(rule_before_max_scrolling) is True assert should_scrolling_continue(rule_over_max_scrolling) is False + + +def test_parse_host(): + assert parse_host("localhost", port=9200) == ["localhost:9200"] + assert parse_host("host1:9200, host2:9200, host3:9300") ==["host1:9200", + "host2:9200", + "host3:9300"] From 3bce4ad1e55de79dc8436a831205f73848240eed Mon Sep 17 00:00:00 2001 From: chenxi Date: Sat, 30 Jan 2021 12:56:50 +0800 Subject: [PATCH 2/3] feat: modify ElasticSearchClient init params to 'hosts' from 'host' --- elastalert/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/elastalert/__init__.py b/elastalert/__init__.py index 55bfdb32f..38100b0eb 100644 --- a/elastalert/__init__.py +++ b/elastalert/__init__.py @@ -16,7 +16,7 @@ def __init__(self, conf): """ :arg conf: es_conn_config dictionary. Ref. :func:`~util.build_es_conn_config` """ - super(ElasticSearchClient, self).__init__(host=conf['es_host'], + super(ElasticSearchClient, self).__init__(hosts=conf['es_host'], port=conf['es_port'], url_prefix=conf['es_url_prefix'], use_ssl=conf['use_ssl'], From 8a817dd6a4184f786d1c60aadcf8a2e05b69af2c Mon Sep 17 00:00:00 2001 From: chenxi Date: Sat, 30 Jan 2021 13:48:09 +0800 Subject: [PATCH 3/3] fix: add test case for config part --- tests/util_test.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/tests/util_test.py b/tests/util_test.py index b3c0706ad..72d2b6ab2 100644 --- a/tests/util_test.py +++ b/tests/util_test.py @@ -6,7 +6,7 @@ import pytest from dateutil.parser import parse as dt -from elastalert.util import add_raw_postfix, parse_host +from elastalert.util import add_raw_postfix, parse_host, build_es_conn_config from elastalert.util import format_index from elastalert.util import lookup_es_key from elastalert.util import parse_deadline @@ -235,3 +235,14 @@ def test_parse_host(): assert parse_host("host1:9200, host2:9200, host3:9300") ==["host1:9200", "host2:9200", "host3:9300"] + +def test_build_cofig_for_multi(): + assert build_es_conn_config({ + "es_host":"localhost", + "es_port": 9200 + })['es_host'] == ['localhost:9200'] + + assert build_es_conn_config({ + "es_host": "host1:9200, host2:9200, host3:9300", + "es_port": 9200 + })['es_host'] == ["host1:9200","host2:9200","host3:9300"]