From 9712f898a82adf666f1aef2687b7aa09a16ef36e Mon Sep 17 00:00:00 2001 From: aplathrop Date: Fri, 31 May 2024 17:37:35 -0400 Subject: [PATCH] Add tag support for zabbix_host_events_info module (#1258) --- changelogs/fragments/pr_1258.yml | 2 + plugins/modules/zabbix_host_events_info.py | 76 +++++++++++++++++-- .../meta/main.yml | 3 + .../tasks/main.yml | 13 ++++ .../tasks/zabbix_setup.yml | 63 +++++++++++++++ .../tasks/zabbix_teardown.yml | 9 +++ .../tasks/zabbix_tests.yml | 42 ++++++++++ 7 files changed, 203 insertions(+), 5 deletions(-) create mode 100644 changelogs/fragments/pr_1258.yml create mode 100644 tests/integration/targets/test_zabbix_host_events_info/meta/main.yml create mode 100644 tests/integration/targets/test_zabbix_host_events_info/tasks/main.yml create mode 100644 tests/integration/targets/test_zabbix_host_events_info/tasks/zabbix_setup.yml create mode 100644 tests/integration/targets/test_zabbix_host_events_info/tasks/zabbix_teardown.yml create mode 100644 tests/integration/targets/test_zabbix_host_events_info/tasks/zabbix_tests.yml diff --git a/changelogs/fragments/pr_1258.yml b/changelogs/fragments/pr_1258.yml new file mode 100644 index 000000000..c8b6abf82 --- /dev/null +++ b/changelogs/fragments/pr_1258.yml @@ -0,0 +1,2 @@ +minor_changes: + - zabbix_host_events_info - add tag support diff --git a/plugins/modules/zabbix_host_events_info.py b/plugins/modules/zabbix_host_events_info.py index fde86bc12..c957e68de 100644 --- a/plugins/modules/zabbix_host_events_info.py +++ b/plugins/modules/zabbix_host_events_info.py @@ -104,6 +104,9 @@ eventid: description: ID of the event type: int + tags: + description: List of tags + type: list value: description: State of the related object type: int @@ -177,6 +180,36 @@ - high - disaster type: str + tags: + description: + - list of tags to filter by + required: false + type: list + elements: dict + suboptions: + tag: + description: + - the tag name + required: true + type: str + value: + description: + - the tag value + required: true + type: str + operator: + description: + - how to match tags + required: true + type: str + choices: + - like + - equal + - not_like + - not_equal + - exists + - not_exists + extends_documentation_fragment: - community.zabbix.zabbix @@ -213,6 +246,25 @@ - fail: msg: "machine alert in zabbix" when: zbx_host["triggers_problem"]|length > 0 + + +- name: filter events for host based on tag + # set task level variables as we change ansible_connection plugin here + vars: + ansible_network_os: community.zabbix.zabbix + ansible_connection: httpapi + ansible_httpapi_port: 443 + ansible_httpapi_use_ssl: true + ansible_httpapi_validate_certs: false + ansible_zabbix_url_path: "zabbixeu" # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http:///zabbixeu + ansible_host: zabbix-example-fqdn.org + community.zabbix.zabbix_host_events_info: + host_identifier: "{{inventory_hostname}}" + host_id_type: "hostname" + tags: + - tag: ExampleTag + value: ExampleValue + operator: equal """ @@ -233,11 +285,15 @@ def get_host(self, host_identifier, host_inventory, search_key): else: return host[0] - def get_triggers_by_host_id_in_problem_state(self, host_id, trigger_severity): + def get_triggers_by_host_id_in_problem_state(self, host_id, trigger_severity, tags=None): """ Get triggers in problem state from a hostid""" output = "extend" - triggers_list = self._zapi.trigger.get({"output": output, "hostids": host_id, - "min_severity": trigger_severity}) + if tags: + triggers_list = self._zapi.trigger.get({"output": output, "hostids": host_id, + "min_severity": trigger_severity, "tags": tags}) + else: + triggers_list = self._zapi.trigger.get({"output": output, "hostids": host_id, + "min_severity": trigger_severity}) return triggers_list def get_last_event_by_trigger_id(self, triggers_id): @@ -245,7 +301,7 @@ def get_last_event_by_trigger_id(self, triggers_id): output = ["eventid", "clock", "acknowledged", "value"] select_acknowledges = ["clock", "alias", "message"] event = self._zapi.event.get({"output": output, "objectids": triggers_id, - "select_acknowledges": select_acknowledges, "limit": 1, "sortfield": "clock", + "select_acknowledges": select_acknowledges, "selectTags": "extend", "limit": 1, "sortfield": "clock", "sortorder": "DESC"}) return event[0] @@ -263,6 +319,10 @@ def main(): required=False, default="average", choices=["not_classified", "information", "warning", "average", "high", "disaster"]), + tags=dict( + type="list", + required=False, + elements="dict"), )) module = AnsibleModule( argument_spec=argument_spec, @@ -270,9 +330,11 @@ def main(): ) trigger_severity_map = {"not_classified": 0, "information": 1, "warning": 2, "average": 3, "high": 4, "disaster": 5} + tags_operator_map = {"like": 0, "equal": 1, "not_like": 2, "not_equal": 3, "exists": 4, "not_exists": 5} host_id = module.params["host_identifier"] host_id_type = module.params["host_id_type"] trigger_severity = trigger_severity_map[module.params["trigger_severity"]] + tags = module.params["tags"] host_inventory = "hostid" @@ -290,7 +352,11 @@ def main(): # check hostid exist zabbix_host = host.get_host(host_id, host_inventory, "hostid") - triggers = host.get_triggers_by_host_id_in_problem_state(host_id, trigger_severity) + if tags: + for tag in tags: + tag["operator"] = tags_operator_map[tag["operator"]] + + triggers = host.get_triggers_by_host_id_in_problem_state(host_id, trigger_severity, tags) triggers_ok = [] triggers_problem = [] diff --git a/tests/integration/targets/test_zabbix_host_events_info/meta/main.yml b/tests/integration/targets/test_zabbix_host_events_info/meta/main.yml new file mode 100644 index 000000000..acdb704c8 --- /dev/null +++ b/tests/integration/targets/test_zabbix_host_events_info/meta/main.yml @@ -0,0 +1,3 @@ +--- +dependencies: + - setup_zabbix diff --git a/tests/integration/targets/test_zabbix_host_events_info/tasks/main.yml b/tests/integration/targets/test_zabbix_host_events_info/tasks/main.yml new file mode 100644 index 000000000..131e87ac9 --- /dev/null +++ b/tests/integration/targets/test_zabbix_host_events_info/tasks/main.yml @@ -0,0 +1,13 @@ +--- +- name: test - do not run tests for Zabbix < 6.4 + meta: end_play + when: zabbix_version is version('6.4', '<') + +# setup stuff +- include_tasks: zabbix_setup.yml + +# zabbix_item module tests +- include_tasks: zabbix_tests.yml + +# tear down stuff set up earlier +- include_tasks: zabbix_teardown.yml diff --git a/tests/integration/targets/test_zabbix_host_events_info/tasks/zabbix_setup.yml b/tests/integration/targets/test_zabbix_host_events_info/tasks/zabbix_setup.yml new file mode 100644 index 000000000..f7969a74a --- /dev/null +++ b/tests/integration/targets/test_zabbix_host_events_info/tasks/zabbix_setup.yml @@ -0,0 +1,63 @@ +--- + +- name: Create example template + community.zabbix.zabbix_template: + template_name: ExampleTemplate + template_groups: + - Templates + +- name: Create example host + community.zabbix.zabbix_host: + host_name: ExampleHost + host_groups: + - Linux servers + - Zabbix servers + link_templates: + - ExampleTemplate + status: enabled + state: present + interfaces: + - type: 1 + main: 1 + useip: 1 + ip: 10.1.1.1 + dns: "" + port: "10050" + +- name: create ping item + community.zabbix.zabbix_item: + name: ping + template_name: ExampleTemplate + params: + type: zabbix_agent_active + key: agent.ping + value_type: numeric_unsigned + interval: 20s + state: present + +- name: create ping warning trigger + community.zabbix.zabbix_trigger: + name: ping-warning + template_name: ExampleTemplate + params: + severity: warning + expression: 'nodata(/ExampleTemplate/agent.ping,1m)=1' + manual_close: True + state: present + +- name: create ping high trigger + community.zabbix.zabbix_trigger: + name: ping-high + template_name: ExampleTemplate + params: + severity: high + expression: 'nodata(/ExampleTemplate/agent.ping,2m)=1' + manual_close: True + tags: + - tag: ExampleTag + value: ExampleValue + state: present + +- name: Wait to ensure triggers are firing + ansible.builtin.wait_for: + timeout: 180 diff --git a/tests/integration/targets/test_zabbix_host_events_info/tasks/zabbix_teardown.yml b/tests/integration/targets/test_zabbix_host_events_info/tasks/zabbix_teardown.yml new file mode 100644 index 000000000..2df4dd571 --- /dev/null +++ b/tests/integration/targets/test_zabbix_host_events_info/tasks/zabbix_teardown.yml @@ -0,0 +1,9 @@ +- name: remove example host + community.zabbix.zabbix_host: + host_name: ExampleHost + state: absent + +- name: remove example template + community.zabbix.zabbix_template: + template_name: ExampleTemplate + state: absent diff --git a/tests/integration/targets/test_zabbix_host_events_info/tasks/zabbix_tests.yml b/tests/integration/targets/test_zabbix_host_events_info/tasks/zabbix_tests.yml new file mode 100644 index 000000000..3e72edbe6 --- /dev/null +++ b/tests/integration/targets/test_zabbix_host_events_info/tasks/zabbix_tests.yml @@ -0,0 +1,42 @@ +--- + +- name: test - get warning and higher events for host + community.zabbix.zabbix_host_events_info: + host_identifier: ExampleHost + host_id_type: hostname + trigger_severity: warning + register: zabbix_host_events_warning + +- name: assert that both events are listed as problem + ansible.builtin.assert: + that: zabbix_host_events_warning.triggers_problem | length == 2 + +- name: test - get events high and higher for host + community.zabbix.zabbix_host_events_info: + host_identifier: ExampleHost + host_id_type: hostname + trigger_severity: high + register: zabbix_host_events_high + +- name: assert that only high event is listed as problem + ansible.builtin.assert: + that: zabbix_host_events_high.triggers_problem | length == 1 + +- name: test - get events with tag for host + community.zabbix.zabbix_host_events_info: + host_identifier: ExampleHost + host_id_type: hostname + trigger_severity: warning + tags: + - tag: ExampleTag + value: ExampleValue + operator: equal + register: zabbix_host_events_tags + +- name: assert that only matching tag is listed as problem + ansible.builtin.assert: + that: zabbix_host_events_tags.triggers_problem | length == 1 + +- name: assert tags are returned + ansible.builtin.assert: + that: zabbix_host_events_tags.triggers_problem[0].last_event.tags[0].tag == 'ExampleTag'