Debian Codegraph @

ansible_collections/community/zabbix/plugins/modules/zabbix_group_events_info.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # built by Martin Eiswirth (@meis4h) on top of the work by Stéphane Travassac (@stravassac) on zabbix_host_events_info.py
5 # and Michael Miko (@RedWhiteMiko) on zabbix_group_info.py
6 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
7
8
9 from __future__ import (absolute_import, division, print_function)
10
11 __metaclass__ = type
12
13 RETURN = """
14 ---
15 triggers_ok:
16 description: Zabbix Triggers in OK state
17 returned: On success
18 type: complex
19 contains:
20 comments:
21 description: Additional description of the trigger
22 type: str
23 description:
24 description: Name of the trigger
25 type: str
26 error:
27 description: Error text if there have been any problems when updating the state of the trigger
28 type: str
29 expression:
30 description: Reduced trigger expression
31 type: str
32 flags:
33 description: Origin of the trigger
34 type: int
35 lastchange:
36 description: Time when the trigger last changed its state (timestamp)
37 type: int
38 priority:
39 description: Severity of the trigger
40 type: int
41 state:
42 description: State of the trigger
43 type: int
44 status:
45 description: Whether the trigger is enabled or disabled
46 type: int
47 templateid:
48 description: ID of the parent template trigger
49 type: int
50 triggerid:
51 description: ID of the trigger
52 type: int
53 type:
54 description: Whether the trigger can generate multiple problem events
55 type: int
56 url:
57 description: URL associated with the trigger
58 type: str
59 value:
60 description: Whether the trigger is in OK or problem state
61 type: int
62 triggers_problem:
63 description: Zabbix Triggers in problem state. See trigger and event objects in API documentation of your zabbix version for more
64 returned: On success
65 type: complex
66 contains:
67 comments:
68 description: Additional description of the trigger
69 type: str
70 description:
71 description: Name of the trigger
72 type: str
73 error:
74 description: Error text if there have been any problems when updating the state of the trigger
75 type: str
76 expression:
77 description: Reduced trigger expression
78 type: str
79 flags:
80 description: Origin of the trigger
81 type: int
82 last_event:
83 description: last event informations
84 type: complex
85 contains:
86 acknowledged:
87 description: If set to true return only acknowledged events
88 type: int
89 acknowledges:
90 description: acknowledges informations
91 type: complex
92 contains:
93 username:
94 description: Account who acknowledge
95 type: str
96 clock:
97 description: Time when the event was created (timestamp)
98 type: int
99 message:
100 description: Text of the acknowledgement message
101 type: str
102 clock:
103 description: Time when the event was created (timestamp)
104 type: int
105 eventid:
106 description: ID of the event
107 type: int
108 tags:
109 description: List of tags
110 type: list
111 value:
112 description: State of the related object
113 type: int
114 lastchange:
115 description: Time when the trigger last changed its state (timestamp)
116 type: int
117 priority:
118 description: Severity of the trigger
119 type: int
120 state:
121 description: State of the trigger
122 type: int
123 status:
124 description: Whether the trigger is enabled or disabled
125 type: int
126 templateid:
127 description: ID of the parent template trigger
128 type: int
129 triggerid:
130 description: ID of the trigger
131 type: int
132 type:
133 description: Whether the trigger can generate multiple problem events
134 type: int
135 url:
136 description: URL associated with the trigger
137 type: str
138 value:
139 description: Whether the trigger is in OK or problem state
140 type: int
141 """
142
143 DOCUMENTATION = """
144 ---
145 module: zabbix_group_events_info
146 short_description: Get all triggers about a Zabbix group
147 description:
148 - This module allows you to check the state of triggers of all hosts in a Zabbix hostgroup.
149 author:
150 - "Martin Eiswirth (@meis4h)"
151 requirements:
152 - "python >= 3.9"
153 options:
154 hostgroup_name:
155 description:
156 - Name of the hostgroup in Zabbix.
157 required: true
158 type: list
159 elements: str
160 trigger_severity:
161 description:
162 - Zabbix severity for search filter
163 default: average
164 required: false
165 choices:
166 - not_classified
167 - information
168 - warning
169 - average
170 - high
171 - disaster
172 type: str
173 tags:
174 description:
175 - list of tags to filter by
176 required: false
177 type: list
178 elements: dict
179 suboptions:
180 tag:
181 description:
182 - the tag name
183 required: true
184 type: str
185 value:
186 description:
187 - the tag value
188 required: true
189 type: str
190 operator:
191 description:
192 - how to match tags
193 required: true
194 type: str
195 choices:
196 - like
197 - equal
198 - not_like
199 - not_equal
200 - exists
201 - not_exists
202
203 extends_documentation_fragment:
204 - community.zabbix.zabbix
205
206 """
207
208 EXAMPLES = """
209 # If you want to use Username and Password to be authenticated by Zabbix Server
210 - name: Set credentials to access Zabbix Server API
211 set_fact:
212 ansible_user: Admin
213 ansible_httpapi_pass: zabbix
214
215 # If you want to use API token to be authenticated by Zabbix Server
216 # https://www.zabbix.com/documentation/current/en/manual/web_interface/frontend_sections/administration/general#api-tokens
217 - name: Set API token
218 set_fact:
219 ansible_zabbix_auth_key: 8ec0d52432c15c91fcafe9888500cf9a607f44091ab554dbee860f6b44fac895
220
221 - name: Fail if alert active in hostgroup
222 # set task level variables as we change ansible_connection plugin here
223 vars:
224 ansible_network_os: community.zabbix.zabbix
225 ansible_connection: httpapi
226 ansible_httpapi_port: 443
227 ansible_httpapi_use_ssl: true
228 ansible_httpapi_validate_certs: false
229 ansible_zabbix_url_path: "zabbixeu" # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu
230 ansible_host: zabbix-example-fqdn.org
231 community.zabbix.zabbix_group_events_info:
232 hostgroup_name: "{{ inventory_hostname }}"
233 register: zbx_hostgroup
234 delegate_to: localhost
235 - fail:
236 msg: "Active alert in zabbix"
237 when: zbx_hostgroup["triggers_problem"] | length > 0
238
239 - name: filter events for host based on tag
240 # set task level variables as we change ansible_connection plugin here
241 vars:
242 ansible_network_os: community.zabbix.zabbix
243 ansible_connection: httpapi
244 ansible_httpapi_port: 443
245 ansible_httpapi_use_ssl: true
246 ansible_httpapi_validate_certs: false
247 ansible_zabbix_url_path: "zabbixeu" # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu
248 ansible_host: zabbix-example-fqdn.org
249 community.zabbix.zabbix_group_events_info:
250 hostgroup_name: "{{ inventory_hostname }}"
251 tags:
252 - tag: ExampleTag
253 value: ExampleValue
254 operator: equal
255 """
256
257
258 from ansible.module_utils.basic import AnsibleModule
259
260 from ansible_collections.community.zabbix.plugins.module_utils.base import ZabbixBase
261
262 from ansible.module_utils.compat.version import LooseVersion
263
264 import ansible_collections.community.zabbix.plugins.module_utils.helpers as zabbix_utils
265
266
267 class Host(ZabbixBase):
268 def get_group_ids_by_group_names(self, group_names):
269 group_list = self._zapi.hostgroup.get({"output": "extend", "filter": {"name": group_names}})
270 if len(group_list) < 1:
271 self._module.fail_json(msg="Hostgroup not found: %s" % group_names)
272 return group_list
273
274 def get_triggers_by_group_id_in_problem_state(self, group_id, trigger_severity, tags=None):
275 """ Get triggers in problem state from a groupid"""
276 output = "extend"
277 if tags:
278 triggers_list = self._zapi.trigger.get({"output": output, "groupids": group_id,
279 "min_severity": trigger_severity, "tags": tags})
280 else:
281 triggers_list = self._zapi.trigger.get({"output": output, "groupids": group_id,
282 "min_severity": trigger_severity})
283 return triggers_list
284
285 def get_last_event_by_trigger_id(self, triggers_id):
286 """ Get the last event from triggerid"""
287 output = ["eventid", "clock", "acknowledged", "value"]
288 if LooseVersion(self._zbx_api_version) < LooseVersion("7.0"):
289 event = self._zapi.event.get({"output": output, "objectids": triggers_id,
290 "select_acknowledges": "extend", "selectTags": "extend", "limit": 1, "sortfield": "clock",
291 "sortorder": "DESC"})
292 else:
293 event = self._zapi.event.get({"output": output, "objectids": triggers_id,
294 "selectAcknowledges": "extend", "selectTags": "extend", "limit": 1, "sortfield": "clock",
295 "sortorder": "DESC"})
296 return event[0]
297
298
299 def main():
300 argument_spec = zabbix_utils.zabbix_common_argument_spec()
301 argument_spec.update(dict(
302 hostgroup_name=dict(type="list", required=True, elements="str"),
303 trigger_severity=dict(
304 type="str",
305 required=False,
306 default="average",
307 choices=["not_classified", "information", "warning", "average", "high", "disaster"]),
308 tags=dict(
309 type="list",
310 required=False,
311 elements="dict"),
312 ))
313 module = AnsibleModule(
314 argument_spec=argument_spec,
315 supports_check_mode=True
316 )
317
318 trigger_severity_map = {"not_classified": 0, "information": 1, "warning": 2, "average": 3, "high": 4, "disaster": 5}
319 tags_operator_map = {"like": 0, "equal": 1, "not_like": 2, "not_equal": 3, "exists": 4, "not_exists": 5}
320 trigger_severity = trigger_severity_map[module.params["trigger_severity"]]
321 tags = module.params["tags"]
322
323 hostgroup_name = module.params["hostgroup_name"]
324
325 host = Host(module)
326 host_groups = host.get_group_ids_by_group_names(hostgroup_name)
327 triggers = []
328
329 for host_group in host_groups:
330 host_group_id = host_group["groupid"]
331 if tags:
332 for tag in tags:
333 tag["operator"] = tags_operator_map[tag["operator"]]
334 host_group_triggers = host.get_triggers_by_group_id_in_problem_state(host_group_id, trigger_severity, tags)
335 triggers = triggers + host_group_triggers
336
337 triggers_ok = []
338 triggers_problem = []
339 for trigger in triggers:
340 # tGet last event for trigger with problem value = 1
341 # https://www.zabbix.com/documentation/3.4/manual/api/reference/trigger/object
342 if int(trigger["value"]) == 1:
343 event = host.get_last_event_by_trigger_id(trigger["triggerid"])
344 trigger["last_event"] = event
345 triggers_problem.append(trigger)
346 else:
347 triggers_ok.append(trigger)
348
349 module.exit_json(ok=True, host_groups=host_groups, triggers_ok=triggers_ok, triggers_problem=triggers_problem)
350
351
352 if __name__ == "__main__":
353 main()