Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions elastalert/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import os
import datetime
import logging

import dateutil.parser
import dateutil.tz
from auth import Auth
Expand Down Expand Up @@ -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'],
Expand All @@ -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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check isn't necessary, it's always there.

es_host = os.environ.get('ES_HOST', conf['es_host'])

if "es_host" in conf:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

es_port

Though, this also isn't necessary.

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'])
Expand Down Expand Up @@ -344,6 +347,18 @@ def build_es_conn_config(conf):
return parsed_conf


def parse_host(host, port="9200"):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not accept a list instead of a comma separated string?

"""
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('=')
Expand Down
11 changes: 11 additions & 0 deletions tests/util_unit_test.py
Original file line number Diff line number Diff line change
@@ -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"])