From 68fb1baf61697aa6dfc6a79a5e021fac3d70b5ef Mon Sep 17 00:00:00 2001 From: Feroz Salam Date: Sun, 16 May 2021 12:08:50 +0100 Subject: [PATCH] Begin alerter refactoring and migration - Create a new alerters folder - Move the Zabbix and OpsGenie alerters to the folder - Refactor to load the alerters from the folder - Add a new test for the Zabbix alerter Once this is merged (and if we are happy with this format for the migration), I will do all the other alerters. --- elastalert/alerters/__init__.py | 0 elastalert/{ => alerters}/opsgenie.py | 10 ++++---- elastalert/{ => alerters}/zabbix.py | 4 ++-- elastalert/loaders.py | 4 ++-- tests/alerts_test.py | 33 ++++++++++++++++++++++++++- 5 files changed, 41 insertions(+), 10 deletions(-) create mode 100644 elastalert/alerters/__init__.py rename elastalert/{ => alerters}/opsgenie.py (97%) rename elastalert/{ => alerters}/zabbix.py (98%) diff --git a/elastalert/alerters/__init__.py b/elastalert/alerters/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/elastalert/opsgenie.py b/elastalert/alerters/opsgenie.py similarity index 97% rename from elastalert/opsgenie.py rename to elastalert/alerters/opsgenie.py index 8db52d89..7b3ac702 100644 --- a/elastalert/opsgenie.py +++ b/elastalert/alerters/opsgenie.py @@ -3,11 +3,11 @@ import os.path import requests -from .alerts import Alerter -from .alerts import BasicMatchString -from .util import EAException -from .util import elastalert_logger -from .util import lookup_es_key +from ..alerts import Alerter +from ..alerts import BasicMatchString +from ..util import EAException +from ..util import elastalert_logger +from ..util import lookup_es_key class OpsGenieAlerter(Alerter): diff --git a/elastalert/zabbix.py b/elastalert/alerters/zabbix.py similarity index 98% rename from elastalert/zabbix.py rename to elastalert/alerters/zabbix.py index e2b5f1ed..1ec9eab0 100644 --- a/elastalert/zabbix.py +++ b/elastalert/alerters/zabbix.py @@ -2,8 +2,8 @@ from pyzabbix import ZabbixSender, ZabbixMetric, ZabbixAPI -from .alerts import Alerter -from .util import elastalert_logger, EAException +from ..alerts import Alerter +from ..util import elastalert_logger, EAException class ZabbixClient(ZabbixAPI): diff --git a/elastalert/loaders.py b/elastalert/loaders.py index bd95e3e7..013be0dd 100644 --- a/elastalert/loaders.py +++ b/elastalert/loaders.py @@ -15,7 +15,8 @@ from . import alerts from . import enhancements from . import ruletypes -from .opsgenie import OpsGenieAlerter +from .alerters.opsgenie import OpsGenieAlerter +from .alerters.zabbix import ZabbixAlerter from .util import dt_to_ts from .util import dt_to_ts_with_format from .util import dt_to_unix @@ -27,7 +28,6 @@ from .util import ts_to_dt_with_format from .util import unix_to_dt from .util import unixms_to_dt -from .zabbix import ZabbixAlerter from .yaml import read_yaml diff --git a/tests/alerts_test.py b/tests/alerts_test.py index 1d16018e..4e22b33c 100644 --- a/tests/alerts_test.py +++ b/tests/alerts_test.py @@ -36,7 +36,8 @@ from elastalert.alerts import SlackAlerter from elastalert.alerts import TelegramAlerter from elastalert.loaders import FileRulesLoader -from elastalert.opsgenie import OpsGenieAlerter +from elastalert.alerters.opsgenie import OpsGenieAlerter +from elastalert.alerters.zabbix import ZabbixAlerter from elastalert.alerts import VictorOpsAlerter from elastalert.util import ts_add from elastalert.util import ts_now @@ -7156,3 +7157,33 @@ def test_thehive_alerter(): del actual_data['sourceRef'] assert expected_data == actual_data + + +def test_zabbix_basic(): + rule = { + 'name': 'Basic Zabbix test', + 'type': 'any', + 'alert_text_type': 'alert_text_only', + 'alert': [], + 'alert_subject': 'Test Zabbix', + 'zbx_host': 'example.com', + 'zbx_key': 'example-key' + } + rules_loader = FileRulesLoader({}) + rules_loader.load_modules(rule) + alert = ZabbixAlerter(rule) + match = { + '@timestamp': '2021-01-01T00:00:00Z', + 'somefield': 'foobarbaz' + } + with mock.patch('pyzabbix.ZabbixSender.send') as mock_zbx_send: + alert.alert([match]) + + zabbix_metrics = { + "host": "example.com", + "key": "example-key", + "value": "1", + "clock": 1609459200 + } + alerter_args = mock_zbx_send.call_args.args + assert vars(alerter_args[0][0]) == zabbix_metrics