Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create-index bug fix #20

Merged
merged 2 commits into from
Apr 20, 2023
Merged
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
30 changes: 24 additions & 6 deletions elastalert/create_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,14 @@ def create_index_mappings(es_client, ea_index, recreate=False, old_ea_index=None
esversion = get_version_from_cluster_info(es_client)

es_index_mappings = {}

#using es_mappings 7
es_index_mappings = read_es_index_mappings(7)

if is_atleasteight(esversion):
es_index_mappings = read_es_index_mappings()
elif is_atleastseven(esversion) or is_atleastsix(esversion):
es_index_mappings = read_es_index_mappings(7)
else:
print('FATAL - Unsupported Elasticsearch version: ' + esversion + '. Aborting.')
exit(1)

es_index = IndicesClient(es_client)
if not recreate:
Expand All @@ -35,7 +40,7 @@ def create_index_mappings(es_client, ea_index, recreate=False, old_ea_index=None
return None

# (Re-)Create indices.
if is_atleastseven(esversion):
if is_atleastseven(esversion) or is_atleastsix(esversion):
index_names = (
ea_index,
ea_index + '_status',
Expand Down Expand Up @@ -70,7 +75,7 @@ def create_index_mappings(es_client, ea_index, recreate=False, old_ea_index=None
body=es_index_mappings['elastalert_error'])
es_client.indices.put_mapping(index=ea_index + '_past',
body=es_index_mappings['past_elastalert'])
elif is_atleastseven(esversion):
elif is_atleastseven(esversion) :
es_client.indices.put_mapping(index=ea_index, doc_type='_doc',
body=es_index_mappings['elastalert'], include_type_name=True)
es_client.indices.put_mapping(index=ea_index + '_status', doc_type='_doc',
Expand All @@ -81,7 +86,17 @@ def create_index_mappings(es_client, ea_index, recreate=False, old_ea_index=None
body=es_index_mappings['elastalert_error'], include_type_name=True)
es_client.indices.put_mapping(index=ea_index + '_past', doc_type='_doc',
body=es_index_mappings['past_elastalert'], include_type_name=True)

elif is_atleastsix(esversion):
es_client.indices.put_mapping(index=ea_index, doc_type='elastalert',
body=es_index_mappings['elastalert'])
es_client.indices.put_mapping(index=ea_index + '_status', doc_type='elastalert_status',
body=es_index_mappings['elastalert_status'])
es_client.indices.put_mapping(index=ea_index + '_silence', doc_type='silence',
body=es_index_mappings['silence'])
es_client.indices.put_mapping(index=ea_index + '_error', doc_type='elastalert_error',
body=es_index_mappings['elastalert_error'])
es_client.indices.put_mapping(index=ea_index + '_past', doc_type='past_elastalert',
body=es_index_mappings['past_elastalert'])
print('New index %s created' % ea_index)
if old_ea_index:
print("Copying all data from old index '{0}' to new index '{1}'".format(old_ea_index, ea_index))
Expand Down Expand Up @@ -110,6 +125,9 @@ def read_es_index_mapping(mapping, es_version=7):
print("Reading index mapping '{0}'".format(mapping_path))
return json.load(f)

def is_atleastsix(es_version):
return int(es_version.split(".")[0]) >= 6

def is_atleastseven(es_version):
return int(es_version.split(".")[0]) >= 7

Expand Down