| 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 |
DOCUMENTATION = r''' |
| 10 |
--- |
| 11 |
module: zabbix_httptest |
| 12 |
short_description: Create/delete Zabbix httptests aka Web Scenarios |
| 13 |
description: |
| 14 |
- Create httptests if they do not exist. |
| 15 |
- Delete existing httptests if they exist. |
| 16 |
author: |
| 17 |
- "Trystan Mata (@tytan652)" |
| 18 |
requirements: |
| 19 |
- "python >= 3.9" |
| 20 |
|
| 21 |
options: |
| 22 |
state: |
| 23 |
description: |
| 24 |
- Create or delete httptest. |
| 25 |
required: false |
| 26 |
type: str |
| 27 |
default: "present" |
| 28 |
choices: [ "present", "absent" ] |
| 29 |
name: |
| 30 |
description: |
| 31 |
- Name of httptest to create or delete. |
| 32 |
required: true |
| 33 |
type: str |
| 34 |
host_name: |
| 35 |
description: |
| 36 |
- Name of host to add httptest to. |
| 37 |
required: true |
| 38 |
type: str |
| 39 |
params: |
| 40 |
description: |
| 41 |
- Parameters to create/update httptest with. |
| 42 |
- Required if state is "present". |
| 43 |
- Parameters as defined at https://www.zabbix.com/documentation/current/en/manual/api/reference/httptest/object |
| 44 |
- Additionally supported parameters are below. |
| 45 |
required: false |
| 46 |
type: dict |
| 47 |
suboptions: |
| 48 |
interval: |
| 49 |
description: |
| 50 |
- Update interval of the httptest. |
| 51 |
- Alias for "delay" in API docs. |
| 52 |
required: false |
| 53 |
type: str |
| 54 |
attempts: |
| 55 |
description: |
| 56 |
- Number of attempts before failing the httptest |
| 57 |
- Alias for "retries" in API docs |
| 58 |
required: false |
| 59 |
type: int |
| 60 |
agent: |
| 61 |
description: |
| 62 |
- User agent string used by the httptest. |
| 63 |
required: false |
| 64 |
type: str |
| 65 |
http_proxy: |
| 66 |
description: |
| 67 |
- HTTP(S) proxy used by the httptest |
| 68 |
required: false |
| 69 |
type: str |
| 70 |
variables: |
| 71 |
description: |
| 72 |
- httptest variables. |
| 73 |
- Overriden by I(steps) variables. |
| 74 |
required: false |
| 75 |
type: list |
| 76 |
elements: dict |
| 77 |
suboptions: |
| 78 |
name: |
| 79 |
description: |
| 80 |
- Name of the variable field. |
| 81 |
required: true |
| 82 |
type: str |
| 83 |
value: |
| 84 |
description: |
| 85 |
- Value of the variable field. |
| 86 |
required: true |
| 87 |
type: str |
| 88 |
headers: |
| 89 |
description: |
| 90 |
- HTTP headers used when performing the httptest. |
| 91 |
- Overriden by I(steps) headers. |
| 92 |
required: false |
| 93 |
type: list |
| 94 |
elements: dict |
| 95 |
suboptions: |
| 96 |
name: |
| 97 |
description: |
| 98 |
- Name of the header field. |
| 99 |
required: true |
| 100 |
type: str |
| 101 |
value: |
| 102 |
description: |
| 103 |
- Value of the header field. |
| 104 |
required: true |
| 105 |
type: str |
| 106 |
status: |
| 107 |
description: |
| 108 |
- Status of the httptest. |
| 109 |
required: false |
| 110 |
type: str |
| 111 |
choices: [ "enabled", "disabled" ] |
| 112 |
enabled: |
| 113 |
description: |
| 114 |
- Status of the httptest. |
| 115 |
- Overrides "status" in API docs. |
| 116 |
required: false |
| 117 |
type: bool |
| 118 |
new_name: |
| 119 |
description: |
| 120 |
- New name for httptest |
| 121 |
required: false |
| 122 |
type: str |
| 123 |
steps: |
| 124 |
description: |
| 125 |
- Scenario steps for the httptest |
| 126 |
- Required if state is "present" and creating an httptest. |
| 127 |
required: false |
| 128 |
type: list |
| 129 |
elements: dict |
| 130 |
suboptions: |
| 131 |
name: |
| 132 |
description: |
| 133 |
- Name of the scenario step. |
| 134 |
required: true |
| 135 |
type: str |
| 136 |
url: |
| 137 |
description: |
| 138 |
- URL to be checked. |
| 139 |
required: true |
| 140 |
type: str |
| 141 |
query_field: |
| 142 |
description: |
| 143 |
- Query fields used when performing the scenario step. |
| 144 |
required: false |
| 145 |
type: list |
| 146 |
elements: dict |
| 147 |
suboptions: |
| 148 |
name: |
| 149 |
description: |
| 150 |
- Name of the query field. |
| 151 |
required: true |
| 152 |
type: str |
| 153 |
value: |
| 154 |
description: |
| 155 |
- Value of the query field. |
| 156 |
required: true |
| 157 |
type: str |
| 158 |
posts: |
| 159 |
description: |
| 160 |
- POST variables for the scenario step. |
| 161 |
required: false |
| 162 |
type: list |
| 163 |
elements: dict |
| 164 |
suboptions: |
| 165 |
name: |
| 166 |
description: |
| 167 |
- Name of the POST field. |
| 168 |
required: true |
| 169 |
type: str |
| 170 |
value: |
| 171 |
description: |
| 172 |
- Value of the POST field. |
| 173 |
required: true |
| 174 |
type: str |
| 175 |
variables: |
| 176 |
description: |
| 177 |
- Variables used when performing the scenario step. |
| 178 |
- Overrides I(params) variables. |
| 179 |
required: false |
| 180 |
type: list |
| 181 |
elements: dict |
| 182 |
suboptions: |
| 183 |
name: |
| 184 |
description: |
| 185 |
- Name of the variable field. |
| 186 |
required: true |
| 187 |
type: str |
| 188 |
value: |
| 189 |
description: |
| 190 |
- Value of the variable field. |
| 191 |
required: true |
| 192 |
type: str |
| 193 |
headers: |
| 194 |
description: |
| 195 |
- HTTP headers used when performing the scenario step. |
| 196 |
- Overrides I(params) headers. |
| 197 |
required: false |
| 198 |
type: list |
| 199 |
elements: dict |
| 200 |
suboptions: |
| 201 |
name: |
| 202 |
description: |
| 203 |
- Name of the header field. |
| 204 |
required: true |
| 205 |
type: str |
| 206 |
value: |
| 207 |
description: |
| 208 |
- Value of the header field. |
| 209 |
required: true |
| 210 |
type: str |
| 211 |
follow_redirects: |
| 212 |
description: |
| 213 |
- Whether to follow HTTP redirects. |
| 214 |
required: false |
| 215 |
type: bool |
| 216 |
retrieve_mode: |
| 217 |
description: |
| 218 |
- Part of the HTTP response that the scenario step must retrieve. |
| 219 |
required: false |
| 220 |
type: str |
| 221 |
choices: ["only_body", "only_headers", "headers_and_body"] |
| 222 |
timeout: |
| 223 |
description: |
| 224 |
- Request timeout of the scenario step. |
| 225 |
required: false |
| 226 |
type: str |
| 227 |
required: |
| 228 |
description: |
| 229 |
- Text that must be present in the response. |
| 230 |
required: false |
| 231 |
type: str |
| 232 |
status_codes: |
| 233 |
description: |
| 234 |
- Expected response status code. |
| 235 |
required: false |
| 236 |
type: list |
| 237 |
elements: int |
| 238 |
tags: |
| 239 |
description: |
| 240 |
- Tags of the httptest. |
| 241 |
required: false |
| 242 |
type: list |
| 243 |
elements: dict |
| 244 |
suboptions: |
| 245 |
tag: |
| 246 |
description: |
| 247 |
- Name of the httptest tag. |
| 248 |
required: true |
| 249 |
type: str |
| 250 |
value: |
| 251 |
description: |
| 252 |
- Value of the httptest tag. |
| 253 |
required: false |
| 254 |
type: str |
| 255 |
authentication: |
| 256 |
description: |
| 257 |
- HTTP authentication method used by the httptest. |
| 258 |
required: false |
| 259 |
type: str |
| 260 |
choices: ["none", "basic", "ntlm"] |
| 261 |
username: |
| 262 |
description: |
| 263 |
- Username to authenticate with the host. |
| 264 |
- Used if C(authentication) is one of C(basic) or C(ntlm) |
| 265 |
- Alias for "http_user" in API docs. |
| 266 |
required: false |
| 267 |
type: str |
| 268 |
password: |
| 269 |
description: |
| 270 |
- Password to authenticate with the host. |
| 271 |
- Used if C(authentication) is one of C(basic) or C(ntlm) |
| 272 |
- Alias for "http_password" in API docs. |
| 273 |
required: false |
| 274 |
type: str |
| 275 |
verify_peer: |
| 276 |
description: |
| 277 |
- Whether the httptest should verify the host's certificate. |
| 278 |
required: false |
| 279 |
type: bool |
| 280 |
verify_host: |
| 281 |
description: |
| 282 |
- Whether the httptest should verify the host's hostname. |
| 283 |
required: false |
| 284 |
type: bool |
| 285 |
ssl_cert_file: |
| 286 |
description: |
| 287 |
- Public SSL key file path for client authentication. |
| 288 |
required: false |
| 289 |
type: str |
| 290 |
ssl_key_file: |
| 291 |
description: |
| 292 |
- Private SSL key file path for client authentication. |
| 293 |
required: false |
| 294 |
type: str |
| 295 |
ssl_key_password: |
| 296 |
description: |
| 297 |
- Password of the private SSL key file. |
| 298 |
required: false |
| 299 |
type: str |
| 300 |
|
| 301 |
extends_documentation_fragment: |
| 302 |
- community.zabbix.zabbix |
| 303 |
''' |
| 304 |
|
| 305 |
EXAMPLES = r''' |
| 306 |
|
| 307 |
# If you want to use Username and Password to be authenticated by Zabbix Server |
| 308 |
- name: Set credentials to access Zabbix Server API |
| 309 |
ansible.builtin.set_fact: |
| 310 |
ansible_user: Admin |
| 311 |
ansible_httpapi_pass: zabbix |
| 312 |
|
| 313 |
# If you want to use API token to be authenticated by Zabbix Server |
| 314 |
# https://www.zabbix.com/documentation/current/en/manual/web_interface/frontend_sections/administration/general#api-tokens |
| 315 |
- name: Set API token |
| 316 |
ansible.builtin.set_fact: |
| 317 |
ansible_zabbix_auth_key: 8ec0d52432c15c91fcafe9888500cf9a607f44091ab554dbee860f6b44fac895 |
| 318 |
|
| 319 |
# Create a web scenario on example_host |
| 320 |
- name: create httptest |
| 321 |
# set task level variables as we change ansible_connection plugin here |
| 322 |
vars: |
| 323 |
ansible_network_os: community.zabbix.zabbix |
| 324 |
ansible_connection: httpapi |
| 325 |
ansible_httpapi_port: 443 |
| 326 |
ansible_httpapi_use_ssl: true |
| 327 |
ansible_httpapi_validate_certs: false |
| 328 |
ansible_zabbix_url_path: 'zabbixeu' # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu |
| 329 |
ansible_host: zabbix-example-fqdn.org |
| 330 |
community.zabbix.zabbix_httptest: |
| 331 |
name: example_host_website |
| 332 |
host_name: example_host |
| 333 |
params: |
| 334 |
steps: |
| 335 |
- name: Test |
| 336 |
url: https://zabbix-example-fqdn.org |
| 337 |
status_code: [200] |
| 338 |
interval: 1h |
| 339 |
enabled: True |
| 340 |
state: present |
| 341 |
|
| 342 |
# Change interval for existing Zabbix web scenario |
| 343 |
- name: update rule |
| 344 |
# set task level variables as we change ansible_connection plugin here |
| 345 |
vars: |
| 346 |
ansible_network_os: community.zabbix.zabbix |
| 347 |
ansible_connection: httpapi |
| 348 |
ansible_httpapi_port: 443 |
| 349 |
ansible_httpapi_use_ssl: true |
| 350 |
ansible_httpapi_validate_certs: false |
| 351 |
ansible_zabbix_url_path: 'zabbixeu' # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu |
| 352 |
ansible_host: zabbix-example-fqdn.org |
| 353 |
community.zabbix.zabbix_discoveryrule: |
| 354 |
name: example_host_website |
| 355 |
host_name: example_host |
| 356 |
params: |
| 357 |
interval: 2h |
| 358 |
state: present |
| 359 |
|
| 360 |
# Delete web scenario |
| 361 |
- name: delete rule |
| 362 |
# set task level variables as we change ansible_connection plugin here |
| 363 |
vars: |
| 364 |
ansible_network_os: community.zabbix.zabbix |
| 365 |
ansible_connection: httpapi |
| 366 |
ansible_httpapi_port: 443 |
| 367 |
ansible_httpapi_use_ssl: true |
| 368 |
ansible_httpapi_validate_certs: false |
| 369 |
ansible_zabbix_url_path: 'zabbixeu' # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu |
| 370 |
ansible_host: zabbix-example-fqdn.org |
| 371 |
community.zabbix.zabbix_discoveryrule: |
| 372 |
name: example_host_website |
| 373 |
host_name: example_host |
| 374 |
state: absent |
| 375 |
|
| 376 |
- name: Rename Zabbix web scenario |
| 377 |
# set task level variables as we change ansible_connection plugin here |
| 378 |
vars: |
| 379 |
ansible_network_os: community.zabbix.zabbix |
| 380 |
ansible_connection: httpapi |
| 381 |
ansible_httpapi_port: 443 |
| 382 |
ansible_httpapi_use_ssl: true |
| 383 |
ansible_httpapi_validate_certs: false |
| 384 |
ansible_zabbix_url_path: "zabbixeu" # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu |
| 385 |
ansible_host: zabbix-example-fqdn.org |
| 386 |
community.zabbix.zabbix_discoveryrule: |
| 387 |
name: example_host_website |
| 388 |
host_name: example_host |
| 389 |
params: |
| 390 |
new_name: new_example_host_website |
| 391 |
state: present |
| 392 |
''' |
| 393 |
|
| 394 |
from ansible.module_utils.basic import AnsibleModule |
| 395 |
|
| 396 |
from ansible_collections.community.zabbix.plugins.module_utils.base import ZabbixBase |
| 397 |
import ansible_collections.community.zabbix.plugins.module_utils.helpers as zabbix_utils |
| 398 |
|
| 399 |
|
| 400 |
class Httptest(ZabbixBase): |
| 401 |
RETRIEVE_MODES = {'only_body': 0, |
| 402 |
'only_headers': 1, |
| 403 |
'headers_and_body': 2} |
| 404 |
|
| 405 |
AUTHENTICATION_TYPES = {'none': 0, |
| 406 |
'basic': 1, |
| 407 |
'ntlm': 2} |
| 408 |
|
| 409 |
def get_hosts(self, host_name): |
| 410 |
try: |
| 411 |
return self._zapi.host.get({"filter": {"host": host_name}}) |
| 412 |
except Exception as e: |
| 413 |
self._module.fail_json(msg="Failed to get host: %s" % e) |
| 414 |
|
| 415 |
def get_httptests(self, httptest_name, host_name): |
| 416 |
httptests = [] |
| 417 |
try: |
| 418 |
httptests = self._zapi.httptest.get({'filter': {'name': httptest_name, 'host': host_name}}) |
| 419 |
except Exception as e: |
| 420 |
self._module.fail_json(msg="Failed to get httptests: %s" & e) |
| 421 |
return httptests |
| 422 |
|
| 423 |
def sanitize_params(self, name, params): |
| 424 |
params['name'] = name |
| 425 |
if 'interval' in params: |
| 426 |
params['delay'] = params['interval'] |
| 427 |
params.pop("interval") |
| 428 |
if 'attempts' in params: |
| 429 |
params['retries'] = params['attempts'] |
| 430 |
params.pop("attempts") |
| 431 |
if 'enabled' in params: |
| 432 |
if params['enabled']: |
| 433 |
params['status'] = 'enabled' |
| 434 |
else: |
| 435 |
params['status'] = 'disabled' |
| 436 |
params.pop("enabled") |
| 437 |
if 'status' in params: |
| 438 |
status = params['status'] |
| 439 |
if status == 'enabled': |
| 440 |
params['status'] = 0 |
| 441 |
elif status == 'disabled': |
| 442 |
params['status'] = 1 |
| 443 |
else: |
| 444 |
self._module.fail_json(msg="Status must be 'enabled' or 'disabled', got %s" % status) |
| 445 |
if 'steps' in params: |
| 446 |
no = 0 |
| 447 |
for step in params['steps']: |
| 448 |
no += 1 |
| 449 |
step['no'] = no |
| 450 |
if 'follow_redirects' in step: |
| 451 |
follow_redirects = step['follow_redirects'] |
| 452 |
if follow_redirects: |
| 453 |
step['follow_redirects'] = 1 |
| 454 |
else: |
| 455 |
step['follow_redirects'] = 0 |
| 456 |
if 'retrieve_mode' in step: |
| 457 |
retrieve_mode_int = self.RETRIEVE_MODES[step['retrieve_mode']] |
| 458 |
step['retrieve_mode'] = retrieve_mode_int |
| 459 |
if 'status_codes' in step: |
| 460 |
status_codes_str = ",".join(str(code) for code in step['status_codes']) |
| 461 |
step['status_codes'] = status_codes_str |
| 462 |
if 'authentication' in params: |
| 463 |
authentication_int = self.AUTHENTICATION_TYPES[params['authentication']] |
| 464 |
params['authentication'] = authentication_int |
| 465 |
if 'username' in params: |
| 466 |
params['http_user'] = params['username'] |
| 467 |
params.pop("username") |
| 468 |
if 'password' in params: |
| 469 |
params['http_password'] = params['password'] |
| 470 |
params.pop("password") |
| 471 |
if 'verify_peer' in params: |
| 472 |
verify = params['verify_peer'] |
| 473 |
if verify: |
| 474 |
params['verify_peer'] = 1 |
| 475 |
else: |
| 476 |
params['verify_peer'] = 0 |
| 477 |
if 'verify_host' in params: |
| 478 |
verify = params['verify_host'] |
| 479 |
if verify: |
| 480 |
params['verify_host'] = 1 |
| 481 |
else: |
| 482 |
params['verify_host'] = 0 |
| 483 |
|
| 484 |
def add_httptest(self, params): |
| 485 |
if self._module.check_mode: |
| 486 |
self._module.exit_json(changed=True) |
| 487 |
try: |
| 488 |
results = self._zapi.httptest.create(params) |
| 489 |
except Exception as e: |
| 490 |
self._module.fail_json(msg="Failed to create httptest: %s" % e) |
| 491 |
return results |
| 492 |
|
| 493 |
def update_httptest(self, params): |
| 494 |
if self._module.check_mode: |
| 495 |
self._module.exit_json(changed=True) |
| 496 |
try: |
| 497 |
results = self._zapi.httptest.update(params) |
| 498 |
except Exception as e: |
| 499 |
self._module.fail_json(msg="Failed to update httptest: %s" % e) |
| 500 |
return results |
| 501 |
|
| 502 |
def check_httptest_changed(self, old_httptest): |
| 503 |
try: |
| 504 |
new_httptest = self._zapi.httptest.get({'httptestids': "%s" % old_httptest['httptestid']})[0] |
| 505 |
except Exception as e: |
| 506 |
self._module.fail_json(msg="Failed to get httptest: %s" % e) |
| 507 |
return old_httptest != new_httptest |
| 508 |
|
| 509 |
def delete_httptests(self, httptest_ids): |
| 510 |
if self._module.check_mode: |
| 511 |
self._module.exit_json(changed=True) |
| 512 |
try: |
| 513 |
results = self._zapi.httptest.delete(httptest_ids) |
| 514 |
except Exception as e: |
| 515 |
self._module.fail_json(msg="Failed to delete httptests: %s" % e) |
| 516 |
return results |
| 517 |
|
| 518 |
|
| 519 |
def main(): |
| 520 |
argument_spec = zabbix_utils.zabbix_common_argument_spec() |
| 521 |
argument_spec.update(dict( |
| 522 |
name=dict(type='str', required=True), |
| 523 |
host_name=dict(type='str', required=True), |
| 524 |
params=dict(type='dict', required=False), |
| 525 |
state=dict(type='str', default="present", choices=['present', 'absent']), |
| 526 |
)) |
| 527 |
module = AnsibleModule( |
| 528 |
argument_spec=argument_spec, |
| 529 |
required_if=[ |
| 530 |
['state', 'present', ['params']] |
| 531 |
], |
| 532 |
supports_check_mode=True |
| 533 |
) |
| 534 |
|
| 535 |
name = module.params['name'] |
| 536 |
host_name = module.params['host_name'] |
| 537 |
params = module.params['params'] |
| 538 |
state = module.params['state'] |
| 539 |
|
| 540 |
httptest = Httptest(module) |
| 541 |
|
| 542 |
if state == "absent": |
| 543 |
httptests = httptest.get_httptests(name, host_name) |
| 544 |
if len(httptests) == 0: |
| 545 |
module.exit_json(changed=False, result="No httptest to delete.") |
| 546 |
else: |
| 547 |
delete_ids = [] |
| 548 |
for h in httptests: |
| 549 |
delete_ids.append(h['httptestid']) |
| 550 |
results = httptest.delete_httptests(delete_ids) |
| 551 |
module.exit_json(changed=True, result=results) |
| 552 |
|
| 553 |
elif state == "present": |
| 554 |
httptest.sanitize_params(name, params) |
| 555 |
httptests = httptest.get_httptests(name, host_name) |
| 556 |
if 'new_name' in params: |
| 557 |
new_name_httptest = httptest.get_httptests(params['new_name'], host_name) |
| 558 |
if len(new_name_httptest) > 0: |
| 559 |
module.exit_json(changed=False, results=[{'httptestids': [new_name_httptest[0]['httptestid']]}]) |
| 560 |
results = [] |
| 561 |
if len(httptests) == 0: |
| 562 |
if 'new_name' in params: |
| 563 |
module.fail_json('Cannot rename httptest: %s is not found' % name) |
| 564 |
if not ('steps' in params): |
| 565 |
module.fail_json('Cannot create httptest without steps') |
| 566 |
hosts = httptest.get_hosts(host_name) |
| 567 |
for host in hosts: |
| 568 |
if 'hostid' in host: |
| 569 |
params['hostid'] = host['hostid'] |
| 570 |
else: |
| 571 |
module.fail_json(msg="host did not return id") |
| 572 |
results.append(httptest.add_httptest(params)) |
| 573 |
module.exit_json(changed=True, result=results) |
| 574 |
else: |
| 575 |
changed = False |
| 576 |
for h in httptests: |
| 577 |
params['httptestid'] = h['httptestid'] |
| 578 |
if 'new_name' in params: |
| 579 |
params['name'] = params['new_name'] |
| 580 |
params.pop("new_name") |
| 581 |
results.append(httptest.update_httptest(params)) |
| 582 |
changed_test = httptest.check_httptest_changed(h) |
| 583 |
if changed_test: |
| 584 |
changed = True |
| 585 |
module.exit_json(changed=changed, result=results) |
| 586 |
|
| 587 |
|
| 588 |
if __name__ == '__main__': |
| 589 |
main() |