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

Add tag support for zabbix_host_events_info module #1258

Merged
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
2 changes: 2 additions & 0 deletions changelogs/fragments/pr_1258.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- zabbix_host_events_info - add tag support
76 changes: 71 additions & 5 deletions plugins/modules/zabbix_host_events_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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://<FQDN>/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
"""


Expand All @@ -233,19 +285,23 @@ 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):
""" Get the last event from triggerid"""
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]

Expand All @@ -263,16 +319,22 @@ 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,
supports_check_mode=True
)

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"

Expand All @@ -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 = []
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
dependencies:
- setup_zabbix
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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'
Loading