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

Support OpenSearch v1.0.0 #483

Merged
merged 8 commits into from
Sep 25, 2021
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
- [OpsGenie] Add support for custom description - [#457](https://github.com/jertel/elastalert2/pull/457), [#460](https://github.com/jertel/elastalert2/pull/460) - @nickbabkin
- [Tencent SMS] Added support for Tencent SMS - [#470](https://github.com/jertel/elastalert2/pull/470) - @liuxingjun
- Add support for Kibana 7.15 for Kibana Discover - [#481](https://github.com/jertel/elastalert2/pull/481) - @nsano-rururu
- Begin working toward support of OpenSearch (This is still a work in progress) [#483](https://github.com/jertel/elastalert2/pull/483) @nbrownus

## Other changes
- [Rule Test] Fix issue related to --start/--end/--days params - [#424](https://github.com/jertel/elastalert2/pull/424), [#433](https://github.com/jertel/elastalert2/pull/433) - @thican
Expand Down
2 changes: 1 addition & 1 deletion docs/source/elastalert.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
ElastAlert 2 - Automated rule-based alerting for Elasticsearch
**************************************************************

ElastAlert 2 is a simple framework for alerting on anomalies, spikes, or other patterns of interest from data in Elasticsearch.
ElastAlert 2 is a simple framework for alerting on anomalies, spikes, or other patterns of interest from data in `Elasticsearch <https://www.elastic.co/elasticsearch/>` and `OpenSearch <https://opensearch.org/>` (Under development).

If you have data being written into Elasticsearch in near real time and want to be alerted when that data matches certain patterns, ElastAlert 2 is the tool for you.

Expand Down
8 changes: 7 additions & 1 deletion elastalert/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,13 @@ def es_version(self):
if self._es_version is None:
for retry in range(3):
try:
self._es_version = self.info()['version']['number']
esinfo = self.info()['version']
if esinfo['distribution'] == "opensearch":
# OpenSearch is based on Elasticsearch 7.10.2, currently only v1.0.0 exists
# https://opensearch.org/
self._es_version = "7.10.2"
else:
self._es_version = esinfo['number']
break
except TransportError:
if retry == 2:
Expand Down
9 changes: 7 additions & 2 deletions elastalert/create_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,13 @@


def create_index_mappings(es_client, ea_index, recreate=False, old_ea_index=None):
esversion = es_client.info()["version"]["number"]
print("Elastic Version: " + esversion)
esinfo = es_client.info()['version']
if esinfo['distribution'] == "opensearch":
# OpenSearch is based on Elasticsearch 7.10.2, currently only v1.0.0 exists
# https://opensearch.org/
esversion = "7.10.2"
else:
esversion = esinfo['number']

es_index_mappings = read_es_index_mappings() if is_atleastsix(esversion) else read_es_index_mappings(5)

Expand Down
9 changes: 7 additions & 2 deletions elastalert/ruletypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -918,8 +918,13 @@ def add_terms_data(self, terms):
self.seen_values[field].append(bucket['key'])

def is_five_or_above(self):
version = self.es.info()['version']['number']
return int(version[0]) >= 5
esinfo = self.es.info()['version']
if esinfo['distribution'] == "opensearch":
# OpenSearch is based on Elasticsearch 7.10.2, currently only v1.0.0 exists
# https://opensearch.org/
return True
else:
return int(esinfo['number'][0]) >= 5


class CardinalityRule(RuleType):
Expand Down
12 changes: 6 additions & 6 deletions tests/rules_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ def test_new_term():
with mock.patch('elastalert.ruletypes.elasticsearch_client') as mock_es:
mock_es.return_value = mock.Mock()
mock_es.return_value.search.return_value = mock_res
mock_es.return_value.info.return_value = {'version': {'number': '2.x.x'}}
mock_es.return_value.info.return_value = {'version': {'number': '2.x.x', 'distribution': 'mock-es'}}
call_args = []

# search is called with a mutable dict containing timestamps, this is required to test
Expand Down Expand Up @@ -619,7 +619,7 @@ def record_args(*args, **kwargs):
with mock.patch('elastalert.ruletypes.elasticsearch_client') as mock_es:
mock_es.return_value = mock.Mock()
mock_es.return_value.search.return_value = mock_res
mock_es.return_value.info.return_value = {'version': {'number': '2.x.x'}}
mock_es.return_value.info.return_value = {'version': {'number': '2.x.x', 'distribution': 'mock-es'}}
rule = NewTermsRule(rules)
rule.add_data([{'@timestamp': ts_now(), 'a': 'key2'}])
assert len(rule.matches) == 1
Expand All @@ -637,7 +637,7 @@ def test_new_term_nested_field():
with mock.patch('elastalert.ruletypes.elasticsearch_client') as mock_es:
mock_es.return_value = mock.Mock()
mock_es.return_value.search.return_value = mock_res
mock_es.return_value.info.return_value = {'version': {'number': '2.x.x'}}
mock_es.return_value.info.return_value = {'version': {'number': '2.x.x', 'distribution': 'mock-es'}}
rule = NewTermsRule(rules)

assert rule.es.search.call_count == 60
Expand All @@ -662,7 +662,7 @@ def test_new_term_with_terms():
with mock.patch('elastalert.ruletypes.elasticsearch_client') as mock_es:
mock_es.return_value = mock.Mock()
mock_es.return_value.search.return_value = mock_res
mock_es.return_value.info.return_value = {'version': {'number': '2.x.x'}}
mock_es.return_value.info.return_value = {'version': {'number': '2.x.x', 'distribution': 'mock-es'}}
rule = NewTermsRule(rules)

# Only 15 queries because of custom step size
Expand Down Expand Up @@ -732,7 +732,7 @@ def test_new_term_with_composite_fields():
with mock.patch('elastalert.ruletypes.elasticsearch_client') as mock_es:
mock_es.return_value = mock.Mock()
mock_es.return_value.search.return_value = mock_res
mock_es.return_value.info.return_value = {'version': {'number': '2.x.x'}}
mock_es.return_value.info.return_value = {'version': {'number': '2.x.x', 'distribution': 'mock-es'}}
rule = NewTermsRule(rules)

assert rule.es.search.call_count == 60
Expand Down Expand Up @@ -769,7 +769,7 @@ def test_new_term_with_composite_fields():
with mock.patch('elastalert.ruletypes.elasticsearch_client') as mock_es:
mock_es.return_value = mock.Mock()
mock_es.return_value.search.return_value = mock_res
mock_es.return_value.info.return_value = {'version': {'number': '2.x.x'}}
mock_es.return_value.info.return_value = {'version': {'number': '2.x.x', 'distribution': 'mock-es'}}
rule = NewTermsRule(rules)
rule.add_data([{'@timestamp': ts_now(), 'a': 'key2'}])
assert len(rule.matches) == 2
Expand Down