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