| 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 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
DOCUMENTATION = """ |
| 11 |
--- |
| 12 |
module: zabbix_action |
| 13 |
|
| 14 |
short_description: Create/Delete/Update Zabbix actions |
| 15 |
|
| 16 |
|
| 17 |
description: |
| 18 |
- This module allows you to create, modify and delete Zabbix actions. |
| 19 |
|
| 20 |
author: |
| 21 |
- Ruben Tsirunyan (@rubentsirunyan) |
| 22 |
- Ruben Harutyunov (@K-DOT) |
| 23 |
|
| 24 |
requirements: |
| 25 |
- "python >= 3.9" |
| 26 |
|
| 27 |
options: |
| 28 |
name: |
| 29 |
type: str |
| 30 |
description: |
| 31 |
- Name of the action |
| 32 |
required: true |
| 33 |
event_source: |
| 34 |
type: str |
| 35 |
description: |
| 36 |
- Type of events that the action will handle. |
| 37 |
- Required when C(state=present). |
| 38 |
required: false |
| 39 |
choices: ["trigger", "discovery", "auto_registration", "internal"] |
| 40 |
state: |
| 41 |
type: str |
| 42 |
description: |
| 43 |
- State of the action. |
| 44 |
- On C(present), it will create an action if it does not exist or update the action if the associated data is different. |
| 45 |
- On C(absent), it will remove the action if it exists. |
| 46 |
choices: ["present", "absent"] |
| 47 |
default: "present" |
| 48 |
status: |
| 49 |
type: str |
| 50 |
description: |
| 51 |
- Status of the action. |
| 52 |
choices: ["enabled", "disabled"] |
| 53 |
default: "enabled" |
| 54 |
pause_in_maintenance: |
| 55 |
description: |
| 56 |
- Whether to pause escalation during maintenance periods or not. |
| 57 |
- Can be used when I(event_source=trigger). |
| 58 |
type: "bool" |
| 59 |
default: true |
| 60 |
notify_if_canceled: |
| 61 |
description: |
| 62 |
- Weather to notify when escalation is canceled. |
| 63 |
- Can be used when I(event_source=trigger). |
| 64 |
type: "bool" |
| 65 |
default: true |
| 66 |
esc_period: |
| 67 |
type: str |
| 68 |
description: |
| 69 |
- Default operation step duration. Must be greater than 60 seconds. |
| 70 |
- Accepts seconds, time unit with suffix and user macro since => Zabbix 3.4 |
| 71 |
- Required when C(state=present). |
| 72 |
required: false |
| 73 |
conditions: |
| 74 |
type: list |
| 75 |
elements: dict |
| 76 |
description: |
| 77 |
- List of conditions to use for filtering results. |
| 78 |
- For more information about suboptions of this option please |
| 79 |
check out Zabbix API documentation U(https://www.zabbix.com/documentation/5.0/manual/api/reference/action/object#action_filter_condition) |
| 80 |
default: [] |
| 81 |
suboptions: |
| 82 |
type: |
| 83 |
type: str |
| 84 |
description: |
| 85 |
- Type (label) of the condition. |
| 86 |
- "Possible values when I(event_source=trigger):" |
| 87 |
- " - C(host_group)" |
| 88 |
- " - C(host)" |
| 89 |
- " - C(trigger)" |
| 90 |
- " - C(trigger_name)" |
| 91 |
- " - C(trigger_severity)" |
| 92 |
- " - C(time_period)" |
| 93 |
- " - C(host_template)" |
| 94 |
- " - C(maintenance_status) known in Zabbix 4.0 and above as 'Problem is suppressed'" |
| 95 |
- " - C(event_tag)" |
| 96 |
- " - C(event_tag_value)" |
| 97 |
- "Possible values when I(event_source=discovery):" |
| 98 |
- " - C(host_IP)" |
| 99 |
- " - C(discovered_service_type)" |
| 100 |
- " - C(discovered_service_port)" |
| 101 |
- " - C(discovery_status)" |
| 102 |
- " - C(uptime_or_downtime_duration)" |
| 103 |
- " - C(received_value)" |
| 104 |
- " - C(discovery_rule)" |
| 105 |
- " - C(discovery_check)" |
| 106 |
- " - C(proxy)" |
| 107 |
- " - C(discovery_object)" |
| 108 |
- "Possible values when I(event_source=auto_registration):" |
| 109 |
- " - C(proxy)" |
| 110 |
- " - C(host_name)" |
| 111 |
- " - C(host_metadata)" |
| 112 |
- "Possible values when I(event_source=internal):" |
| 113 |
- " - C(host_group)" |
| 114 |
- " - C(host)" |
| 115 |
- " - C(host_template)" |
| 116 |
- " - C(event_type)" |
| 117 |
required: true |
| 118 |
value: |
| 119 |
type: str |
| 120 |
description: |
| 121 |
- Value to compare with. |
| 122 |
- "When I(type=discovery_status), the choices are:" |
| 123 |
- " - C(up)" |
| 124 |
- " - C(down)" |
| 125 |
- " - C(discovered)" |
| 126 |
- " - C(lost)" |
| 127 |
- "When I(type=discovery_object), the choices are:" |
| 128 |
- " - C(host)" |
| 129 |
- " - C(service)" |
| 130 |
- "When I(type=event_type), the choices are:" |
| 131 |
- " - C(item in not supported state)" |
| 132 |
- " - C(item in normal state)" |
| 133 |
- " - C(LLD rule in not supported state)" |
| 134 |
- " - C(LLD rule in normal state)" |
| 135 |
- " - C(trigger in unknown state)" |
| 136 |
- " - C(trigger in normal state)" |
| 137 |
- "When I(type=trigger_severity), the choices are (case-insensitive):" |
| 138 |
- " - C(not classified)" |
| 139 |
- " - C(information)" |
| 140 |
- " - C(warning)" |
| 141 |
- " - C(average)" |
| 142 |
- " - C(high)" |
| 143 |
- " - C(disaster)" |
| 144 |
- Irrespective of user-visible names being changed in Zabbix. Defaults to C(not classified) if omitted. |
| 145 |
- Besides the above options, this is usually either the name |
| 146 |
of the object or a string to compare with. |
| 147 |
value2: |
| 148 |
type: str |
| 149 |
description: |
| 150 |
- Secondary value to compare with. |
| 151 |
- Required for trigger actions when condition I(type=event_tag_value). |
| 152 |
operator: |
| 153 |
type: str |
| 154 |
description: |
| 155 |
- Condition operator. |
| 156 |
- When I(type) is set to C(time_period), the choices are C(in), C(not in). |
| 157 |
choices: |
| 158 |
- "equals" |
| 159 |
- "=" |
| 160 |
- "does not equal" |
| 161 |
- "<>" |
| 162 |
- "contains" |
| 163 |
- "like" |
| 164 |
- "does not contain" |
| 165 |
- "not like" |
| 166 |
- "in" |
| 167 |
- "is greater than or equals" |
| 168 |
- ">=" |
| 169 |
- "is less than or equals" |
| 170 |
- "<=" |
| 171 |
- "not in" |
| 172 |
- "matches" |
| 173 |
- "does not match" |
| 174 |
- "Yes" |
| 175 |
- "No" |
| 176 |
required: true |
| 177 |
formulaid: |
| 178 |
type: str |
| 179 |
description: |
| 180 |
- Arbitrary unique ID that is used to reference the condition from a custom expression. |
| 181 |
- Can only contain upper-case letters. |
| 182 |
- Required for custom expression filters and ignored otherwise. |
| 183 |
eval_type: |
| 184 |
type: str |
| 185 |
description: |
| 186 |
- Filter condition evaluation method. |
| 187 |
- Defaults to C(andor) if conditions are less then 2 or if |
| 188 |
I(formula) is not specified. |
| 189 |
- Defaults to C(custom_expression) when formula is specified. |
| 190 |
choices: |
| 191 |
- "andor" |
| 192 |
- "and" |
| 193 |
- "or" |
| 194 |
- "custom_expression" |
| 195 |
formula: |
| 196 |
type: str |
| 197 |
description: |
| 198 |
- User-defined expression to be used for evaluating conditions with a custom expression. |
| 199 |
- The expression must contain IDs that reference each condition by its formulaid. |
| 200 |
- The IDs used in the expression must exactly match the ones |
| 201 |
defined in the I(conditions). No condition can remain unused or omitted. |
| 202 |
- Required when I(eval_type=custom_expression). |
| 203 |
- Use sequential IDs that start at "A". If non-sequential IDs are used, Zabbix re-indexes them. |
| 204 |
This makes each module run notice the difference in IDs and update the action. |
| 205 |
operations: |
| 206 |
type: list |
| 207 |
elements: dict |
| 208 |
description: |
| 209 |
- List of action operations |
| 210 |
default: [] |
| 211 |
suboptions: |
| 212 |
type: |
| 213 |
type: str |
| 214 |
description: |
| 215 |
- Type of operation. |
| 216 |
- "Valid choices when setting type for I(recovery_operations) and I(acknowledge_operations):" |
| 217 |
- " - C(send_message)" |
| 218 |
- " - C(remote_command)" |
| 219 |
- " - C(notify_all_involved)" |
| 220 |
- Choice C(notify_all_involved) only supported in I(recovery_operations) and I(acknowledge_operations). |
| 221 |
- C(add_host_tags) and C(remove_host_tags) available since Zabbix 7.0. |
| 222 |
choices: |
| 223 |
- send_message |
| 224 |
- remote_command |
| 225 |
- add_host |
| 226 |
- remove_host |
| 227 |
- add_to_host_group |
| 228 |
- remove_from_host_group |
| 229 |
- link_to_template |
| 230 |
- unlink_from_template |
| 231 |
- enable_host |
| 232 |
- disable_host |
| 233 |
- set_host_inventory_mode |
| 234 |
- notify_all_involved |
| 235 |
- add_host_tags |
| 236 |
- remove_host_tags |
| 237 |
required: true |
| 238 |
esc_period: |
| 239 |
type: str |
| 240 |
description: |
| 241 |
- Duration of an escalation step in seconds. |
| 242 |
- Must be greater than 60 seconds. |
| 243 |
- Accepts seconds, time unit with suffix and user macro. |
| 244 |
- If set to 0 or 0s, the default action escalation period will be used. |
| 245 |
default: 0s |
| 246 |
esc_step_from: |
| 247 |
type: int |
| 248 |
description: |
| 249 |
- Step to start escalation from. |
| 250 |
default: 1 |
| 251 |
esc_step_to: |
| 252 |
type: int |
| 253 |
description: |
| 254 |
- Step to end escalation at. |
| 255 |
- Specify 0 for infinitely. |
| 256 |
default: 1 |
| 257 |
send_to_groups: |
| 258 |
type: list |
| 259 |
elements: str |
| 260 |
description: |
| 261 |
- User groups to send messages to. |
| 262 |
send_to_users: |
| 263 |
type: list |
| 264 |
elements: str |
| 265 |
description: |
| 266 |
- Users (usernames or aliases) to send messages to. |
| 267 |
op_message: |
| 268 |
type: str |
| 269 |
description: |
| 270 |
- Operation message text. |
| 271 |
- If I(op_message) and I(subject) not defined then "default message" from media type will be used |
| 272 |
subject: |
| 273 |
type: str |
| 274 |
description: |
| 275 |
- Operation message subject. |
| 276 |
- If I(op_message) and I(subject) not defined then "default message" from media type will be used |
| 277 |
media_type: |
| 278 |
type: str |
| 279 |
description: |
| 280 |
- Media type that will be used to send the message. |
| 281 |
- Can be used with I(type=send_message) or I(type=notify_all_involved) inside I(acknowledge_operations). |
| 282 |
- Set to C(all) for all media types |
| 283 |
default: "all" |
| 284 |
operation_condition: |
| 285 |
type: "str" |
| 286 |
description: |
| 287 |
- The action operation condition object defines a condition that must be met to perform the current operation. |
| 288 |
choices: |
| 289 |
- acknowledged |
| 290 |
- not_acknowledged |
| 291 |
host_groups: |
| 292 |
type: list |
| 293 |
elements: str |
| 294 |
description: |
| 295 |
- List of host groups host should be added to. |
| 296 |
- Required when I(type=add_to_host_group) or I(type=remove_from_host_group). |
| 297 |
templates: |
| 298 |
type: list |
| 299 |
elements: str |
| 300 |
description: |
| 301 |
- List of templates host should be linked to. |
| 302 |
- Required when I(type=link_to_template) or I(type=unlink_from_template). |
| 303 |
inventory: |
| 304 |
type: str |
| 305 |
description: |
| 306 |
- Host inventory mode. |
| 307 |
- Required when I(type=set_host_inventory_mode). |
| 308 |
choices: |
| 309 |
- manual |
| 310 |
- automatic |
| 311 |
command_type: |
| 312 |
type: str |
| 313 |
description: |
| 314 |
- Type of operation command. |
| 315 |
- Required when I(type=remote_command). |
| 316 |
choices: |
| 317 |
- custom_script |
| 318 |
- ipmi |
| 319 |
- ssh |
| 320 |
- telnet |
| 321 |
- global_script |
| 322 |
command: |
| 323 |
type: str |
| 324 |
description: |
| 325 |
- Command to run. |
| 326 |
- Required when I(type=remote_command) and I(command_type!=global_script). |
| 327 |
execute_on: |
| 328 |
type: str |
| 329 |
description: |
| 330 |
- Target on which the custom script operation command will be executed. |
| 331 |
- Required when I(type=remote_command) and I(command_type=custom_script). |
| 332 |
choices: |
| 333 |
- agent |
| 334 |
- server |
| 335 |
- proxy |
| 336 |
run_on_groups: |
| 337 |
type: list |
| 338 |
elements: str |
| 339 |
description: |
| 340 |
- Host groups to run remote commands on. |
| 341 |
- Required when I(type=remote_command) and I(run_on_hosts) is not set. |
| 342 |
run_on_hosts: |
| 343 |
type: list |
| 344 |
elements: str |
| 345 |
description: |
| 346 |
- Hosts to run remote commands on. |
| 347 |
- Required when I(type=remote_command) and I(run_on_groups) is not set. |
| 348 |
- If set to 0 the command will be run on the current host. |
| 349 |
ssh_auth_type: |
| 350 |
type: str |
| 351 |
description: |
| 352 |
- Authentication method used for SSH commands. |
| 353 |
- Required when I(type=remote_command) and I(command_type=ssh). |
| 354 |
choices: |
| 355 |
- password |
| 356 |
- public_key |
| 357 |
ssh_privatekey_file: |
| 358 |
type: str |
| 359 |
description: |
| 360 |
- Name of the private key file used for SSH commands with public key authentication. |
| 361 |
- Required when I(ssh_auth_type=public_key). |
| 362 |
- Can be used when I(type=remote_command). |
| 363 |
ssh_publickey_file: |
| 364 |
type: str |
| 365 |
description: |
| 366 |
- Name of the public key file used for SSH commands with public key authentication. |
| 367 |
- Required when I(ssh_auth_type=public_key). |
| 368 |
- Can be used when I(type=remote_command). |
| 369 |
username: |
| 370 |
type: str |
| 371 |
description: |
| 372 |
- User name used for authentication. |
| 373 |
- Required when I(ssh_auth_type in [public_key, password]) or I(command_type=telnet). |
| 374 |
- Can be used when I(type=remote_command). |
| 375 |
password: |
| 376 |
type: str |
| 377 |
description: |
| 378 |
- Password used for authentication. |
| 379 |
- Required when I(ssh_auth_type=password) or I(command_type=telnet). |
| 380 |
- Can be used when I(type=remote_command). |
| 381 |
port: |
| 382 |
type: int |
| 383 |
description: |
| 384 |
- Port number used for authentication. |
| 385 |
- Can be used when I(command_type in [ssh, telnet]) and I(type=remote_command). |
| 386 |
script_name: |
| 387 |
type: str |
| 388 |
description: |
| 389 |
- The name of script used for global script commands. |
| 390 |
- Required when I(command_type=global_script). |
| 391 |
- Can be used when I(type=remote_command). |
| 392 |
tags: |
| 393 |
type: list |
| 394 |
elements: dict |
| 395 |
description: |
| 396 |
- Host tags to adt have tag property defined. |
| 397 |
- The value property is optional. or remove. |
| 398 |
- upported if operationtype is set to C(add host tags) or C(remove host tags). |
| 399 |
suboptions: |
| 400 |
tag: |
| 401 |
type: str |
| 402 |
description: Tag name |
| 403 |
value: |
| 404 |
type: str |
| 405 |
description: Tag value (optional) |
| 406 |
recovery_operations: |
| 407 |
type: list |
| 408 |
elements: dict |
| 409 |
description: |
| 410 |
- List of recovery operations. |
| 411 |
- C(Suboptions) are the same as for I(operations). |
| 412 |
default: [] |
| 413 |
suboptions: |
| 414 |
type: |
| 415 |
type: str |
| 416 |
description: |
| 417 |
- Type of operation. |
| 418 |
choices: |
| 419 |
- send_message |
| 420 |
- remote_command |
| 421 |
- notify_all_involved |
| 422 |
required: true |
| 423 |
command_type: |
| 424 |
type: str |
| 425 |
required: false |
| 426 |
description: |
| 427 |
- Type of operation command. |
| 428 |
choices: |
| 429 |
- custom_script |
| 430 |
- ipmi |
| 431 |
- ssh |
| 432 |
- telnet |
| 433 |
- global_script |
| 434 |
command: |
| 435 |
type: str |
| 436 |
required: false |
| 437 |
description: |
| 438 |
- Command to run. |
| 439 |
execute_on: |
| 440 |
type: str |
| 441 |
required: false |
| 442 |
description: |
| 443 |
- Target on which the custom script operation command will be executed. |
| 444 |
choices: |
| 445 |
- agent |
| 446 |
- server |
| 447 |
- proxy |
| 448 |
ssh_auth_type: |
| 449 |
type: str |
| 450 |
description: |
| 451 |
- Authentication method used for SSH commands. |
| 452 |
- Required when I(type=remote_command) and I(command_type=ssh). |
| 453 |
choices: |
| 454 |
- password |
| 455 |
- public_key |
| 456 |
ssh_privatekey_file: |
| 457 |
type: str |
| 458 |
description: |
| 459 |
- Name of the private key file used for SSH commands with public key authentication. |
| 460 |
- Required when I(ssh_auth_type=public_key). |
| 461 |
- Can be used when I(type=remote_command). |
| 462 |
ssh_publickey_file: |
| 463 |
type: str |
| 464 |
description: |
| 465 |
- Name of the public key file used for SSH commands with public key authentication. |
| 466 |
- Required when I(ssh_auth_type=public_key). |
| 467 |
- Can be used when I(type=remote_command). |
| 468 |
run_on_groups: |
| 469 |
type: list |
| 470 |
elements: str |
| 471 |
description: |
| 472 |
- Host groups to run remote commands on. |
| 473 |
- Required when I(type=remote_command) and I(run_on_hosts) is not set. |
| 474 |
run_on_hosts: |
| 475 |
type: list |
| 476 |
elements: str |
| 477 |
description: |
| 478 |
- Hosts to run remote commands on. |
| 479 |
- Required when I(type=remote_command) and I(run_on_groups) is not set. |
| 480 |
- If set to 0 the command will be run on the current host. |
| 481 |
send_to_groups: |
| 482 |
type: list |
| 483 |
elements: str |
| 484 |
description: |
| 485 |
- User groups to send messages to. |
| 486 |
send_to_users: |
| 487 |
type: list |
| 488 |
elements: str |
| 489 |
description: |
| 490 |
- Users (usernames or aliases) to send messages to. |
| 491 |
media_type: |
| 492 |
type: str |
| 493 |
description: |
| 494 |
- Media type that will be used to send the message. |
| 495 |
- Can be used with I(type=send_message) or I(type=notify_all_involved) inside I(acknowledge_operations). |
| 496 |
- Set to C(all) for all media types |
| 497 |
default: "all" |
| 498 |
op_message: |
| 499 |
type: str |
| 500 |
description: |
| 501 |
- Operation message text. |
| 502 |
- If I(op_message) and I(subject) not defined then "default message" from media type will be used |
| 503 |
subject: |
| 504 |
type: str |
| 505 |
description: |
| 506 |
- Operation message subject. |
| 507 |
- If I(op_message) and I(subject) not defined then "default message" from media type will be used |
| 508 |
username: |
| 509 |
type: str |
| 510 |
description: |
| 511 |
- User name used for authentication. |
| 512 |
- Required when I(ssh_auth_type in [public_key, password]) or I(command_type=telnet). |
| 513 |
- Can be used when I(type=remote_command). |
| 514 |
password: |
| 515 |
type: str |
| 516 |
description: |
| 517 |
- Password used for authentication. |
| 518 |
- Required when I(ssh_auth_type=password) or I(command_type=telnet). |
| 519 |
- Can be used when I(type=remote_command). |
| 520 |
port: |
| 521 |
type: int |
| 522 |
description: |
| 523 |
- Port number used for authentication. |
| 524 |
- Can be used when I(command_type in [ssh, telnet]) and I(type=remote_command). |
| 525 |
script_name: |
| 526 |
type: str |
| 527 |
description: |
| 528 |
- The name of script used for global script commands. |
| 529 |
- Required when I(command_type=global_script). |
| 530 |
- Can be used when I(type=remote_command). |
| 531 |
acknowledge_operations: |
| 532 |
type: list |
| 533 |
elements: dict |
| 534 |
description: |
| 535 |
- List of acknowledge operations. |
| 536 |
- Action acknowledge operations are known as update operations since Zabbix 4.0. |
| 537 |
- C(Suboptions) are the same as for I(operations). |
| 538 |
suboptions: |
| 539 |
type: |
| 540 |
type: str |
| 541 |
description: |
| 542 |
- Type of operation. |
| 543 |
choices: |
| 544 |
- send_message |
| 545 |
- remote_command |
| 546 |
- notify_all_involved |
| 547 |
required: true |
| 548 |
command_type: |
| 549 |
type: str |
| 550 |
description: |
| 551 |
- Type of operation command. |
| 552 |
required: false |
| 553 |
choices: |
| 554 |
- custom_script |
| 555 |
- ipmi |
| 556 |
- ssh |
| 557 |
- telnet |
| 558 |
- global_script |
| 559 |
execute_on: |
| 560 |
type: str |
| 561 |
required: false |
| 562 |
description: |
| 563 |
- Target on which the custom script operation command will be executed. |
| 564 |
choices: |
| 565 |
- agent |
| 566 |
- server |
| 567 |
- proxy |
| 568 |
command: |
| 569 |
type: str |
| 570 |
required: false |
| 571 |
description: |
| 572 |
- Command to run. |
| 573 |
ssh_auth_type: |
| 574 |
type: str |
| 575 |
description: |
| 576 |
- Authentication method used for SSH commands. |
| 577 |
- Required when I(type=remote_command) and I(command_type=ssh). |
| 578 |
choices: |
| 579 |
- password |
| 580 |
- public_key |
| 581 |
ssh_privatekey_file: |
| 582 |
type: str |
| 583 |
description: |
| 584 |
- Name of the private key file used for SSH commands with public key authentication. |
| 585 |
- Required when I(ssh_auth_type=public_key). |
| 586 |
- Can be used when I(type=remote_command). |
| 587 |
ssh_publickey_file: |
| 588 |
type: str |
| 589 |
description: |
| 590 |
- Name of the public key file used for SSH commands with public key authentication. |
| 591 |
- Required when I(ssh_auth_type=public_key). |
| 592 |
- Can be used when I(type=remote_command). |
| 593 |
run_on_groups: |
| 594 |
type: list |
| 595 |
elements: str |
| 596 |
description: |
| 597 |
- Host groups to run remote commands on. |
| 598 |
- Required when I(type=remote_command) and I(run_on_hosts) is not set. |
| 599 |
run_on_hosts: |
| 600 |
type: list |
| 601 |
elements: str |
| 602 |
description: |
| 603 |
- Hosts to run remote commands on. |
| 604 |
- Required when I(type=remote_command) and I(run_on_groups) is not set. |
| 605 |
- If set to 0 the command will be run on the current host. |
| 606 |
send_to_groups: |
| 607 |
type: list |
| 608 |
elements: str |
| 609 |
description: |
| 610 |
- User groups to send messages to. |
| 611 |
send_to_users: |
| 612 |
type: list |
| 613 |
elements: str |
| 614 |
description: |
| 615 |
- Users (usernames or aliases) to send messages to. |
| 616 |
media_type: |
| 617 |
type: str |
| 618 |
description: |
| 619 |
- Media type that will be used to send the message. |
| 620 |
- Can be used with I(type=send_message) or I(type=notify_all_involved) inside I(acknowledge_operations). |
| 621 |
- Set to C(all) for all media types |
| 622 |
default: "all" |
| 623 |
op_message: |
| 624 |
type: str |
| 625 |
description: |
| 626 |
- Operation message text. |
| 627 |
- If I(op_message) and I(subject) not defined then "default message" from media type will be used |
| 628 |
subject: |
| 629 |
type: str |
| 630 |
description: |
| 631 |
- Operation message subject. |
| 632 |
- If I(op_message) and I(subject) not defined then "default message" from media type will be used |
| 633 |
username: |
| 634 |
type: str |
| 635 |
description: |
| 636 |
- User name used for authentication. |
| 637 |
- Required when I(ssh_auth_type in [public_key, password]) or I(command_type=telnet). |
| 638 |
- Can be used when I(type=remote_command). |
| 639 |
password: |
| 640 |
type: str |
| 641 |
description: |
| 642 |
- Password used for authentication. |
| 643 |
- Required when I(ssh_auth_type=password) or I(command_type=telnet). |
| 644 |
- Can be used when I(type=remote_command). |
| 645 |
port: |
| 646 |
type: int |
| 647 |
description: |
| 648 |
- Port number used for authentication. |
| 649 |
- Can be used when I(command_type in [ssh, telnet]) and I(type=remote_command). |
| 650 |
script_name: |
| 651 |
type: str |
| 652 |
description: |
| 653 |
- The name of script used for global script commands. |
| 654 |
- Required when I(command_type=global_script). |
| 655 |
- Can be used when I(type=remote_command). |
| 656 |
aliases: [ update_operations ] |
| 657 |
default: [] |
| 658 |
pause_symptoms: |
| 659 |
type: bool |
| 660 |
description: |
| 661 |
- Whether to pause escalation if event is a symptom event. |
| 662 |
- I(supported) if C(event_source) is set to C(trigger) |
| 663 |
- Works only with >= Zabbix 7.0 |
| 664 |
default: true |
| 665 |
|
| 666 |
|
| 667 |
extends_documentation_fragment: |
| 668 |
- community.zabbix.zabbix |
| 669 |
|
| 670 |
""" |
| 671 |
|
| 672 |
EXAMPLES = """ |
| 673 |
# If you want to use Username and Password to be authenticated by Zabbix Server |
| 674 |
- name: Set credentials to access Zabbix Server API |
| 675 |
ansible.builtin.set_fact: |
| 676 |
ansible_user: Admin |
| 677 |
ansible_httpapi_pass: zabbix |
| 678 |
|
| 679 |
# If you want to use API token to be authenticated by Zabbix Server |
| 680 |
# https://www.zabbix.com/documentation/current/en/manual/web_interface/frontend_sections/administration/general#api-tokens |
| 681 |
- name: Set API token |
| 682 |
ansible.builtin.set_fact: |
| 683 |
ansible_zabbix_auth_key: 8ec0d52432c15c91fcafe9888500cf9a607f44091ab554dbee860f6b44fac895 |
| 684 |
|
| 685 |
# Trigger action with only one condition |
| 686 |
- name: Deploy trigger action |
| 687 |
# set task level variables as we change ansible_connection plugin here |
| 688 |
vars: |
| 689 |
ansible_network_os: community.zabbix.zabbix |
| 690 |
ansible_connection: httpapi |
| 691 |
ansible_httpapi_port: 443 |
| 692 |
ansible_httpapi_use_ssl: true |
| 693 |
ansible_httpapi_validate_certs: false |
| 694 |
ansible_zabbix_url_path: "zabbixeu" # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu |
| 695 |
ansible_host: zabbix-example-fqdn.org |
| 696 |
community.zabbix.zabbix_action: |
| 697 |
name: "Send alerts to Admin" |
| 698 |
event_source: "trigger" |
| 699 |
state: present |
| 700 |
status: enabled |
| 701 |
esc_period: 60 |
| 702 |
conditions: |
| 703 |
- type: "trigger_severity" |
| 704 |
operator: ">=" |
| 705 |
value: "Information" |
| 706 |
operations: |
| 707 |
- type: send_message |
| 708 |
subject: "Something bad is happening" |
| 709 |
op_message: "Come on, guys do something" |
| 710 |
media_type: "Email" |
| 711 |
send_to_users: |
| 712 |
- "Admin" |
| 713 |
|
| 714 |
# Trigger action with multiple conditions and operations |
| 715 |
- name: Deploy trigger action |
| 716 |
# set task level variables as we change ansible_connection plugin here |
| 717 |
vars: |
| 718 |
ansible_network_os: community.zabbix.zabbix |
| 719 |
ansible_connection: httpapi |
| 720 |
ansible_httpapi_port: 443 |
| 721 |
ansible_httpapi_use_ssl: true |
| 722 |
ansible_httpapi_validate_certs: false |
| 723 |
ansible_zabbix_url_path: "zabbixeu" # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu |
| 724 |
ansible_host: zabbix-example-fqdn.org |
| 725 |
community.zabbix.zabbix_action: |
| 726 |
name: "Send alerts to Admin" |
| 727 |
event_source: "trigger" |
| 728 |
state: present |
| 729 |
status: enabled |
| 730 |
esc_period: 1m |
| 731 |
conditions: |
| 732 |
- type: "trigger_name" |
| 733 |
operator: "like" |
| 734 |
value: "Zabbix agent is unreachable" |
| 735 |
formulaid: A |
| 736 |
- type: "trigger_severity" |
| 737 |
operator: ">=" |
| 738 |
value: "disaster" |
| 739 |
formulaid: B |
| 740 |
formula: A or B |
| 741 |
operations: |
| 742 |
- type: send_message |
| 743 |
media_type: "Email" |
| 744 |
send_to_users: |
| 745 |
- "Admin" |
| 746 |
- type: remote_command |
| 747 |
command: "systemctl restart zabbix-agent" |
| 748 |
command_type: custom_script |
| 749 |
execute_on: server |
| 750 |
run_on_hosts: |
| 751 |
- 0 |
| 752 |
|
| 753 |
# Trigger action with recovery and acknowledge operations |
| 754 |
- name: Deploy trigger action |
| 755 |
# set task level variables as we change ansible_connection plugin here |
| 756 |
vars: |
| 757 |
ansible_network_os: community.zabbix.zabbix |
| 758 |
ansible_connection: httpapi |
| 759 |
ansible_httpapi_port: 443 |
| 760 |
ansible_httpapi_use_ssl: true |
| 761 |
ansible_httpapi_validate_certs: false |
| 762 |
ansible_zabbix_url_path: "zabbixeu" # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu |
| 763 |
ansible_host: zabbix-example-fqdn.org |
| 764 |
community.zabbix.zabbix_action: |
| 765 |
name: "Send alerts to Admin" |
| 766 |
event_source: "trigger" |
| 767 |
state: present |
| 768 |
status: enabled |
| 769 |
esc_period: 1h |
| 770 |
conditions: |
| 771 |
- type: "trigger_severity" |
| 772 |
operator: ">=" |
| 773 |
value: "Information" |
| 774 |
operations: |
| 775 |
- type: send_message |
| 776 |
subject: "Something bad is happening" |
| 777 |
op_message: "Come on, guys do something" |
| 778 |
media_type: "Email" |
| 779 |
send_to_users: |
| 780 |
- "Admin" |
| 781 |
recovery_operations: |
| 782 |
- type: send_message |
| 783 |
subject: "Host is down" |
| 784 |
op_message: "Come on, guys do something" |
| 785 |
media_type: "Email" |
| 786 |
send_to_users: |
| 787 |
- "Admin" |
| 788 |
acknowledge_operations: |
| 789 |
- type: send_message |
| 790 |
media_type: "Email" |
| 791 |
send_to_users: |
| 792 |
- "Admin" |
| 793 |
""" |
| 794 |
|
| 795 |
RETURN = """ |
| 796 |
msg: |
| 797 |
description: The result of the operation |
| 798 |
returned: success |
| 799 |
type: str |
| 800 |
sample: "Action Deleted: Register webservers, ID: 0001" |
| 801 |
""" |
| 802 |
|
| 803 |
|
| 804 |
from ansible.module_utils.basic import AnsibleModule |
| 805 |
|
| 806 |
from ansible_collections.community.zabbix.plugins.module_utils.base import ZabbixBase |
| 807 |
from ansible.module_utils.compat.version import LooseVersion |
| 808 |
|
| 809 |
import ansible_collections.community.zabbix.plugins.module_utils.helpers as zabbix_utils |
| 810 |
import re |
| 811 |
|
| 812 |
|
| 813 |
class Zapi(ZabbixBase): |
| 814 |
def __init__(self, module, zbx=None): |
| 815 |
super(Zapi, self).__init__(module, zbx) |
| 816 |
self._zapi_wrapper = self |
| 817 |
|
| 818 |
def check_if_action_exists(self, name): |
| 819 |
"""Check if action exists. |
| 820 |
|
| 821 |
Args: |
| 822 |
name: Name of the action. |
| 823 |
|
| 824 |
Returns: |
| 825 |
The return value. True for success, False otherwise. |
| 826 |
|
| 827 |
""" |
| 828 |
try: |
| 829 |
_params = { |
| 830 |
"selectOperations": "extend", |
| 831 |
"selectRecoveryOperations": "extend", |
| 832 |
"selectUpdateOperations": "extend", |
| 833 |
"selectFilter": "extend", |
| 834 |
"filter": {"name": [name]} |
| 835 |
} |
| 836 |
_action = self._zapi.action.get(_params) |
| 837 |
return _action |
| 838 |
except Exception as e: |
| 839 |
self._module.fail_json(msg="Failed to check if action '%s' exists: %s" % (name, e)) |
| 840 |
|
| 841 |
def get_action_by_name(self, name): |
| 842 |
"""Get action by name |
| 843 |
|
| 844 |
Args: |
| 845 |
name: Name of the action. |
| 846 |
|
| 847 |
Returns: |
| 848 |
dict: Zabbix action |
| 849 |
|
| 850 |
""" |
| 851 |
try: |
| 852 |
action_list = self._zapi.action.get({ |
| 853 |
"output": "extend", |
| 854 |
"filter": {"name": [name]} |
| 855 |
}) |
| 856 |
if len(action_list) < 1: |
| 857 |
self._module.fail_json(msg="Action not found: %s" % name) |
| 858 |
else: |
| 859 |
return action_list[0] |
| 860 |
except Exception as e: |
| 861 |
self._module.fail_json(msg="Failed to get ID of '%s': %s" % (name, e)) |
| 862 |
|
| 863 |
def get_host_by_host_name(self, host_name): |
| 864 |
"""Get host by host name |
| 865 |
|
| 866 |
Args: |
| 867 |
host_name: host name. |
| 868 |
|
| 869 |
Returns: |
| 870 |
host matching host name |
| 871 |
|
| 872 |
""" |
| 873 |
try: |
| 874 |
host_list = self._zapi.host.get({ |
| 875 |
"output": "extend", |
| 876 |
"selectInventory": "extend", |
| 877 |
"filter": {"host": [host_name]} |
| 878 |
}) |
| 879 |
if len(host_list) < 1: |
| 880 |
self._module.fail_json(msg="Host not found: %s" % host_name) |
| 881 |
else: |
| 882 |
return host_list[0] |
| 883 |
except Exception as e: |
| 884 |
self._module.fail_json(msg="Failed to get host '%s': %s" % (host_name, e)) |
| 885 |
|
| 886 |
def get_hostgroup_by_hostgroup_name(self, hostgroup_name): |
| 887 |
"""Get host group by host group name |
| 888 |
|
| 889 |
Args: |
| 890 |
hostgroup_name: host group name. |
| 891 |
|
| 892 |
Returns: |
| 893 |
host group matching host group name |
| 894 |
|
| 895 |
""" |
| 896 |
try: |
| 897 |
hostgroup_list = self._zapi.hostgroup.get({ |
| 898 |
"output": "extend", |
| 899 |
"filter": {"name": [hostgroup_name]} |
| 900 |
}) |
| 901 |
if len(hostgroup_list) < 1: |
| 902 |
self._module.fail_json(msg="Host group not found: %s" % hostgroup_name) |
| 903 |
else: |
| 904 |
return hostgroup_list[0] |
| 905 |
except Exception as e: |
| 906 |
self._module.fail_json(msg="Failed to get host group '%s': %s" % (hostgroup_name, e)) |
| 907 |
|
| 908 |
def get_template_by_template_name(self, template_name): |
| 909 |
"""Get template by template name |
| 910 |
|
| 911 |
Args: |
| 912 |
template_name: template name. |
| 913 |
|
| 914 |
Returns: |
| 915 |
template matching template name |
| 916 |
|
| 917 |
""" |
| 918 |
try: |
| 919 |
template_list = self._zapi.template.get({ |
| 920 |
"output": "extend", |
| 921 |
"filter": {"host": [template_name]} |
| 922 |
}) |
| 923 |
if len(template_list) < 1: |
| 924 |
self._module.fail_json(msg="Template not found: %s" % template_name) |
| 925 |
else: |
| 926 |
return template_list[0] |
| 927 |
except Exception as e: |
| 928 |
self._module.fail_json(msg="Failed to get template '%s': %s" % (template_name, e)) |
| 929 |
|
| 930 |
def get_trigger_by_trigger_name(self, trigger_name): |
| 931 |
"""Get trigger by trigger name |
| 932 |
|
| 933 |
Args: |
| 934 |
trigger_name: trigger name. |
| 935 |
|
| 936 |
Returns: |
| 937 |
trigger matching trigger name |
| 938 |
|
| 939 |
""" |
| 940 |
try: |
| 941 |
trigger_list = self._zapi.trigger.get({ |
| 942 |
"output": "extend", |
| 943 |
"filter": {"description": [trigger_name]} |
| 944 |
}) |
| 945 |
if len(trigger_list) < 1: |
| 946 |
self._module.fail_json(msg="Trigger not found: %s" % trigger_name) |
| 947 |
else: |
| 948 |
return trigger_list[0] |
| 949 |
except Exception as e: |
| 950 |
self._module.fail_json(msg="Failed to get trigger '%s': %s" % (trigger_name, e)) |
| 951 |
|
| 952 |
def get_discovery_rule_by_discovery_rule_name(self, discovery_rule_name): |
| 953 |
"""Get discovery rule by discovery rule name |
| 954 |
|
| 955 |
Args: |
| 956 |
discovery_rule_name: discovery rule name. |
| 957 |
|
| 958 |
Returns: |
| 959 |
discovery rule matching discovery rule name |
| 960 |
|
| 961 |
""" |
| 962 |
try: |
| 963 |
discovery_rule_list = self._zapi.drule.get({ |
| 964 |
"output": "extend", |
| 965 |
"filter": {"name": [discovery_rule_name]} |
| 966 |
}) |
| 967 |
if len(discovery_rule_list) < 1: |
| 968 |
self._module.fail_json(msg="Discovery rule not found: %s" % discovery_rule_name) |
| 969 |
else: |
| 970 |
return discovery_rule_list[0] |
| 971 |
except Exception as e: |
| 972 |
self._module.fail_json(msg="Failed to get discovery rule '%s': %s" % (discovery_rule_name, e)) |
| 973 |
|
| 974 |
def get_discovery_check_by_discovery_check_name(self, discovery_check_name): |
| 975 |
"""Get discovery check by discovery check name |
| 976 |
|
| 977 |
Args: |
| 978 |
discovery_check_name: discovery check name. |
| 979 |
|
| 980 |
Returns: |
| 981 |
discovery check matching discovery check name |
| 982 |
|
| 983 |
""" |
| 984 |
try: |
| 985 |
dcheck_pattern = r'^([^:]+)\:\s([\w\s]+)(\s\(([\d,-]+)\))?(\s\"(.+)\")?$' |
| 986 |
match = re.match(dcheck_pattern, discovery_check_name) |
| 987 |
if match: |
| 988 |
discovery_rule_name = match.group(1) |
| 989 |
dcheck_type = match.group(2) |
| 990 |
dcheck_ports = match.group(4) |
| 991 |
dcheck_key = match.group(6) |
| 992 |
else: |
| 993 |
self._module.fail_json(msg="Discovery check name: %s does not set" % discovery_check_name) |
| 994 |
dcheck_type_to_number = { |
| 995 |
"SSH": "0", |
| 996 |
"LDAP": "1", |
| 997 |
"SMTP": "2", |
| 998 |
"FTP": "3", |
| 999 |
"HTTP": "4", |
| 1000 |
"POP": "5", |
| 1001 |
"NNTP": "6", |
| 1002 |
"IMAP": "7", |
| 1003 |
"TCP": "8", |
| 1004 |
"Zabbix agent": "9", |
| 1005 |
"SNMPv1 agent": "10", |
| 1006 |
"SNMPv2 agent": "11", |
| 1007 |
"ICMP ping": "12", |
| 1008 |
"SNMPv3 agent": "13", |
| 1009 |
"HTTPS": "14", |
| 1010 |
"Telnet": "15" |
| 1011 |
} |
| 1012 |
|
| 1013 |
if dcheck_type not in dcheck_type_to_number: |
| 1014 |
self._module.fail_json(msg="Discovery check type: %s does not exist" % dcheck_type) |
| 1015 |
|
| 1016 |
discovery_rule_list = self._zapi.drule.get({ |
| 1017 |
"output": ["dchecks"], |
| 1018 |
"filter": {"name": [discovery_rule_name]}, |
| 1019 |
"selectDChecks": "extend" |
| 1020 |
}) |
| 1021 |
if len(discovery_rule_list) < 1: |
| 1022 |
self._module.fail_json(msg="Discovery check not found: %s" % discovery_check_name) |
| 1023 |
for dcheck in discovery_rule_list[0]["dchecks"]: |
| 1024 |
if dcheck_key is not None and dcheck_ports is not None: |
| 1025 |
if dcheck_type_to_number[dcheck_type] == dcheck["type"] and dcheck_key == dcheck["key_"] and dcheck_ports == dcheck["ports"]: |
| 1026 |
return dcheck |
| 1027 |
elif dcheck_key is not None and dcheck_ports is None: |
| 1028 |
if dcheck_type_to_number[dcheck_type] == dcheck["type"] and dcheck_key == dcheck["key_"]: |
| 1029 |
return dcheck |
| 1030 |
elif dcheck_key is None and dcheck_ports is not None: |
| 1031 |
if dcheck_type_to_number[dcheck_type] == dcheck["type"] and dcheck_ports == dcheck["ports"]: |
| 1032 |
return dcheck |
| 1033 |
elif dcheck_type_to_number[dcheck_type] == dcheck["type"]: |
| 1034 |
return dcheck |
| 1035 |
self._module.fail_json(msg="Discovery check not found in condition: %s" % discovery_check_name) |
| 1036 |
except Exception as e: |
| 1037 |
self._module.fail_json(msg="Failed to get discovery check '%s': %s" % (discovery_check_name, e)) |
| 1038 |
|
| 1039 |
def get_proxy_by_proxy_name(self, proxy_name): |
| 1040 |
"""Get proxy by proxy name |
| 1041 |
|
| 1042 |
Args: |
| 1043 |
proxy_name: proxy name. |
| 1044 |
|
| 1045 |
Returns: |
| 1046 |
proxy matching proxy name |
| 1047 |
|
| 1048 |
""" |
| 1049 |
try: |
| 1050 |
if LooseVersion(self._zbx_api_version) >= LooseVersion('7.0'): |
| 1051 |
filter = {'name': [proxy_name]} |
| 1052 |
else: |
| 1053 |
filter = {'host': [proxy_name]} |
| 1054 |
proxy_list = self._zapi.proxy.get({ |
| 1055 |
"output": "extend", |
| 1056 |
"filter": filter, |
| 1057 |
}) |
| 1058 |
if len(proxy_list) < 1: |
| 1059 |
self._module.fail_json(msg="Proxy not found: %s" % proxy_name) |
| 1060 |
else: |
| 1061 |
return proxy_list[0] |
| 1062 |
except Exception as e: |
| 1063 |
self._module.fail_json(msg="Failed to get proxy '%s': %s" % (proxy_name, e)) |
| 1064 |
|
| 1065 |
def get_mediatype_by_mediatype_name(self, mediatype_name): |
| 1066 |
"""Get mediatype by mediatype name |
| 1067 |
|
| 1068 |
Args: |
| 1069 |
mediatype_name: mediatype name |
| 1070 |
|
| 1071 |
Returns: |
| 1072 |
mediatype matching mediatype name |
| 1073 |
|
| 1074 |
""" |
| 1075 |
filter = {"name": [mediatype_name]} |
| 1076 |
|
| 1077 |
try: |
| 1078 |
if str(mediatype_name).lower() == "all": |
| 1079 |
return "0" |
| 1080 |
mediatype_list = self._zapi.mediatype.get({ |
| 1081 |
"output": "extend", |
| 1082 |
"filter": filter |
| 1083 |
}) |
| 1084 |
if len(mediatype_list) < 1: |
| 1085 |
self._module.fail_json(msg="Media type not found: %s" % mediatype_name) |
| 1086 |
else: |
| 1087 |
return mediatype_list[0]["mediatypeid"] |
| 1088 |
except Exception as e: |
| 1089 |
self._module.fail_json(msg="Failed to get mediatype '%s': %s" % (mediatype_name, e)) |
| 1090 |
|
| 1091 |
def get_user_by_user_name(self, user_name): |
| 1092 |
"""Get user by user name |
| 1093 |
|
| 1094 |
Args: |
| 1095 |
user_name: user name |
| 1096 |
|
| 1097 |
Returns: |
| 1098 |
user matching user name |
| 1099 |
|
| 1100 |
""" |
| 1101 |
try: |
| 1102 |
filter = {"username": [user_name]} |
| 1103 |
user_list = self._zapi.user.get({ |
| 1104 |
"output": "extend", |
| 1105 |
"filter": filter, |
| 1106 |
}) |
| 1107 |
if len(user_list) < 1: |
| 1108 |
self._module.fail_json(msg="User not found: %s" % user_name) |
| 1109 |
else: |
| 1110 |
return user_list[0] |
| 1111 |
except Exception as e: |
| 1112 |
self._module.fail_json(msg="Failed to get user '%s': %s" % (user_name, e)) |
| 1113 |
|
| 1114 |
def get_usergroup_by_usergroup_name(self, usergroup_name): |
| 1115 |
"""Get usergroup by usergroup name |
| 1116 |
|
| 1117 |
Args: |
| 1118 |
usergroup_name: usergroup name |
| 1119 |
|
| 1120 |
Returns: |
| 1121 |
usergroup matching usergroup name |
| 1122 |
|
| 1123 |
""" |
| 1124 |
try: |
| 1125 |
usergroup_list = self._zapi.usergroup.get({ |
| 1126 |
"output": "extend", |
| 1127 |
"filter": {"name": [usergroup_name]} |
| 1128 |
}) |
| 1129 |
if len(usergroup_list) < 1: |
| 1130 |
self._module.fail_json(msg="User group not found: %s" % usergroup_name) |
| 1131 |
else: |
| 1132 |
return usergroup_list[0] |
| 1133 |
except Exception as e: |
| 1134 |
self._module.fail_json(msg="Failed to get user group '%s': %s" % (usergroup_name, e)) |
| 1135 |
|
| 1136 |
# get script by script name |
| 1137 |
def get_script_by_script_name(self, script_name): |
| 1138 |
"""Get script by script name |
| 1139 |
|
| 1140 |
Args: |
| 1141 |
script_name: script name |
| 1142 |
|
| 1143 |
Returns: |
| 1144 |
script matching script name |
| 1145 |
|
| 1146 |
""" |
| 1147 |
try: |
| 1148 |
if script_name is None: |
| 1149 |
return {} |
| 1150 |
script_list = self._zapi.script.get({ |
| 1151 |
"output": "extend", |
| 1152 |
"filter": {"name": [script_name]} |
| 1153 |
}) |
| 1154 |
if len(script_list) < 1: |
| 1155 |
self._module.fail_json(msg="Script not found: %s" % script_name) |
| 1156 |
else: |
| 1157 |
return script_list[0] |
| 1158 |
except Exception as e: |
| 1159 |
self._module.fail_json(msg="Failed to get script '%s': %s" % (script_name, e)) |
| 1160 |
|
| 1161 |
|
| 1162 |
class Action(Zapi): |
| 1163 |
def __init__(self, module, zbx=None): |
| 1164 |
super(Action, self).__init__(module, zbx) |
| 1165 |
self.existing_data = None |
| 1166 |
|
| 1167 |
def _construct_parameters(self, **kwargs): |
| 1168 |
"""Construct parameters. |
| 1169 |
|
| 1170 |
Args: |
| 1171 |
**kwargs: Arbitrary keyword parameters. |
| 1172 |
|
| 1173 |
Returns: |
| 1174 |
dict: dictionary of specified parameters |
| 1175 |
""" |
| 1176 |
|
| 1177 |
_params = { |
| 1178 |
"name": kwargs["name"], |
| 1179 |
"eventsource": zabbix_utils.helper_to_numeric_value([ |
| 1180 |
"trigger", |
| 1181 |
"discovery", |
| 1182 |
"auto_registration", |
| 1183 |
"internal", |
| 1184 |
"services"], kwargs["event_source"]), |
| 1185 |
"esc_period": kwargs.get("esc_period"), |
| 1186 |
"filter": kwargs["conditions"], |
| 1187 |
"operations": kwargs["operations"], |
| 1188 |
"recovery_operations": kwargs.get("recovery_operations"), |
| 1189 |
"acknowledge_operations": kwargs.get("acknowledge_operations"), |
| 1190 |
"status": zabbix_utils.helper_to_numeric_value([ |
| 1191 |
"enabled", |
| 1192 |
"disabled"], kwargs["status"]) |
| 1193 |
} |
| 1194 |
|
| 1195 |
if kwargs["event_source"] == "trigger": |
| 1196 |
_params["pause_suppressed"] = "1" if kwargs["pause_in_maintenance"] else "0" |
| 1197 |
if LooseVersion(self._zbx_api_version) >= LooseVersion("7.0"): |
| 1198 |
_params["pause_symptoms"] = "1" if kwargs["pause_symptoms"] else "0" |
| 1199 |
_params["notify_if_canceled"] = "1" if kwargs["notify_if_canceled"] else "0" |
| 1200 |
|
| 1201 |
_params["update_operations"] = kwargs.get("update_operations") |
| 1202 |
if "update_operations" in _params and not isinstance(_params.get("update_operations", None), type(None)): |
| 1203 |
_params.pop("acknowledge_operations", None) |
| 1204 |
elif isinstance(_params.get("acknowledge_operations", None), list): |
| 1205 |
_params["update_operations"] = _params.pop("acknowledge_operations", []) |
| 1206 |
else: |
| 1207 |
_params["update_operations"] = [] |
| 1208 |
_params.pop("acknowledge_operations", None) |
| 1209 |
|
| 1210 |
if "esc_period" in _params and isinstance(_params.get("esc_period", None), type(None)): |
| 1211 |
_params.pop("esc_period") |
| 1212 |
|
| 1213 |
if "recovery_operations" in _params: |
| 1214 |
if isinstance(_params.get("recovery_operations", None), type(None)) or len(_params.get("recovery_operations", [])) == 0: |
| 1215 |
_params.pop("recovery_operations") |
| 1216 |
|
| 1217 |
if "update_operations" in _params: |
| 1218 |
if isinstance(_params.get("update_operations", None), type(None)) or len(_params.get("update_operations", [])) == 0: |
| 1219 |
_params.pop("update_operations") |
| 1220 |
|
| 1221 |
if _params["eventsource"] not in [0, 4]: |
| 1222 |
_params.pop("esc_period") |
| 1223 |
|
| 1224 |
return _params |
| 1225 |
|
| 1226 |
def check_difference(self, **kwargs): |
| 1227 |
"""Check difference between action and user specified parameters. |
| 1228 |
|
| 1229 |
Args: |
| 1230 |
**kwargs: Arbitrary keyword parameters. |
| 1231 |
|
| 1232 |
Returns: |
| 1233 |
dict: dictionary of differences |
| 1234 |
""" |
| 1235 |
existing_action = zabbix_utils.helper_convert_unicode_to_str(self.check_if_action_exists(kwargs["name"])[0]) |
| 1236 |
parameters = zabbix_utils.helper_convert_unicode_to_str(self._construct_parameters(**kwargs)) |
| 1237 |
change_parameters = {} |
| 1238 |
_diff = zabbix_utils.helper_cleanup_data(zabbix_utils.helper_compare_dictionaries(parameters, existing_action, change_parameters)) |
| 1239 |
return _diff |
| 1240 |
|
| 1241 |
def update_action(self, **kwargs): |
| 1242 |
"""Update action. |
| 1243 |
|
| 1244 |
Args: |
| 1245 |
**kwargs: Arbitrary keyword parameters. |
| 1246 |
|
| 1247 |
Returns: |
| 1248 |
action: updated action |
| 1249 |
""" |
| 1250 |
try: |
| 1251 |
if self._module.check_mode: |
| 1252 |
self._module.exit_json(msg="Action would be updated if check mode was not specified: %s" % kwargs, changed=True) |
| 1253 |
kwargs["actionid"] = kwargs.pop("action_id") |
| 1254 |
return self._zapi.action.update(kwargs) |
| 1255 |
except Exception as e: |
| 1256 |
self._module.fail_json(msg="Failed to update action '%s': %s" % (kwargs["actionid"], e)) |
| 1257 |
|
| 1258 |
def add_action(self, **kwargs): |
| 1259 |
"""Add action. |
| 1260 |
|
| 1261 |
Args: |
| 1262 |
**kwargs: Arbitrary keyword parameters. |
| 1263 |
|
| 1264 |
Returns: |
| 1265 |
action: added action |
| 1266 |
""" |
| 1267 |
try: |
| 1268 |
if self._module.check_mode: |
| 1269 |
self._module.exit_json(msg="Action would be added if check mode was not specified", changed=True) |
| 1270 |
parameters = self._construct_parameters(**kwargs) |
| 1271 |
action_list = self._zapi.action.create(parameters) |
| 1272 |
return action_list["actionids"][0] |
| 1273 |
except Exception as e: |
| 1274 |
self._module.fail_json(msg="Failed to create action '%s': %s" % (kwargs["name"], e)) |
| 1275 |
|
| 1276 |
def delete_action(self, action_id): |
| 1277 |
"""Delete action. |
| 1278 |
|
| 1279 |
Args: |
| 1280 |
action_id: Action id |
| 1281 |
|
| 1282 |
Returns: |
| 1283 |
action: deleted action |
| 1284 |
""" |
| 1285 |
try: |
| 1286 |
if self._module.check_mode: |
| 1287 |
self._module.exit_json(msg="Action would be deleted if check mode was not specified", changed=True) |
| 1288 |
return self._zapi.action.delete([action_id]) |
| 1289 |
except Exception as e: |
| 1290 |
self._module.fail_json(msg="Failed to delete action '%s': %s" % (action_id, e)) |
| 1291 |
|
| 1292 |
|
| 1293 |
class Operations(Zapi): |
| 1294 |
def _construct_operationtype(self, operation): |
| 1295 |
"""Construct operation type. |
| 1296 |
|
| 1297 |
Args: |
| 1298 |
operation: operation to construct |
| 1299 |
|
| 1300 |
Returns: |
| 1301 |
str: constructed operation |
| 1302 |
""" |
| 1303 |
try: |
| 1304 |
return zabbix_utils.helper_to_numeric_value([ |
| 1305 |
"send_message", |
| 1306 |
"remote_command", |
| 1307 |
"add_host", |
| 1308 |
"remove_host", |
| 1309 |
"add_to_host_group", |
| 1310 |
"remove_from_host_group", |
| 1311 |
"link_to_template", |
| 1312 |
"unlink_from_template", |
| 1313 |
"enable_host", |
| 1314 |
"disable_host", |
| 1315 |
"set_host_inventory_mode", |
| 1316 |
"none", |
| 1317 |
"none", |
| 1318 |
"add_host_tags", |
| 1319 |
"remove_host_tags"], |
| 1320 |
operation["type"] |
| 1321 |
) |
| 1322 |
except Exception: |
| 1323 |
self._module.fail_json(msg="Unsupported value '%s' for operation type." % operation["type"]) |
| 1324 |
|
| 1325 |
def _construct_opmessage(self, operation): |
| 1326 |
"""Construct operation message. |
| 1327 |
|
| 1328 |
Args: |
| 1329 |
operation: operation to construct the message |
| 1330 |
|
| 1331 |
Returns: |
| 1332 |
dict: constructed operation message |
| 1333 |
""" |
| 1334 |
try: |
| 1335 |
return { |
| 1336 |
"default_msg": "0" if operation.get("op_message") is not None or operation.get("subject") is not None else "1", |
| 1337 |
"mediatypeid": self._zapi_wrapper.get_mediatype_by_mediatype_name( |
| 1338 |
operation.get("media_type") |
| 1339 |
) if operation.get("media_type") is not None else "0", |
| 1340 |
"message": operation.get("op_message"), |
| 1341 |
"subject": operation.get("subject"), |
| 1342 |
} |
| 1343 |
except Exception as e: |
| 1344 |
self._module.fail_json(msg="Failed to construct operation message. The error was: %s" % e) |
| 1345 |
|
| 1346 |
def _construct_opmessage_usr(self, operation): |
| 1347 |
"""Construct operation message user. |
| 1348 |
|
| 1349 |
Args: |
| 1350 |
operation: operation to construct the message user |
| 1351 |
|
| 1352 |
Returns: |
| 1353 |
list: constructed operation message user or None if operation not found |
| 1354 |
""" |
| 1355 |
if operation.get("send_to_users") is None: |
| 1356 |
return None |
| 1357 |
return [{ |
| 1358 |
"userid": self._zapi_wrapper.get_user_by_user_name(_user)["userid"] |
| 1359 |
} for _user in operation.get("send_to_users")] |
| 1360 |
|
| 1361 |
def _construct_opmessage_grp(self, operation): |
| 1362 |
"""Construct operation message group. |
| 1363 |
|
| 1364 |
Args: |
| 1365 |
operation: operation to construct the message group |
| 1366 |
|
| 1367 |
Returns: |
| 1368 |
list: constructed operation message group or None if operation not found |
| 1369 |
""" |
| 1370 |
if operation.get("send_to_groups") is None: |
| 1371 |
return None |
| 1372 |
return [{ |
| 1373 |
"usrgrpid": self._zapi_wrapper.get_usergroup_by_usergroup_name(_group)["usrgrpid"] |
| 1374 |
} for _group in operation.get("send_to_groups")] |
| 1375 |
|
| 1376 |
def _construct_opcommand(self, operation): |
| 1377 |
"""Construct operation command. |
| 1378 |
|
| 1379 |
Args: |
| 1380 |
operation: operation to construct command |
| 1381 |
|
| 1382 |
Returns: |
| 1383 |
list: constructed operation command |
| 1384 |
""" |
| 1385 |
try: |
| 1386 |
opcommand = { |
| 1387 |
"scriptid": self._zapi_wrapper.get_script_by_script_name( |
| 1388 |
operation.get("script_name") |
| 1389 |
).get("scriptid") |
| 1390 |
} |
| 1391 |
|
| 1392 |
return opcommand |
| 1393 |
|
| 1394 |
except Exception as e: |
| 1395 |
self._module.fail_json(msg="Failed to construct operation command. The error was: %s" % e) |
| 1396 |
|
| 1397 |
def _construct_opcommand_hst(self, operation): |
| 1398 |
"""Construct operation command host. |
| 1399 |
|
| 1400 |
Args: |
| 1401 |
operation: operation to construct command host |
| 1402 |
|
| 1403 |
Returns: |
| 1404 |
list: constructed operation command host |
| 1405 |
""" |
| 1406 |
if operation.get("run_on_hosts") is None: |
| 1407 |
return None |
| 1408 |
return [{ |
| 1409 |
"hostid": self._zapi_wrapper.get_host_by_host_name(_host)["hostid"] |
| 1410 |
} if str(_host) != "0" else {"hostid": "0"} for _host in operation.get("run_on_hosts")] |
| 1411 |
|
| 1412 |
def _construct_opcommand_grp(self, operation): |
| 1413 |
"""Construct operation command group. |
| 1414 |
|
| 1415 |
Args: |
| 1416 |
operation: operation to construct command group |
| 1417 |
|
| 1418 |
Returns: |
| 1419 |
list: constructed operation command group |
| 1420 |
""" |
| 1421 |
if operation.get("run_on_groups") is None: |
| 1422 |
return None |
| 1423 |
return [{ |
| 1424 |
"groupid": self._zapi_wrapper.get_hostgroup_by_hostgroup_name(_group)["groupid"] |
| 1425 |
} for _group in operation.get("run_on_groups")] |
| 1426 |
|
| 1427 |
def _construct_opgroup(self, operation): |
| 1428 |
"""Construct operation group. |
| 1429 |
|
| 1430 |
Args: |
| 1431 |
operation: operation to construct group |
| 1432 |
|
| 1433 |
Returns: |
| 1434 |
list: constructed operation group |
| 1435 |
""" |
| 1436 |
return [{ |
| 1437 |
"groupid": self._zapi_wrapper.get_hostgroup_by_hostgroup_name(_group)["groupid"] |
| 1438 |
} for _group in operation.get("host_groups", [])] |
| 1439 |
|
| 1440 |
def _construct_optemplate(self, operation): |
| 1441 |
"""Construct operation template. |
| 1442 |
|
| 1443 |
Args: |
| 1444 |
operation: operation to construct template |
| 1445 |
|
| 1446 |
Returns: |
| 1447 |
list: constructed operation template |
| 1448 |
""" |
| 1449 |
return [{ |
| 1450 |
"templateid": self._zapi_wrapper.get_template_by_template_name(_template)["templateid"] |
| 1451 |
} for _template in operation.get("templates", [])] |
| 1452 |
|
| 1453 |
def _construct_opinventory(self, operation): |
| 1454 |
"""Construct operation inventory. |
| 1455 |
|
| 1456 |
Args: |
| 1457 |
operation: operation to construct inventory |
| 1458 |
|
| 1459 |
Returns: |
| 1460 |
dict: constructed operation inventory |
| 1461 |
""" |
| 1462 |
return { |
| 1463 |
"inventory_mode": zabbix_utils.helper_to_numeric_value([ |
| 1464 |
"manual", |
| 1465 |
"automatic" |
| 1466 |
], operation.get("inventory")) |
| 1467 |
} |
| 1468 |
|
| 1469 |
def _construct_opconditions(self, operation): |
| 1470 |
"""Construct operation conditions. |
| 1471 |
|
| 1472 |
Args: |
| 1473 |
operation: operation to construct the conditions |
| 1474 |
|
| 1475 |
Returns: |
| 1476 |
list: constructed operation conditions |
| 1477 |
""" |
| 1478 |
_opcond = operation.get("operation_condition") |
| 1479 |
if _opcond is not None: |
| 1480 |
if _opcond == "acknowledged": |
| 1481 |
_value = "1" |
| 1482 |
elif _opcond == "not_acknowledged": |
| 1483 |
_value = "0" |
| 1484 |
return [{ |
| 1485 |
"conditiontype": "14", |
| 1486 |
"operator": "0", |
| 1487 |
"value": _value |
| 1488 |
}] |
| 1489 |
return [] |
| 1490 |
|
| 1491 |
def construct_the_data(self, operations, event_source): |
| 1492 |
"""Construct the operation data using helper methods. |
| 1493 |
|
| 1494 |
Args: |
| 1495 |
operation: operation to construct |
| 1496 |
|
| 1497 |
Returns: |
| 1498 |
list: constructed operation data |
| 1499 |
""" |
| 1500 |
constructed_data = [] |
| 1501 |
for op in operations: |
| 1502 |
operation_type = self._construct_operationtype(op) |
| 1503 |
constructed_operation = { |
| 1504 |
"operationtype": operation_type, |
| 1505 |
"esc_period": op.get("esc_period"), |
| 1506 |
"esc_step_from": op.get("esc_step_from"), |
| 1507 |
"esc_step_to": op.get("esc_step_to") |
| 1508 |
} |
| 1509 |
# Send Message type |
| 1510 |
if constructed_operation["operationtype"] == 0: |
| 1511 |
constructed_operation["opmessage"] = self._construct_opmessage(op) |
| 1512 |
constructed_operation["opmessage_usr"] = self._construct_opmessage_usr(op) |
| 1513 |
constructed_operation["opmessage_grp"] = self._construct_opmessage_grp(op) |
| 1514 |
if event_source == "trigger": |
| 1515 |
# opconditions valid only for "trigger" action |
| 1516 |
constructed_operation["opconditions"] = self._construct_opconditions(op) |
| 1517 |
|
| 1518 |
# Send Command type |
| 1519 |
if constructed_operation["operationtype"] == 1: |
| 1520 |
constructed_operation["opcommand"] = self._construct_opcommand(op) |
| 1521 |
constructed_operation["opcommand_hst"] = self._construct_opcommand_hst(op) |
| 1522 |
constructed_operation["opcommand_grp"] = self._construct_opcommand_grp(op) |
| 1523 |
if event_source == "trigger": |
| 1524 |
# opconditions valid only for "trigger" action |
| 1525 |
constructed_operation["opconditions"] = self._construct_opconditions(op) |
| 1526 |
|
| 1527 |
# Add to/Remove from host group |
| 1528 |
if constructed_operation["operationtype"] in (4, 5): |
| 1529 |
constructed_operation["opgroup"] = self._construct_opgroup(op) |
| 1530 |
|
| 1531 |
if LooseVersion(self._zbx_api_version) >= LooseVersion("7.0"): |
| 1532 |
# Add/Remove tags |
| 1533 |
if constructed_operation["operationtype"] in (13, 14): |
| 1534 |
constructed_operation["optag"] = op["tags"] |
| 1535 |
|
| 1536 |
# Link/Unlink template |
| 1537 |
if constructed_operation["operationtype"] in (6, 7): |
| 1538 |
constructed_operation["optemplate"] = self._construct_optemplate(op) |
| 1539 |
|
| 1540 |
# Set inventory mode |
| 1541 |
if constructed_operation["operationtype"] == 10: |
| 1542 |
constructed_operation["opinventory"] = self._construct_opinventory(op) |
| 1543 |
|
| 1544 |
# Remove escalation params when for event sources where they are not applicable |
| 1545 |
if event_source in ["trigger", "internal"]: |
| 1546 |
if isinstance(constructed_operation.get("esc_period"), type(None)): |
| 1547 |
constructed_operation["esc_period"] = 0 |
| 1548 |
else: |
| 1549 |
constructed_operation.pop("esc_period") |
| 1550 |
constructed_operation.pop("esc_step_from") |
| 1551 |
constructed_operation.pop("esc_step_to") |
| 1552 |
|
| 1553 |
constructed_data.append(constructed_operation) |
| 1554 |
|
| 1555 |
return zabbix_utils.helper_cleanup_data(constructed_data) |
| 1556 |
|
| 1557 |
|
| 1558 |
class RecoveryOperations(Operations): |
| 1559 |
""" |
| 1560 |
Restructures the user defined recovery operations data to fit the Zabbix API requirements |
| 1561 |
""" |
| 1562 |
|
| 1563 |
def _construct_operationtype(self, operation): |
| 1564 |
"""Construct operation type. |
| 1565 |
|
| 1566 |
Args: |
| 1567 |
operation: operation to construct type |
| 1568 |
|
| 1569 |
Returns: |
| 1570 |
str: constructed operation type |
| 1571 |
""" |
| 1572 |
try: |
| 1573 |
return zabbix_utils.helper_to_numeric_value([ |
| 1574 |
"send_message", |
| 1575 |
"remote_command", |
| 1576 |
None, |
| 1577 |
None, |
| 1578 |
None, |
| 1579 |
None, |
| 1580 |
None, |
| 1581 |
None, |
| 1582 |
None, |
| 1583 |
None, |
| 1584 |
None, |
| 1585 |
"notify_all_involved"], operation["type"] |
| 1586 |
) |
| 1587 |
except Exception: |
| 1588 |
self._module.fail_json(msg="Unsupported value '%s' for recovery operation type." % operation["type"]) |
| 1589 |
|
| 1590 |
def construct_the_data(self, operations): |
| 1591 |
"""Construct the recovery operations data using helper methods. |
| 1592 |
|
| 1593 |
Args: |
| 1594 |
operation: operation to construct |
| 1595 |
|
| 1596 |
Returns: |
| 1597 |
list: constructed recovery operations data |
| 1598 |
""" |
| 1599 |
constructed_data = [] |
| 1600 |
for op in operations: |
| 1601 |
operation_type = self._construct_operationtype(op) |
| 1602 |
constructed_operation = { |
| 1603 |
"operationtype": operation_type, |
| 1604 |
} |
| 1605 |
|
| 1606 |
# Send Message type |
| 1607 |
if constructed_operation["operationtype"] == 0: |
| 1608 |
constructed_operation["opmessage"] = self._construct_opmessage(op) |
| 1609 |
constructed_operation["opmessage_usr"] = self._construct_opmessage_usr(op) |
| 1610 |
constructed_operation["opmessage_grp"] = self._construct_opmessage_grp(op) |
| 1611 |
|
| 1612 |
if constructed_operation["operationtype"] == 11: |
| 1613 |
constructed_operation["opmessage"] = self._construct_opmessage(op) |
| 1614 |
constructed_operation["opmessage"].pop("mediatypeid") |
| 1615 |
|
| 1616 |
# Send Command type |
| 1617 |
if constructed_operation["operationtype"] == 1: |
| 1618 |
constructed_operation["opcommand"] = self._construct_opcommand(op) |
| 1619 |
constructed_operation["opcommand_hst"] = self._construct_opcommand_hst(op) |
| 1620 |
constructed_operation["opcommand_grp"] = self._construct_opcommand_grp(op) |
| 1621 |
|
| 1622 |
constructed_data.append(constructed_operation) |
| 1623 |
|
| 1624 |
return zabbix_utils.helper_cleanup_data(constructed_data) |
| 1625 |
|
| 1626 |
|
| 1627 |
class AcknowledgeOperations(Operations): |
| 1628 |
""" |
| 1629 |
Restructures the user defined acknowledge operations data to fit the Zabbix API requirements |
| 1630 |
""" |
| 1631 |
|
| 1632 |
def _construct_operationtype(self, operation): |
| 1633 |
"""Construct operation type. |
| 1634 |
|
| 1635 |
Args: |
| 1636 |
operation: operation to construct type |
| 1637 |
|
| 1638 |
Returns: |
| 1639 |
str: constructed operation type |
| 1640 |
""" |
| 1641 |
try: |
| 1642 |
return zabbix_utils.helper_to_numeric_value([ |
| 1643 |
"send_message", |
| 1644 |
"remote_command", |
| 1645 |
None, |
| 1646 |
None, |
| 1647 |
None, |
| 1648 |
None, |
| 1649 |
None, |
| 1650 |
None, |
| 1651 |
None, |
| 1652 |
None, |
| 1653 |
None, |
| 1654 |
None, |
| 1655 |
"notify_all_involved"], operation["type"] |
| 1656 |
) |
| 1657 |
except Exception: |
| 1658 |
self._module.fail_json(msg="Unsupported value '%s' for acknowledge operation type." % operation["type"]) |
| 1659 |
|
| 1660 |
def construct_the_data(self, operations): |
| 1661 |
"""Construct the acknowledge operations data using helper methods. |
| 1662 |
|
| 1663 |
Args: |
| 1664 |
operation: operation to construct |
| 1665 |
|
| 1666 |
Returns: |
| 1667 |
list: constructed acknowledge operations data |
| 1668 |
""" |
| 1669 |
constructed_data = [] |
| 1670 |
for op in operations: |
| 1671 |
operation_type = self._construct_operationtype(op) |
| 1672 |
constructed_operation = { |
| 1673 |
"operationtype": operation_type, |
| 1674 |
} |
| 1675 |
|
| 1676 |
# Send Message type |
| 1677 |
if constructed_operation["operationtype"] == 0: |
| 1678 |
constructed_operation["opmessage"] = self._construct_opmessage(op) |
| 1679 |
constructed_operation["opmessage_usr"] = self._construct_opmessage_usr(op) |
| 1680 |
constructed_operation["opmessage_grp"] = self._construct_opmessage_grp(op) |
| 1681 |
|
| 1682 |
if constructed_operation["operationtype"] == 12: |
| 1683 |
constructed_operation["opmessage"] = self._construct_opmessage(op) |
| 1684 |
constructed_operation["opmessage"].pop("mediatypeid") |
| 1685 |
|
| 1686 |
# Send Command type |
| 1687 |
if constructed_operation["operationtype"] == 1: |
| 1688 |
constructed_operation["opcommand"] = self._construct_opcommand(op) |
| 1689 |
constructed_operation["opcommand_hst"] = self._construct_opcommand_hst(op) |
| 1690 |
constructed_operation["opcommand_grp"] = self._construct_opcommand_grp(op) |
| 1691 |
|
| 1692 |
constructed_data.append(constructed_operation) |
| 1693 |
|
| 1694 |
return zabbix_utils.helper_cleanup_data(constructed_data) |
| 1695 |
|
| 1696 |
|
| 1697 |
class Filter(Zapi): |
| 1698 |
def _construct_evaltype(self, _eval_type, _formula, _conditions): |
| 1699 |
"""Construct the eval type |
| 1700 |
|
| 1701 |
Args: |
| 1702 |
_formula: zabbix condition evaluation formula |
| 1703 |
_conditions: list of conditions to check |
| 1704 |
|
| 1705 |
Returns: |
| 1706 |
dict: constructed acknowledge operations data |
| 1707 |
""" |
| 1708 |
if len(_conditions) <= 1: |
| 1709 |
return { |
| 1710 |
"evaltype": "0", |
| 1711 |
"formula": None |
| 1712 |
} |
| 1713 |
if _eval_type == "andor": |
| 1714 |
return { |
| 1715 |
"evaltype": "0", |
| 1716 |
"formula": None |
| 1717 |
} |
| 1718 |
if _eval_type == "and": |
| 1719 |
return { |
| 1720 |
"evaltype": "1", |
| 1721 |
"formula": None |
| 1722 |
} |
| 1723 |
if _eval_type == "or": |
| 1724 |
return { |
| 1725 |
"evaltype": "2", |
| 1726 |
"formula": None |
| 1727 |
} |
| 1728 |
if _eval_type == "custom_expression": |
| 1729 |
if _formula is not None: |
| 1730 |
return { |
| 1731 |
"evaltype": "3", |
| 1732 |
"formula": _formula |
| 1733 |
} |
| 1734 |
else: |
| 1735 |
self._module.fail_json(msg="'formula' is required when 'eval_type' is set to 'custom_expression'") |
| 1736 |
if _formula is not None: |
| 1737 |
return { |
| 1738 |
"evaltype": "3", |
| 1739 |
"formula": _formula |
| 1740 |
} |
| 1741 |
return { |
| 1742 |
"evaltype": "0", |
| 1743 |
"formula": None |
| 1744 |
} |
| 1745 |
|
| 1746 |
def _construct_conditiontype(self, _condition): |
| 1747 |
"""Construct the condition type |
| 1748 |
|
| 1749 |
Args: |
| 1750 |
_condition: condition to check |
| 1751 |
|
| 1752 |
Returns: |
| 1753 |
str: constructed condition type data |
| 1754 |
""" |
| 1755 |
try: |
| 1756 |
return zabbix_utils.helper_to_numeric_value([ |
| 1757 |
"host_group", |
| 1758 |
"host", |
| 1759 |
"trigger", |
| 1760 |
"trigger_name", |
| 1761 |
"trigger_severity", |
| 1762 |
"trigger_value", |
| 1763 |
"time_period", |
| 1764 |
"host_ip", |
| 1765 |
"discovered_service_type", |
| 1766 |
"discovered_service_port", |
| 1767 |
"discovery_status", |
| 1768 |
"uptime_or_downtime_duration", |
| 1769 |
"received_value", |
| 1770 |
"host_template", |
| 1771 |
None, |
| 1772 |
None, |
| 1773 |
"maintenance_status", |
| 1774 |
None, |
| 1775 |
"discovery_rule", |
| 1776 |
"discovery_check", |
| 1777 |
"proxy", |
| 1778 |
"discovery_object", |
| 1779 |
"host_name", |
| 1780 |
"event_type", |
| 1781 |
"host_metadata", |
| 1782 |
"event_tag", |
| 1783 |
"event_tag_value"], _condition["type"] |
| 1784 |
) |
| 1785 |
except Exception: |
| 1786 |
self._module.fail_json(msg="Unsupported value '%s' for condition type." % _condition["type"]) |
| 1787 |
|
| 1788 |
def _construct_operator(self, _condition): |
| 1789 |
"""Construct operator |
| 1790 |
|
| 1791 |
Args: |
| 1792 |
_condition: condition to construct |
| 1793 |
|
| 1794 |
Returns: |
| 1795 |
str: constructed operator |
| 1796 |
""" |
| 1797 |
try: |
| 1798 |
return zabbix_utils.helper_to_numeric_value([ |
| 1799 |
["equals", "="], |
| 1800 |
["does not equal", "<>"], |
| 1801 |
["contains", "like"], |
| 1802 |
["does not contain", "not like"], |
| 1803 |
"in", |
| 1804 |
["is greater than or equals", ">="], |
| 1805 |
["is less than or equals", "<="], |
| 1806 |
"not in", |
| 1807 |
"matches", |
| 1808 |
"does not match", |
| 1809 |
"Yes", |
| 1810 |
"No"], _condition["operator"] |
| 1811 |
) |
| 1812 |
except Exception: |
| 1813 |
self._module.fail_json(msg="Unsupported value '%s' for operator." % _condition["operator"]) |
| 1814 |
|
| 1815 |
def _construct_value(self, conditiontype, value): |
| 1816 |
"""Construct operator |
| 1817 |
|
| 1818 |
Args: |
| 1819 |
conditiontype: type of condition to construct |
| 1820 |
value: value to construct |
| 1821 |
|
| 1822 |
Returns: |
| 1823 |
str: constructed value |
| 1824 |
""" |
| 1825 |
try: |
| 1826 |
# Host group |
| 1827 |
if conditiontype == 0: |
| 1828 |
return self._zapi_wrapper.get_hostgroup_by_hostgroup_name(value)["groupid"] |
| 1829 |
# Host |
| 1830 |
if conditiontype == 1: |
| 1831 |
return self._zapi_wrapper.get_host_by_host_name(value)["hostid"] |
| 1832 |
# Trigger |
| 1833 |
if conditiontype == 2: |
| 1834 |
return self._zapi_wrapper.get_trigger_by_trigger_name(value)["triggerid"] |
| 1835 |
# Trigger name: return as is |
| 1836 |
# Trigger severity |
| 1837 |
if conditiontype == 4: |
| 1838 |
return zabbix_utils.helper_to_numeric_value([ |
| 1839 |
"not classified", |
| 1840 |
"information", |
| 1841 |
"warning", |
| 1842 |
"average", |
| 1843 |
"high", |
| 1844 |
"disaster"], value or "not classified" |
| 1845 |
) |
| 1846 |
|
| 1847 |
# Trigger value |
| 1848 |
if conditiontype == 5: |
| 1849 |
return zabbix_utils.helper_to_numeric_value([ |
| 1850 |
"ok", |
| 1851 |
"problem"], value or "ok" |
| 1852 |
) |
| 1853 |
# Time period: return as is |
| 1854 |
# Host IP: return as is |
| 1855 |
# Discovered service type |
| 1856 |
if conditiontype == 8: |
| 1857 |
return zabbix_utils.helper_to_numeric_value([ |
| 1858 |
"SSH", |
| 1859 |
"LDAP", |
| 1860 |
"SMTP", |
| 1861 |
"FTP", |
| 1862 |
"HTTP", |
| 1863 |
"POP", |
| 1864 |
"NNTP", |
| 1865 |
"IMAP", |
| 1866 |
"TCP", |
| 1867 |
"Zabbix agent", |
| 1868 |
"SNMPv1 agent", |
| 1869 |
"SNMPv2 agent", |
| 1870 |
"ICMP ping", |
| 1871 |
"SNMPv3 agent", |
| 1872 |
"HTTPS", |
| 1873 |
"Telnet"], value |
| 1874 |
) |
| 1875 |
# Discovered service port: return as is |
| 1876 |
# Discovery status |
| 1877 |
if conditiontype == 10: |
| 1878 |
return zabbix_utils.helper_to_numeric_value([ |
| 1879 |
"up", |
| 1880 |
"down", |
| 1881 |
"discovered", |
| 1882 |
"lost"], value |
| 1883 |
) |
| 1884 |
if conditiontype == 13: |
| 1885 |
return self._zapi_wrapper.get_template_by_template_name(value)["templateid"] |
| 1886 |
# maintenance_status |
| 1887 |
if conditiontype == 16: |
| 1888 |
return zabbix_utils.helper_to_numeric_value([ |
| 1889 |
"Yes", |
| 1890 |
"No"], value |
| 1891 |
) |
| 1892 |
if conditiontype == 18: |
| 1893 |
return self._zapi_wrapper.get_discovery_rule_by_discovery_rule_name(value)["druleid"] |
| 1894 |
if conditiontype == 19: |
| 1895 |
return self._zapi_wrapper.get_discovery_check_by_discovery_check_name(value)["dcheckid"] |
| 1896 |
if conditiontype == 20: |
| 1897 |
return self._zapi_wrapper.get_proxy_by_proxy_name(value)["proxyid"] |
| 1898 |
if conditiontype == 21: |
| 1899 |
return zabbix_utils.helper_to_numeric_value([ |
| 1900 |
"pchldrfor0", |
| 1901 |
"host", |
| 1902 |
"service"], value |
| 1903 |
) |
| 1904 |
if conditiontype == 23: |
| 1905 |
return zabbix_utils.helper_to_numeric_value([ |
| 1906 |
"item in not supported state", |
| 1907 |
"item in normal state", |
| 1908 |
"LLD rule in not supported state", |
| 1909 |
"LLD rule in normal state", |
| 1910 |
"trigger in unknown state", |
| 1911 |
"trigger in normal state"], value |
| 1912 |
) |
| 1913 |
return value |
| 1914 |
except Exception: |
| 1915 |
self._module.fail_json( |
| 1916 |
msg="""Unsupported value '%s' for specified condition type. |
| 1917 |
Check out Zabbix API documentation for supported values for |
| 1918 |
condition type '%s' at |
| 1919 |
https://www.zabbix.com/documentation/3.4/manual/api/reference/action/object#action_filter_condition""" % (value, conditiontype) |
| 1920 |
) |
| 1921 |
|
| 1922 |
def construct_the_data(self, _eval_type, _formula, _conditions): |
| 1923 |
"""Construct the user defined filter conditions to fit the Zabbix API |
| 1924 |
requirements operations data using helper methods. |
| 1925 |
|
| 1926 |
Args: |
| 1927 |
_formula: zabbix condition evaluation formula |
| 1928 |
_conditions: conditions to construct |
| 1929 |
|
| 1930 |
Returns: |
| 1931 |
dict: user defined filter conditions |
| 1932 |
""" |
| 1933 |
if _conditions is None: |
| 1934 |
return None |
| 1935 |
constructed_data = {} |
| 1936 |
constructed_data["conditions"] = [] |
| 1937 |
for cond in _conditions: |
| 1938 |
condition_type = self._construct_conditiontype(cond) |
| 1939 |
constructed_data["conditions"].append({ |
| 1940 |
"conditiontype": condition_type, |
| 1941 |
"value": self._construct_value(condition_type, cond.get("value")), |
| 1942 |
"value2": cond.get("value2"), |
| 1943 |
"formulaid": cond.get("formulaid"), |
| 1944 |
"operator": self._construct_operator(cond) |
| 1945 |
}) |
| 1946 |
_constructed_evaltype = self._construct_evaltype( |
| 1947 |
_eval_type, |
| 1948 |
_formula, |
| 1949 |
constructed_data["conditions"] |
| 1950 |
) |
| 1951 |
constructed_data["evaltype"] = _constructed_evaltype["evaltype"] |
| 1952 |
constructed_data["formula"] = _constructed_evaltype["formula"] |
| 1953 |
return zabbix_utils.helper_cleanup_data(constructed_data) |
| 1954 |
|
| 1955 |
|
| 1956 |
def main(): |
| 1957 |
"""Main ansible module function |
| 1958 |
""" |
| 1959 |
|
| 1960 |
argument_spec = zabbix_utils.zabbix_common_argument_spec() |
| 1961 |
argument_spec.update(dict( |
| 1962 |
esc_period=dict(type="str", required=False), |
| 1963 |
name=dict(type="str", required=True), |
| 1964 |
event_source=dict(type="str", required=False, choices=["trigger", "discovery", "auto_registration", "internal"]), |
| 1965 |
state=dict(type="str", required=False, default="present", choices=["present", "absent"]), |
| 1966 |
status=dict(type="str", required=False, default="enabled", choices=["enabled", "disabled"]), |
| 1967 |
pause_in_maintenance=dict(type="bool", required=False, default=True), |
| 1968 |
conditions=dict( |
| 1969 |
type="list", |
| 1970 |
required=False, |
| 1971 |
default=[], |
| 1972 |
elements="dict", |
| 1973 |
options=dict( |
| 1974 |
formulaid=dict(type="str", required=False), |
| 1975 |
operator=dict( |
| 1976 |
type="str", |
| 1977 |
required=True, |
| 1978 |
choices=[ |
| 1979 |
"equals", |
| 1980 |
"=", |
| 1981 |
"does not equal", |
| 1982 |
"<>", |
| 1983 |
"contains", |
| 1984 |
"like", |
| 1985 |
"does not contain", |
| 1986 |
"not like", |
| 1987 |
"in", |
| 1988 |
"is greater than or equals", |
| 1989 |
">=", |
| 1990 |
"is less than or equals", |
| 1991 |
"<=", |
| 1992 |
"not in", |
| 1993 |
"matches", |
| 1994 |
"does not match", |
| 1995 |
"Yes", |
| 1996 |
"No" |
| 1997 |
] |
| 1998 |
), |
| 1999 |
type=dict(type="str", required=True), |
| 2000 |
value=dict(type="str", required=False), |
| 2001 |
value2=dict(type="str", required=False) |
| 2002 |
), |
| 2003 |
required_if=[ |
| 2004 |
["type", "event_tag_value", ["value2"]], |
| 2005 |
] |
| 2006 |
), |
| 2007 |
formula=dict(type="str", required=False, default=None), |
| 2008 |
eval_type=dict(type="str", required=False, default=None, choices=["andor", "and", "or", "custom_expression"]), |
| 2009 |
operations=dict( |
| 2010 |
type="list", |
| 2011 |
required=False, |
| 2012 |
default=[], |
| 2013 |
elements="dict", |
| 2014 |
options=dict( |
| 2015 |
type=dict( |
| 2016 |
type="str", |
| 2017 |
required=True, |
| 2018 |
choices=[ |
| 2019 |
"send_message", |
| 2020 |
"remote_command", |
| 2021 |
"add_host", |
| 2022 |
"remove_host", |
| 2023 |
"add_to_host_group", |
| 2024 |
"remove_from_host_group", |
| 2025 |
"link_to_template", |
| 2026 |
"unlink_from_template", |
| 2027 |
"enable_host", |
| 2028 |
"disable_host", |
| 2029 |
"set_host_inventory_mode", |
| 2030 |
"notify_all_involved", |
| 2031 |
"add_host_tags", |
| 2032 |
"remove_host_tags" |
| 2033 |
] |
| 2034 |
), |
| 2035 |
esc_period=dict(type="str", required=False, default="0s"), |
| 2036 |
esc_step_from=dict(type="int", required=False, default=1), |
| 2037 |
esc_step_to=dict(type="int", required=False, default=1), |
| 2038 |
operation_condition=dict( |
| 2039 |
type="str", |
| 2040 |
required=False, |
| 2041 |
default=None, |
| 2042 |
choices=["acknowledged", "not_acknowledged"] |
| 2043 |
), |
| 2044 |
# when type is remote_command |
| 2045 |
command_type=dict( |
| 2046 |
type="str", |
| 2047 |
required=False, |
| 2048 |
choices=[ |
| 2049 |
"custom_script", |
| 2050 |
"ipmi", |
| 2051 |
"ssh", |
| 2052 |
"telnet", |
| 2053 |
"global_script" |
| 2054 |
] |
| 2055 |
), |
| 2056 |
command=dict(type="str", required=False), |
| 2057 |
execute_on=dict( |
| 2058 |
type="str", |
| 2059 |
required=False, |
| 2060 |
choices=["agent", "server", "proxy"] |
| 2061 |
), |
| 2062 |
password=dict(type="str", required=False, no_log=True), |
| 2063 |
port=dict(type="int", required=False), |
| 2064 |
run_on_groups=dict(type="list", required=False, elements="str"), |
| 2065 |
run_on_hosts=dict(type="list", required=False, elements="str"), |
| 2066 |
script_name=dict(type="str", required=False), |
| 2067 |
ssh_auth_type=dict(type="str", required=False, choices=["password", "public_key"]), |
| 2068 |
ssh_privatekey_file=dict(type="str", required=False), |
| 2069 |
ssh_publickey_file=dict(type="str", required=False), |
| 2070 |
username=dict(type="str", required=False), |
| 2071 |
# when type is send_message |
| 2072 |
media_type=dict(type="str", required=False, default="all"), |
| 2073 |
subject=dict(type="str", required=False), |
| 2074 |
op_message=dict(type="str", required=False), |
| 2075 |
send_to_groups=dict(type="list", required=False, elements="str"), |
| 2076 |
send_to_users=dict(type="list", required=False, elements="str"), |
| 2077 |
# when type is add_to_host_group or remove_from_host_group |
| 2078 |
host_groups=dict(type="list", required=False, elements="str"), |
| 2079 |
# when type is set_host_inventory_mode |
| 2080 |
inventory=dict(type="str", required=False, choices=["manual", "automatic"]), |
| 2081 |
# when type is link_to_template or unlink_from_template |
| 2082 |
templates=dict(type="list", required=False, elements="str"), |
| 2083 |
tags=dict(type="list", required=False, elements="dict") |
| 2084 |
), |
| 2085 |
required_if=[ |
| 2086 |
["type", "remote_command", ["command_type"]], |
| 2087 |
["type", "remote_command", ["run_on_groups", "run_on_hosts"], True], |
| 2088 |
["command_type", "custom_script", ["command", "execute_on"]], |
| 2089 |
["command_type", "ipmi", ["command"]], |
| 2090 |
["command_type", "ssh", ["command", "ssh_auth_type"]], |
| 2091 |
["ssh_auth_type", "password", ["username", "password"]], |
| 2092 |
["ssh_auth_type", "public_key", ["username", "ssh_privatekey_file", "ssh_publickey_file"]], |
| 2093 |
["command_type", "telnet", ["command", "username", "password"]], |
| 2094 |
["command_type", "global_script", ["script_name"]], |
| 2095 |
["type", "add_to_host_group", ["host_groups"]], |
| 2096 |
["type", "remove_from_host_group", ["host_groups"]], |
| 2097 |
["type", "link_to_template", ["templates"]], |
| 2098 |
["type", "unlink_from_template", ["templates"]], |
| 2099 |
["type", "set_host_inventory_mode", ["inventory"]], |
| 2100 |
["type", "send_message", ["send_to_users", "send_to_groups"], True], |
| 2101 |
["type", "add_host_tags", ["tags"], True], |
| 2102 |
["type", "remove_host_tags", ["tags"], True] |
| 2103 |
] |
| 2104 |
), |
| 2105 |
recovery_operations=dict( |
| 2106 |
type="list", |
| 2107 |
required=False, |
| 2108 |
default=[], |
| 2109 |
elements="dict", |
| 2110 |
options=dict( |
| 2111 |
type=dict( |
| 2112 |
type="str", |
| 2113 |
required=True, |
| 2114 |
choices=[ |
| 2115 |
"send_message", |
| 2116 |
"remote_command", |
| 2117 |
"notify_all_involved" |
| 2118 |
] |
| 2119 |
), |
| 2120 |
# when type is remote_command |
| 2121 |
command_type=dict( |
| 2122 |
type="str", |
| 2123 |
required=False, |
| 2124 |
choices=[ |
| 2125 |
"custom_script", |
| 2126 |
"ipmi", |
| 2127 |
"ssh", |
| 2128 |
"telnet", |
| 2129 |
"global_script" |
| 2130 |
] |
| 2131 |
), |
| 2132 |
command=dict(type="str", required=False), |
| 2133 |
execute_on=dict( |
| 2134 |
type="str", |
| 2135 |
required=False, |
| 2136 |
choices=["agent", "server", "proxy"] |
| 2137 |
), |
| 2138 |
password=dict(type="str", required=False, no_log=True), |
| 2139 |
port=dict(type="int", required=False), |
| 2140 |
run_on_groups=dict(type="list", required=False, elements="str"), |
| 2141 |
run_on_hosts=dict(type="list", required=False, elements="str"), |
| 2142 |
script_name=dict(type="str", required=False), |
| 2143 |
ssh_auth_type=dict(type="str", required=False, choices=["password", "public_key"]), |
| 2144 |
ssh_privatekey_file=dict(type="str", required=False), |
| 2145 |
ssh_publickey_file=dict(type="str", required=False), |
| 2146 |
username=dict(type="str", required=False), |
| 2147 |
# when type is send_message |
| 2148 |
media_type=dict(type="str", required=False, default="all"), |
| 2149 |
subject=dict(type="str", required=False), |
| 2150 |
op_message=dict(type="str", required=False), |
| 2151 |
send_to_groups=dict(type="list", required=False, elements="str"), |
| 2152 |
send_to_users=dict(type="list", required=False, elements="str"), |
| 2153 |
), |
| 2154 |
required_if=[ |
| 2155 |
["type", "remote_command", ["command_type"]], |
| 2156 |
["type", "remote_command", [ |
| 2157 |
"run_on_groups", |
| 2158 |
"run_on_hosts" |
| 2159 |
], True], |
| 2160 |
["command_type", "custom_script", [ |
| 2161 |
"command", |
| 2162 |
"execute_on" |
| 2163 |
]], |
| 2164 |
["command_type", "ipmi", ["command"]], |
| 2165 |
["command_type", "ssh", ["command", "ssh_auth_type"]], |
| 2166 |
["ssh_auth_type", "password", ["username", "password"]], |
| 2167 |
["ssh_auth_type", "public_key", ["username", "ssh_privatekey_file", "ssh_publickey_file"]], |
| 2168 |
["command_type", "telnet", ["command", "username", "password"]], |
| 2169 |
["command_type", "global_script", ["script_name"]], |
| 2170 |
["type", "send_message", ["send_to_users", "send_to_groups"], True] |
| 2171 |
] |
| 2172 |
), |
| 2173 |
acknowledge_operations=dict( |
| 2174 |
type="list", |
| 2175 |
required=False, |
| 2176 |
default=[], |
| 2177 |
elements="dict", |
| 2178 |
aliases=["update_operations"], |
| 2179 |
options=dict( |
| 2180 |
type=dict( |
| 2181 |
type="str", |
| 2182 |
required=True, |
| 2183 |
choices=[ |
| 2184 |
"send_message", |
| 2185 |
"remote_command", |
| 2186 |
"notify_all_involved" |
| 2187 |
] |
| 2188 |
), |
| 2189 |
# when type is remote_command |
| 2190 |
command_type=dict( |
| 2191 |
type="str", |
| 2192 |
required=False, |
| 2193 |
choices=[ |
| 2194 |
"custom_script", |
| 2195 |
"ipmi", |
| 2196 |
"ssh", |
| 2197 |
"telnet", |
| 2198 |
"global_script" |
| 2199 |
] |
| 2200 |
), |
| 2201 |
command=dict(type="str", required=False), |
| 2202 |
execute_on=dict( |
| 2203 |
type="str", |
| 2204 |
required=False, |
| 2205 |
choices=["agent", "server", "proxy"] |
| 2206 |
), |
| 2207 |
password=dict(type="str", required=False, no_log=True), |
| 2208 |
port=dict(type="int", required=False), |
| 2209 |
run_on_groups=dict(type="list", required=False, elements="str"), |
| 2210 |
run_on_hosts=dict(type="list", required=False, elements="str"), |
| 2211 |
script_name=dict(type="str", required=False), |
| 2212 |
ssh_auth_type=dict(type="str", required=False, choices=["password", "public_key"]), |
| 2213 |
ssh_privatekey_file=dict(type="str", required=False), |
| 2214 |
ssh_publickey_file=dict(type="str", required=False), |
| 2215 |
username=dict(type="str", required=False), |
| 2216 |
# when type is send_message |
| 2217 |
media_type=dict(type="str", required=False, default="all"), |
| 2218 |
subject=dict(type="str", required=False), |
| 2219 |
op_message=dict(type="str", required=False), |
| 2220 |
send_to_groups=dict(type="list", required=False, elements="str"), |
| 2221 |
send_to_users=dict(type="list", required=False, elements="str"), |
| 2222 |
), |
| 2223 |
required_if=[ |
| 2224 |
["type", "remote_command", ["command_type"]], |
| 2225 |
["type", "remote_command", [ |
| 2226 |
"run_on_groups", |
| 2227 |
"run_on_hosts" |
| 2228 |
], True], |
| 2229 |
["command_type", "custom_script", [ |
| 2230 |
"command", |
| 2231 |
"execute_on" |
| 2232 |
]], |
| 2233 |
["command_type", "ipmi", ["command"]], |
| 2234 |
["command_type", "ssh", ["command", "ssh_auth_type"]], |
| 2235 |
["ssh_auth_type", "password", ["username", "password"]], |
| 2236 |
["ssh_auth_type", "public_key", ["username", "ssh_privatekey_file", "ssh_publickey_file"]], |
| 2237 |
["command_type", "telnet", ["command", "username", "password"]], |
| 2238 |
["command_type", "global_script", ["script_name"]], |
| 2239 |
["type", "send_message", ["send_to_users", "send_to_groups"], True] |
| 2240 |
] |
| 2241 |
), |
| 2242 |
pause_symptoms=dict(type="bool", required=False, default=True), |
| 2243 |
notify_if_canceled=dict(type="bool", required=False, default=True) |
| 2244 |
)) |
| 2245 |
module = AnsibleModule( |
| 2246 |
argument_spec=argument_spec, |
| 2247 |
required_if=[ |
| 2248 |
["state", "present", [ |
| 2249 |
"event_source" |
| 2250 |
]] |
| 2251 |
], |
| 2252 |
supports_check_mode=True |
| 2253 |
) |
| 2254 |
|
| 2255 |
name = module.params["name"] |
| 2256 |
esc_period = module.params["esc_period"] |
| 2257 |
event_source = module.params["event_source"] |
| 2258 |
state = module.params["state"] |
| 2259 |
status = module.params["status"] |
| 2260 |
pause_in_maintenance = module.params["pause_in_maintenance"] |
| 2261 |
conditions = module.params["conditions"] |
| 2262 |
formula = module.params["formula"] |
| 2263 |
eval_type = module.params["eval_type"] |
| 2264 |
operations = module.params["operations"] |
| 2265 |
recovery_operations = module.params["recovery_operations"] |
| 2266 |
acknowledge_operations = module.params["acknowledge_operations"] |
| 2267 |
pause_symptoms = module.params["pause_symptoms"] |
| 2268 |
notify_if_canceled = module.params["notify_if_canceled"] |
| 2269 |
|
| 2270 |
zapi_wrapper = Zapi(module) |
| 2271 |
action = Action(module) |
| 2272 |
|
| 2273 |
action_exists = zapi_wrapper.check_if_action_exists(name) |
| 2274 |
ops = Operations(module, zapi_wrapper) |
| 2275 |
recovery_ops = RecoveryOperations(module, zapi_wrapper) |
| 2276 |
acknowledge_ops = AcknowledgeOperations(module, zapi_wrapper) |
| 2277 |
fltr = Filter(module, zapi_wrapper) |
| 2278 |
|
| 2279 |
if action_exists: |
| 2280 |
action_id = zapi_wrapper.get_action_by_name(name)["actionid"] |
| 2281 |
if state == "absent": |
| 2282 |
result = action.delete_action(action_id) |
| 2283 |
module.exit_json(changed=True, msg="Action Deleted: %s, ID: %s" % (name, result)) |
| 2284 |
else: |
| 2285 |
kwargs = dict( |
| 2286 |
action_id=action_id, |
| 2287 |
name=name, |
| 2288 |
event_source=event_source, |
| 2289 |
esc_period=esc_period, |
| 2290 |
status=status, |
| 2291 |
pause_in_maintenance=pause_in_maintenance, |
| 2292 |
operations=ops.construct_the_data(operations, event_source), |
| 2293 |
recovery_operations=recovery_ops.construct_the_data(recovery_operations), |
| 2294 |
conditions=fltr.construct_the_data(eval_type, formula, conditions), |
| 2295 |
notify_if_canceled=notify_if_canceled |
| 2296 |
) |
| 2297 |
|
| 2298 |
if LooseVersion(zapi_wrapper._zbx_api_version) >= LooseVersion("7.0"): |
| 2299 |
kwargs["pause_symptoms"] = pause_symptoms |
| 2300 |
|
| 2301 |
kwargs[argument_spec["acknowledge_operations"]["aliases"][0]] = acknowledge_ops.construct_the_data(acknowledge_operations) |
| 2302 |
|
| 2303 |
difference = action.check_difference(**kwargs) |
| 2304 |
|
| 2305 |
if difference == {}: |
| 2306 |
module.exit_json(changed=False, msg="Action is up to date: %s" % (name)) |
| 2307 |
else: |
| 2308 |
result = action.update_action( |
| 2309 |
action_id=action_id, |
| 2310 |
**difference |
| 2311 |
) |
| 2312 |
module.exit_json(changed=True, msg="Action Updated: %s, ID: %s" % (name, result)) |
| 2313 |
else: |
| 2314 |
if state == "absent": |
| 2315 |
module.exit_json(changed=False) |
| 2316 |
else: |
| 2317 |
kwargs = dict( |
| 2318 |
name=name, |
| 2319 |
event_source=event_source, |
| 2320 |
esc_period=esc_period, |
| 2321 |
status=status, |
| 2322 |
pause_in_maintenance=pause_in_maintenance, |
| 2323 |
operations=ops.construct_the_data(operations, event_source), |
| 2324 |
recovery_operations=recovery_ops.construct_the_data(recovery_operations), |
| 2325 |
conditions=fltr.construct_the_data(eval_type, formula, conditions), |
| 2326 |
notify_if_canceled=notify_if_canceled |
| 2327 |
) |
| 2328 |
|
| 2329 |
kwargs[argument_spec["acknowledge_operations"]["aliases"][0]] = acknowledge_ops.construct_the_data(acknowledge_operations) |
| 2330 |
|
| 2331 |
if LooseVersion(zapi_wrapper._zbx_api_version) >= LooseVersion("7.0"): |
| 2332 |
kwargs["pause_symptoms"] = pause_symptoms |
| 2333 |
|
| 2334 |
action_id = action.add_action(**kwargs) |
| 2335 |
module.exit_json(changed=True, msg="Action created: %s, ID: %s" % (name, action_id)) |
| 2336 |
|
| 2337 |
|
| 2338 |
if __name__ == "__main__": |
| 2339 |
main() |