Skip to content

Commit

Permalink
Merge pull request #227 from nsano-rururu/bugfix_zabbix
Browse files Browse the repository at this point in the history
Fix zabbix alerter
  • Loading branch information
jertel authored Jun 4, 2021
2 parents 1b77f8d + 4a5199a commit a55311a
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 4 deletions.
4 changes: 2 additions & 2 deletions docs/source/ruletypes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2768,9 +2768,9 @@ Zabbix will send notification to a Zabbix server. The item in the host specified

Required:

``zbx_sender_host``: The address where zabbix server is running.
``zbx_sender_host``: The address where zabbix server is running, defaults to ``'localhost'``.

``zbx_sender_port``: The port where zabbix server is listenning.
``zbx_sender_port``: The port where zabbix server is listenning, defaults to ``10051``.

``zbx_host``: This field setup the host in zabbix that receives the value sent by ElastAlert 2.

Expand Down
4 changes: 2 additions & 2 deletions elastalert/alerters/zabbix.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ def __init__(self, *args):

self.zbx_sender_host = self.rule.get('zbx_sender_host', 'localhost')
self.zbx_sender_port = self.rule.get('zbx_sender_port', 10051)
self.zbx_host = self.rule.get('zbx_host')
self.zbx_key = self.rule.get('zbx_key')
self.zbx_host = self.rule['zbx_host']
self.zbx_key = self.rule['zbx_key']
self.timestamp_field = self.rule.get('timestamp_field', '@timestamp')
self.timestamp_type = self.rule.get('timestamp_type', 'iso')
self.timestamp_strptime = self.rule.get('timestamp_strptime', '%Y-%m-%dT%H:%M:%S.%fZ')
Expand Down
57 changes: 57 additions & 0 deletions tests/alerters/zabbix_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import pytest
import mock

from elastalert.alerters.zabbix import ZabbixAlerter
Expand Down Expand Up @@ -32,3 +33,59 @@ def test_zabbix_basic():
}
alerter_args = mock_zbx_send.call_args.args
assert vars(alerter_args[0][0]) == zabbix_metrics


def test_zabbix_getinfo():
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)

expected_data = {
'type': 'zabbix Alerter'
}
actual_data = alert.get_info()
assert expected_data == actual_data


@pytest.mark.parametrize('zbx_host, zbx_key, expected_data', [
('', '', True),
('example.com', '', True),
('', 'example-key', True),
('example.com', 'example-key',
{
'type': 'zabbix Alerter'
})
])
def test_zabbix_key_error(zbx_host, zbx_key, expected_data):
try:
rule = {
'name': 'Basic Zabbix test',
'type': 'any',
'alert_text_type': 'alert_text_only',
'alert': [],
'alert_subject': 'Test Zabbix'
}

if zbx_host != '':
rule['zbx_host'] = zbx_host

if zbx_key != '':
rule['zbx_key'] = zbx_key

rules_loader = FileRulesLoader({})
rules_loader.load_modules(rule)
alert = ZabbixAlerter(rule)

actual_data = alert.get_info()
assert expected_data == actual_data
except KeyError:
assert expected_data

0 comments on commit a55311a

Please sign in to comment.