diff --git a/elastalert/util.py b/elastalert/util.py index 547e72040..25a3e04cd 100644 --- a/elastalert/util.py +++ b/elastalert/util.py @@ -2,7 +2,6 @@ import os import datetime import logging - import dateutil.parser import dateutil.tz from auth import Auth @@ -283,7 +282,6 @@ def elasticsearch_client(conf): profile_name=es_conn_conf['profile']) return Elasticsearch(host=es_conn_conf['es_host'], - port=es_conn_conf['es_port'], url_prefix=es_conn_conf['es_url_prefix'], use_ssl=es_conn_conf['use_ssl'], verify_certs=es_conn_conf['verify_certs'], @@ -308,12 +306,17 @@ 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'])) 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') + if "es_host" in conf: + es_host = os.environ.get('ES_HOST', conf['es_host']) + + if "es_host" in conf: + es_port = int(os.environ.get('ES_PORT', conf['es_port'])) + parsed_conf['es_host'] = parse_host(es_host, es_port) + if 'es_username' in conf: parsed_conf['es_username'] = os.environ.get('ES_USERNAME', conf['es_username']) parsed_conf['es_password'] = os.environ.get('ES_PASSWORD', conf['es_password']) @@ -344,6 +347,18 @@ def build_es_conn_config(conf): return parsed_conf +def parse_host(host, port="9200"): + """ + Convet host str like "host1:port1, host2:port2" to list + """ + 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)] + + def parse_duration(value): """Convert ``unit=num`` spec into a ``timedelta`` object.""" unit, num = value.split('=') diff --git a/tests/util_unit_test.py b/tests/util_unit_test.py new file mode 100644 index 000000000..46ece8eda --- /dev/null +++ b/tests/util_unit_test.py @@ -0,0 +1,11 @@ +from unittest import TestCase + +from elastalert.util import parse_host + + +class UtilsTest(TestCase): + def test_parse_host(self): + self.assertEqual(parse_host("localhost", port="9200"), ["localhost:9200"]) + self.assertEqual(parse_host('host1:9200,host2:9200, host3:9300'), ["host1:9200", + "host2:9200", + "host3:9300"])