| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
# |
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
from __future__ import absolute_import, division, print_function |
| 7 |
__metaclass__ = type |
| 8 |
|
| 9 |
|
| 10 |
DOCUMENTATION = r''' |
| 11 |
--- |
| 12 |
module: zabbix_trigger |
| 13 |
short_description: Create/delete Zabbix triggers |
| 14 |
description: |
| 15 |
- Create triggers if they do not exist. |
| 16 |
- Delete existing triggers if they exist. |
| 17 |
author: |
| 18 |
- "Andrew Lathrop (@aplathrop)" |
| 19 |
requirements: |
| 20 |
- "python >= 2.6" |
| 21 |
|
| 22 |
options: |
| 23 |
state: |
| 24 |
description: |
| 25 |
- Create or delete trigger. |
| 26 |
required: false |
| 27 |
type: str |
| 28 |
default: "present" |
| 29 |
choices: [ "present", "absent" ] |
| 30 |
name: |
| 31 |
description: |
| 32 |
- Name of trigger to create or delete. |
| 33 |
- Overrides "description" in API docs. |
| 34 |
- Cannot be changed. If a trigger's name needs to be changed, it needs to deleted and recreated |
| 35 |
required: true |
| 36 |
type: str |
| 37 |
host_name: |
| 38 |
description: |
| 39 |
- Name of host to add trigger to. |
| 40 |
- Required when I(template_name) is not used. |
| 41 |
- Mutually exclusive with I(template_name). |
| 42 |
required: false |
| 43 |
type: str |
| 44 |
template_name: |
| 45 |
description: |
| 46 |
- Name of template to add trigger to. |
| 47 |
- Required when I(host_name) is not used. |
| 48 |
- Mutually exclusive with I(host_name). |
| 49 |
required: false |
| 50 |
type: str |
| 51 |
desc: |
| 52 |
description: |
| 53 |
- Additional description of the trigger. |
| 54 |
- Overrides "comments" in API docs. |
| 55 |
required: false |
| 56 |
type: str |
| 57 |
aliases: [ "description" ] |
| 58 |
dependencies: |
| 59 |
description: |
| 60 |
- list of triggers that this trigger is dependent on |
| 61 |
required: false |
| 62 |
type: list |
| 63 |
elements: dict |
| 64 |
suboptions: |
| 65 |
name: |
| 66 |
description: |
| 67 |
- Name of dependent trigger. |
| 68 |
required: true |
| 69 |
type: str |
| 70 |
host_name: |
| 71 |
description: |
| 72 |
- Name of host containing dependent trigger. |
| 73 |
- Required when I(template_name) is not used. |
| 74 |
- Mutually exclusive with I(template_name). |
| 75 |
required: false |
| 76 |
type: str |
| 77 |
template_name: |
| 78 |
description: |
| 79 |
- Name of template containing dependent trigger. |
| 80 |
- Required when I(host_name) is not used. |
| 81 |
- Mutually exclusive with I(host_name). |
| 82 |
required: false |
| 83 |
type: str |
| 84 |
params: |
| 85 |
description: |
| 86 |
- Parameters to create/update trigger with. |
| 87 |
- Required if state is "present". |
| 88 |
- Parameters as defined at https://www.zabbix.com/documentation/current/en/manual/api/reference/trigger/object |
| 89 |
- Additionally supported parameters are below. |
| 90 |
required: false |
| 91 |
type: dict |
| 92 |
suboptions: |
| 93 |
severity: |
| 94 |
description: |
| 95 |
- Severity of the trigger. |
| 96 |
- Alias for "priority" in API docs. |
| 97 |
required: false |
| 98 |
type: str |
| 99 |
aliases: [ "priority" ] |
| 100 |
choices: |
| 101 |
- not_classified |
| 102 |
- information |
| 103 |
- warning |
| 104 |
- average |
| 105 |
- high |
| 106 |
- disaster |
| 107 |
status: |
| 108 |
description: |
| 109 |
- Status of the trigger. |
| 110 |
required: false |
| 111 |
type: str |
| 112 |
choices: [ "enabled", "disabled" ] |
| 113 |
enabled: |
| 114 |
description: |
| 115 |
- Status of the trigger. |
| 116 |
- Overrides "status" in API docs. |
| 117 |
required: false |
| 118 |
type: bool |
| 119 |
new_name: |
| 120 |
description: |
| 121 |
- New name for trigger |
| 122 |
required: false |
| 123 |
type: str |
| 124 |
generate_multiple_events: |
| 125 |
description: |
| 126 |
- Whether the trigger can generate multiple problem events. |
| 127 |
- Alias for "type" in API docs. |
| 128 |
required: false |
| 129 |
type: bool |
| 130 |
recovery_mode: |
| 131 |
description: |
| 132 |
- OK event generation mode. |
| 133 |
- Overrides "recovery_mode" in API docs. |
| 134 |
required: false |
| 135 |
type: str |
| 136 |
choices: |
| 137 |
- expression |
| 138 |
- recovery_expression |
| 139 |
- none |
| 140 |
correlation_mode: |
| 141 |
description: |
| 142 |
- OK event closes. |
| 143 |
- Overrides "correlation_mode" in API docs. |
| 144 |
required: false |
| 145 |
type: str |
| 146 |
choices: [ "all", "tag" ] |
| 147 |
manual_close: |
| 148 |
description: |
| 149 |
- Allow manual close. |
| 150 |
- Overrides "manual_close" in API docs. |
| 151 |
required: false |
| 152 |
type: bool |
| 153 |
|
| 154 |
extends_documentation_fragment: |
| 155 |
- community.zabbix.zabbix |
| 156 |
''' |
| 157 |
|
| 158 |
EXAMPLES = r''' |
| 159 |
|
| 160 |
# If you want to use Username and Password to be authenticated by Zabbix Server |
| 161 |
- name: Set credentials to access Zabbix Server API |
| 162 |
ansible.builtin.set_fact: |
| 163 |
ansible_user: Admin |
| 164 |
ansible_httpapi_pass: zabbix |
| 165 |
|
| 166 |
# If you want to use API token to be authenticated by Zabbix Server |
| 167 |
# https://www.zabbix.com/documentation/current/en/manual/web_interface/frontend_sections/administration/general#api-tokens |
| 168 |
- name: Set API token |
| 169 |
ansible.builtin.set_fact: |
| 170 |
ansible_zabbix_auth_key: 8ec0d52432c15c91fcafe9888500cf9a607f44091ab554dbee860f6b44fac895 |
| 171 |
|
| 172 |
# Create ping trigger on example_host |
| 173 |
- name: create ping trigger |
| 174 |
# set task level variables as we change ansible_connection plugin here |
| 175 |
vars: |
| 176 |
ansible_network_os: community.zabbix.zabbix |
| 177 |
ansible_connection: httpapi |
| 178 |
ansible_httpapi_port: 443 |
| 179 |
ansible_httpapi_use_ssl: true |
| 180 |
ansible_httpapi_validate_certs: false |
| 181 |
ansible_zabbix_url_path: 'zabbixeu' # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu |
| 182 |
ansible_host: zabbix-example-fqdn.org |
| 183 |
community.zabbix.zabbix_trigger: |
| 184 |
name: agent_ping |
| 185 |
host_name: example_host |
| 186 |
params: |
| 187 |
severity: high |
| 188 |
expression: 'nodata(/example_host/agent.ping,1m)=1' |
| 189 |
manual_close: True |
| 190 |
enabled: True |
| 191 |
state: present |
| 192 |
|
| 193 |
# Create ping trigger on example_template |
| 194 |
- name: create ping trigger |
| 195 |
# set task level variables as we change ansible_connection plugin here |
| 196 |
vars: |
| 197 |
ansible_network_os: community.zabbix.zabbix |
| 198 |
ansible_connection: httpapi |
| 199 |
ansible_httpapi_port: 443 |
| 200 |
ansible_httpapi_use_ssl: true |
| 201 |
ansible_httpapi_validate_certs: false |
| 202 |
ansible_zabbix_url_path: 'zabbixeu' # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu |
| 203 |
ansible_host: zabbix-example-fqdn.org |
| 204 |
community.zabbix.zabbix_trigger: |
| 205 |
name: agent_ping |
| 206 |
host_name: example_template |
| 207 |
params: |
| 208 |
severity: high |
| 209 |
expression: 'nodata(/example_template/agent.ping,1m)=1' |
| 210 |
manual_close: True |
| 211 |
enabled: True |
| 212 |
state: present |
| 213 |
|
| 214 |
# Add tags to the existing Zabbix trigger |
| 215 |
- name: update ping trigger |
| 216 |
# set task level variables as we change ansible_connection plugin here |
| 217 |
vars: |
| 218 |
ansible_network_os: community.zabbix.zabbix |
| 219 |
ansible_connection: httpapi |
| 220 |
ansible_httpapi_port: 443 |
| 221 |
ansible_httpapi_use_ssl: true |
| 222 |
ansible_httpapi_validate_certs: false |
| 223 |
ansible_zabbix_url_path: 'zabbixeu' # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu |
| 224 |
ansible_host: zabbix-example-fqdn.org |
| 225 |
community.zabbix.zabbix_trigger: |
| 226 |
name: agent_ping |
| 227 |
host_name: example_template |
| 228 |
params: |
| 229 |
severity: high |
| 230 |
expression: 'nodata(/example_template/agent.ping,1m)=1' |
| 231 |
manual_close: True |
| 232 |
enabled: True |
| 233 |
tags: |
| 234 |
- tag: class |
| 235 |
value: application |
| 236 |
state: present |
| 237 |
|
| 238 |
# delete Zabbix trigger |
| 239 |
- name: delete ping trigger |
| 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_trigger: |
| 250 |
name: agent_ping |
| 251 |
host_name: example_template |
| 252 |
state: absent |
| 253 |
|
| 254 |
- name: Rename Zabbix trigger |
| 255 |
# set task level variables as we change ansible_connection plugin here |
| 256 |
vars: |
| 257 |
ansible_network_os: community.zabbix.zabbix |
| 258 |
ansible_connection: httpapi |
| 259 |
ansible_httpapi_port: 443 |
| 260 |
ansible_httpapi_use_ssl: true |
| 261 |
ansible_httpapi_validate_certs: false |
| 262 |
ansible_zabbix_url_path: "zabbixeu" # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu |
| 263 |
ansible_host: zabbix-example-fqdn.org |
| 264 |
community.zabbix.zabbix_trigger: |
| 265 |
name: agent_ping |
| 266 |
template_name: example_template |
| 267 |
params: |
| 268 |
new_name: new_agent_ping |
| 269 |
state: present |
| 270 |
''' |
| 271 |
|
| 272 |
from ansible.module_utils.basic import AnsibleModule |
| 273 |
|
| 274 |
from ansible_collections.community.zabbix.plugins.module_utils.base import ZabbixBase |
| 275 |
import ansible_collections.community.zabbix.plugins.module_utils.helpers as zabbix_utils |
| 276 |
|
| 277 |
|
| 278 |
class Trigger(ZabbixBase): |
| 279 |
|
| 280 |
PRIORITY_TYPES = {'not_classified': 0, |
| 281 |
'information': 1, |
| 282 |
'warning': 2, |
| 283 |
'average': 3, |
| 284 |
'high': 4, |
| 285 |
'disaster': 5} |
| 286 |
|
| 287 |
RECOVERY_MODES = {'expression': 0, |
| 288 |
'recovery_expression': 1, |
| 289 |
'none': 2} |
| 290 |
|
| 291 |
def get_triggers(self, trigger_name, host_name, template_name): |
| 292 |
if host_name is not None: |
| 293 |
host = host_name |
| 294 |
else: |
| 295 |
host = template_name |
| 296 |
triggers = [] |
| 297 |
try: |
| 298 |
triggers = self._zapi.trigger.get({'filter': {'description': trigger_name, 'host': host}, "selectDependencies": "extend", "selectTags": "extend"}) |
| 299 |
except Exception as e: |
| 300 |
self._module.fail_json(msg="Failed to get trigger: %s" % e) |
| 301 |
return triggers |
| 302 |
|
| 303 |
def sanitize_params(self, name, params, desc=None, dependencies=None): |
| 304 |
params['description'] = name |
| 305 |
if desc is not None: |
| 306 |
params['comments'] = desc |
| 307 |
if 'severity' in params: |
| 308 |
params['priority'] = params['severity'] |
| 309 |
params.pop("severity") |
| 310 |
if 'priority' in params: |
| 311 |
priority_id = self.PRIORITY_TYPES[params['priority']] |
| 312 |
params['priority'] = priority_id |
| 313 |
if 'enabled' in params: |
| 314 |
if params['enabled']: |
| 315 |
params['status'] = 'enabled' |
| 316 |
else: |
| 317 |
params['status'] = 'disabled' |
| 318 |
params.pop("enabled") |
| 319 |
if 'status' in params: |
| 320 |
status = params['status'] |
| 321 |
if status == 'enabled': |
| 322 |
params['status'] = 0 |
| 323 |
elif status == 'disabled': |
| 324 |
params['status'] = 1 |
| 325 |
else: |
| 326 |
self._module.fail_json(msg="Status must be 'enabled' or 'disabled', got %s" % status) |
| 327 |
if 'generate_multiple_events' in params: |
| 328 |
multiple_event_type = params['generate_multiple_events'] |
| 329 |
if multiple_event_type: |
| 330 |
params['type'] = 1 |
| 331 |
else: |
| 332 |
params['type'] = 0 |
| 333 |
del params['generate_multiple_events'] |
| 334 |
if 'recovery_mode' in params: |
| 335 |
recovery_mode_id = self.RECOVERY_MODES[params['recovery_mode']] |
| 336 |
params['recovery_mode'] = recovery_mode_id |
| 337 |
if 'correlation_mode' in params: |
| 338 |
correlation_mode = params['correlation_mode'] |
| 339 |
if correlation_mode == 'all': |
| 340 |
params['correlation_mode'] = 0 |
| 341 |
elif correlation_mode == 'tag': |
| 342 |
params['correlation_mode'] = 1 |
| 343 |
else: |
| 344 |
self._module.fail_json(msg="correlation_mode must be all or tag, got %s" % correlation_mode) |
| 345 |
if 'manual_close' in params: |
| 346 |
manual_close = params['manual_close'] |
| 347 |
if manual_close: |
| 348 |
params['manual_close'] = 1 |
| 349 |
else: |
| 350 |
params['manual_close'] = 0 |
| 351 |
if dependencies is not None: |
| 352 |
params['dependencies'] = [] |
| 353 |
for dependency in dependencies: |
| 354 |
host_name = None |
| 355 |
template_name = None |
| 356 |
if 'host_name' in dependency: |
| 357 |
host_name = dependency |
| 358 |
elif 'template_name' in dependency: |
| 359 |
template_name = dependency |
| 360 |
else: |
| 361 |
self._module.fail_json(msg="Each dependency must contain either the host_name or the template_name") |
| 362 |
triggers = self.get_triggers(dependency['name'], host_name, template_name) |
| 363 |
for trigger in triggers: |
| 364 |
params['dependencies'].append({'triggerid': trigger['triggerid']}) |
| 365 |
|
| 366 |
def add_trigger(self, params): |
| 367 |
if self._module.check_mode: |
| 368 |
self._module.exit_json(changed=True) |
| 369 |
try: |
| 370 |
results = self._zapi.trigger.create(params) |
| 371 |
except Exception as e: |
| 372 |
self._module.fail_json(msg="Failed to create trigger: %s" % e) |
| 373 |
return results |
| 374 |
|
| 375 |
def update_trigger(self, params): |
| 376 |
if self._module.check_mode: |
| 377 |
self._module.exit_json(changed=True) |
| 378 |
try: |
| 379 |
results = self._zapi.trigger.update(params) |
| 380 |
except Exception as e: |
| 381 |
self._module.fail_json(msg="Failed to update trigger: %s" % e) |
| 382 |
return results |
| 383 |
|
| 384 |
def check_trigger_changed(self, old_trigger): |
| 385 |
try: |
| 386 |
new_trigger = self._zapi.trigger.get({"triggerids": "%s" % old_trigger['triggerid'], "selectDependencies": "extend", "selectTags": "extend"})[0] |
| 387 |
except Exception as e: |
| 388 |
self._module.fail_json(msg="Failed to get trigger: %s" % e) |
| 389 |
return old_trigger != new_trigger |
| 390 |
|
| 391 |
def delete_trigger(self, trigger_id): |
| 392 |
if self._module.check_mode: |
| 393 |
self._module.exit_json(changed=True) |
| 394 |
try: |
| 395 |
results = self._zapi.trigger.delete(trigger_id) |
| 396 |
except Exception as e: |
| 397 |
self._module.fail_json(msg="Failed to delete trigger: %s" % e) |
| 398 |
return results |
| 399 |
|
| 400 |
|
| 401 |
def main(): |
| 402 |
argument_spec = zabbix_utils.zabbix_common_argument_spec() |
| 403 |
argument_spec.update(dict( |
| 404 |
name=dict(type='str', required=True), |
| 405 |
host_name=dict(type='str', required=False), |
| 406 |
template_name=dict(type='str', required=False), |
| 407 |
params=dict(type='dict', required=False), |
| 408 |
desc=dict(type='str', required=False, aliases=['description']), |
| 409 |
dependencies=dict(type='list', elements='dict', required=False), |
| 410 |
state=dict(type='str', default="present", choices=['present', 'absent']), |
| 411 |
)) |
| 412 |
module = AnsibleModule( |
| 413 |
argument_spec=argument_spec, |
| 414 |
required_one_of=[ |
| 415 |
['host_name', 'template_name'] |
| 416 |
], |
| 417 |
mutually_exclusive=[ |
| 418 |
['host_name', 'template_name'] |
| 419 |
], |
| 420 |
required_if=[ |
| 421 |
['state', 'present', ['params']] |
| 422 |
], |
| 423 |
supports_check_mode=True |
| 424 |
) |
| 425 |
|
| 426 |
name = module.params['name'] |
| 427 |
host_name = module.params['host_name'] |
| 428 |
template_name = module.params['template_name'] |
| 429 |
params = module.params['params'] |
| 430 |
desc = module.params['desc'] |
| 431 |
dependencies = module.params['dependencies'] |
| 432 |
state = module.params['state'] |
| 433 |
|
| 434 |
trigger = Trigger(module) |
| 435 |
|
| 436 |
if state == "absent": |
| 437 |
triggers = trigger.get_triggers(name, host_name, template_name) |
| 438 |
if len(triggers) == 0: |
| 439 |
module.exit_json(changed=False, result="No trigger to delete.") |
| 440 |
else: |
| 441 |
delete_ids = [] |
| 442 |
for t in triggers: |
| 443 |
delete_ids.append(t['triggerid']) |
| 444 |
results = trigger.delete_trigger(delete_ids) |
| 445 |
module.exit_json(changed=True, result=results) |
| 446 |
|
| 447 |
elif state == "present": |
| 448 |
trigger.sanitize_params(name, params, desc, dependencies) |
| 449 |
triggers = trigger.get_triggers(name, host_name, template_name) |
| 450 |
if 'new_name' in params: |
| 451 |
new_name_trigger = trigger.get_triggers(params['new_name'], host_name, template_name) |
| 452 |
if len(new_name_trigger) > 0: |
| 453 |
module.exit_json(changed=False, result=[{'triggerids': [new_name_trigger[0]['triggerid']]}]) |
| 454 |
if len(triggers) == 0: |
| 455 |
if 'new_name' in params: |
| 456 |
module.fail_json('Cannot rename trigger: %s is not found' % name) |
| 457 |
results = trigger.add_trigger(params) |
| 458 |
module.exit_json(changed=True, result=results) |
| 459 |
else: |
| 460 |
results = [] |
| 461 |
changed = False |
| 462 |
for t in triggers: |
| 463 |
params['triggerid'] = t['triggerid'] |
| 464 |
params.pop('description') |
| 465 |
if 'new_name' in params: |
| 466 |
params['description'] = params['new_name'] |
| 467 |
params.pop("new_name") |
| 468 |
results.append(trigger.update_trigger(params)) |
| 469 |
changed_trigger = trigger.check_trigger_changed(t) |
| 470 |
if changed_trigger: |
| 471 |
changed = True |
| 472 |
module.exit_json(changed=changed, result=results) |
| 473 |
|
| 474 |
|
| 475 |
if __name__ == '__main__': |
| 476 |
main() |