| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
# |
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
from __future__ import absolute_import, division, print_function |
| 7 |
__metaclass__ = type |
| 8 |
|
| 9 |
|
| 10 |
DOCUMENTATION = r''' |
| 11 |
--- |
| 12 |
module: zabbix_item |
| 13 |
short_description: Create/delete Zabbix items |
| 14 |
description: |
| 15 |
- Create items if they do not exist. |
| 16 |
- Delete existing items if they exist. |
| 17 |
author: |
| 18 |
- "Andrew Lathrop (@aplathrop)" |
| 19 |
requirements: |
| 20 |
- "python >= 2.6" |
| 21 |
|
| 22 |
options: |
| 23 |
state: |
| 24 |
description: |
| 25 |
- Create or delete item. |
| 26 |
required: false |
| 27 |
type: str |
| 28 |
default: "present" |
| 29 |
choices: [ "present", "absent" ] |
| 30 |
name: |
| 31 |
description: |
| 32 |
- Name of item to create or delete. |
| 33 |
required: true |
| 34 |
type: str |
| 35 |
host_name: |
| 36 |
description: |
| 37 |
- Name of host to add item to. |
| 38 |
- Required when I(template_name) is not used. |
| 39 |
- Mutually exclusive with I(template_name). |
| 40 |
required: false |
| 41 |
type: str |
| 42 |
template_name: |
| 43 |
description: |
| 44 |
- Name of template to add item to. |
| 45 |
- Required when I(host_name) is not used. |
| 46 |
- Mutually exclusive with I(host_name). |
| 47 |
required: false |
| 48 |
type: str |
| 49 |
params: |
| 50 |
description: |
| 51 |
- Parameters to create/update item with. |
| 52 |
- Required if state is "present". |
| 53 |
- Parameters as defined at https://www.zabbix.com/documentation/current/en/manual/api/reference/item/object |
| 54 |
- Additionally supported parameters are below |
| 55 |
required: false |
| 56 |
type: dict |
| 57 |
suboptions: |
| 58 |
key: |
| 59 |
description: |
| 60 |
- Item key. |
| 61 |
- Alias for "key_" in API docs |
| 62 |
required: false |
| 63 |
type: str |
| 64 |
interval: |
| 65 |
description: |
| 66 |
- Update interval of the item. |
| 67 |
- Alias for "delay" in API docs |
| 68 |
required: false |
| 69 |
type: str |
| 70 |
status: |
| 71 |
description: |
| 72 |
- Status of the item. |
| 73 |
required: false |
| 74 |
type: str |
| 75 |
choices: [ "enabled", "disabled" ] |
| 76 |
enabled: |
| 77 |
description: |
| 78 |
- Status of the item. |
| 79 |
- Overrides "status" in API docs |
| 80 |
required: false |
| 81 |
type: bool |
| 82 |
type: |
| 83 |
description: |
| 84 |
- Type of the item. |
| 85 |
- Required if state is "present". |
| 86 |
required: false |
| 87 |
type: str |
| 88 |
choices: |
| 89 |
- zabbix_agent |
| 90 |
- zabbix_trapper |
| 91 |
- simple_check |
| 92 |
- zabbix_internal |
| 93 |
- zabbix_agent_active |
| 94 |
- web_item |
| 95 |
- external_check |
| 96 |
- database_monitor |
| 97 |
- ipmi_agent |
| 98 |
- ssh_agent |
| 99 |
- telnet_agent |
| 100 |
- calculated |
| 101 |
- jmx_agent |
| 102 |
- snmp_trap |
| 103 |
- dependent_item |
| 104 |
- http_agent |
| 105 |
- snmp_agent |
| 106 |
- script |
| 107 |
value_type: |
| 108 |
description: |
| 109 |
- Type of information of the item. |
| 110 |
- Required if state is "present". |
| 111 |
required: false |
| 112 |
type: str |
| 113 |
choices: |
| 114 |
- numeric_float |
| 115 |
- character |
| 116 |
- log |
| 117 |
- numeric_unsigned |
| 118 |
- text |
| 119 |
new_name: |
| 120 |
description: |
| 121 |
- New name for item |
| 122 |
required: false |
| 123 |
type: str |
| 124 |
master_item: |
| 125 |
description: |
| 126 |
- item that is the master of the current one |
| 127 |
- Overrides "master_itemid" in API docs |
| 128 |
required: false |
| 129 |
type: dict |
| 130 |
suboptions: |
| 131 |
item_name: |
| 132 |
description: |
| 133 |
- name of the master item |
| 134 |
required: true |
| 135 |
type: str |
| 136 |
host_name: |
| 137 |
description: |
| 138 |
- name of the host the master item belongs to |
| 139 |
- Required when I(template_name) is not used. |
| 140 |
- Mutually exclusive with I(template_name). |
| 141 |
required: false |
| 142 |
template_name: |
| 143 |
description: |
| 144 |
- name of the template the master item belongs to |
| 145 |
- Required when I(host_name) is not used. |
| 146 |
- Mutually exclusive with I(host_name). |
| 147 |
preprocessing: |
| 148 |
description: |
| 149 |
- Item preprocessing options. |
| 150 |
- Parameters as defined at https://www.zabbix.com/documentation/current/en/manual/api/reference/item/object#item-preprocessing |
| 151 |
- Additionally supported parameters are below |
| 152 |
required: false |
| 153 |
type: list |
| 154 |
elements: dict |
| 155 |
suboptions: |
| 156 |
type: |
| 157 |
description: |
| 158 |
- The preprocessing option type. |
| 159 |
required: true |
| 160 |
type: str |
| 161 |
choices: |
| 162 |
- custom_multiplier |
| 163 |
- right_trim |
| 164 |
- left_trim |
| 165 |
- trim |
| 166 |
- regular_expressions |
| 167 |
- regex |
| 168 |
- boolean_to_decimal |
| 169 |
- octal_to_decimal |
| 170 |
- hexadecimal_to_decimal |
| 171 |
- simple_change |
| 172 |
- change_per_second |
| 173 |
- xml_xpath |
| 174 |
- jsonpath |
| 175 |
- in_range |
| 176 |
- matches_regular_expression |
| 177 |
- matches_regex |
| 178 |
- does_not_match_regular_expression |
| 179 |
- not_match_regex |
| 180 |
- check_for_error_in_json |
| 181 |
- check_for_json_error |
| 182 |
- check_for_error_in_xml |
| 183 |
- check_for_xml_error |
| 184 |
- check_for_error_using_regular_expression |
| 185 |
- check_for_error_regex |
| 186 |
- discard_unchanged |
| 187 |
- discard_unchanged_with_heartbeat |
| 188 |
- javascript |
| 189 |
- prometheus_pattern |
| 190 |
- prometheus_to_json |
| 191 |
- csv_to_json |
| 192 |
- replace |
| 193 |
- check_unsupported |
| 194 |
- xml_to_json |
| 195 |
- snmp_walk_value |
| 196 |
- snmp_walk_to_json |
| 197 |
error_handler: |
| 198 |
description: |
| 199 |
- Action type used in case of preprocessing step failure. |
| 200 |
required: false |
| 201 |
type: str |
| 202 |
choices: |
| 203 |
- zabbix_server |
| 204 |
- discard |
| 205 |
- set_custom_value |
| 206 |
- set_custom_error_message |
| 207 |
interface: |
| 208 |
description: |
| 209 |
- configuration of the item's host interface |
| 210 |
- required if item belongs to host and type is set to "Zabbix agent", "IPMI agent", "JMX agent", "SNMP trap", or "SNMP agent" |
| 211 |
required: false |
| 212 |
type: list |
| 213 |
elements: dict |
| 214 |
suboptions: |
| 215 |
main: |
| 216 |
description: |
| 217 |
- Whether the default interface of the host of this type is used |
| 218 |
required: false |
| 219 |
type: bool |
| 220 |
default: false |
| 221 |
ip: |
| 222 |
description: |
| 223 |
- IP address used by the interface |
| 224 |
- Either ip or dns is required when main is false |
| 225 |
required: false |
| 226 |
type: str |
| 227 |
dns: |
| 228 |
description: |
| 229 |
- DNS name used by the interface |
| 230 |
- Either ip or dns is required when main is false |
| 231 |
required: false |
| 232 |
type: str |
| 233 |
|
| 234 |
extends_documentation_fragment: |
| 235 |
- community.zabbix.zabbix |
| 236 |
''' |
| 237 |
|
| 238 |
EXAMPLES = r''' |
| 239 |
|
| 240 |
# If you want to use Username and Password to be authenticated by Zabbix Server |
| 241 |
- name: Set credentials to access Zabbix Server API |
| 242 |
ansible.builtin.set_fact: |
| 243 |
ansible_user: Admin |
| 244 |
ansible_httpapi_pass: zabbix |
| 245 |
|
| 246 |
# If you want to use API token to be authenticated by Zabbix Server |
| 247 |
# https://www.zabbix.com/documentation/current/en/manual/web_interface/frontend_sections/administration/general#api-tokens |
| 248 |
- name: Set API token |
| 249 |
ansible.builtin.set_fact: |
| 250 |
ansible_zabbix_auth_key: 8ec0d52432c15c91fcafe9888500cf9a607f44091ab554dbee860f6b44fac895 |
| 251 |
|
| 252 |
# Create ping item on example_host |
| 253 |
- name: create ping item |
| 254 |
# set task level variables as we change ansible_connection plugin here |
| 255 |
vars: |
| 256 |
ansible_network_os: community.zabbix.zabbix |
| 257 |
ansible_connection: httpapi |
| 258 |
ansible_httpapi_port: 443 |
| 259 |
ansible_httpapi_use_ssl: true |
| 260 |
ansible_httpapi_validate_certs: false |
| 261 |
ansible_zabbix_url_path: 'zabbixeu' # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu |
| 262 |
ansible_host: zabbix-example-fqdn.org |
| 263 |
community.zabbix.zabbix_item: |
| 264 |
name: agent_ping |
| 265 |
host_name: example_host |
| 266 |
params: |
| 267 |
type: zabbix_agent |
| 268 |
key: agent.ping |
| 269 |
value_type: numeric_unsigned |
| 270 |
interval: 1m |
| 271 |
interface: |
| 272 |
main: true |
| 273 |
state: present |
| 274 |
|
| 275 |
# Create ping item on example_template |
| 276 |
- name: create ping item |
| 277 |
# set task level variables as we change ansible_connection plugin here |
| 278 |
vars: |
| 279 |
ansible_network_os: community.zabbix.zabbix |
| 280 |
ansible_connection: httpapi |
| 281 |
ansible_httpapi_port: 443 |
| 282 |
ansible_httpapi_use_ssl: true |
| 283 |
ansible_httpapi_validate_certs: false |
| 284 |
ansible_zabbix_url_path: 'zabbixeu' # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu |
| 285 |
ansible_host: zabbix-example-fqdn.org |
| 286 |
community.zabbix.zabbix_item: |
| 287 |
name: agent_ping |
| 288 |
template_name: example_template |
| 289 |
params: |
| 290 |
type: zabbix_agent |
| 291 |
key: agent.ping |
| 292 |
value_type: numeric_unsigned |
| 293 |
interval: 1m |
| 294 |
state: present |
| 295 |
|
| 296 |
- name: Add tags to the existing Zabbix item |
| 297 |
# set task level variables as we change ansible_connection plugin here |
| 298 |
vars: |
| 299 |
ansible_network_os: community.zabbix.zabbix |
| 300 |
ansible_connection: httpapi |
| 301 |
ansible_httpapi_port: 443 |
| 302 |
ansible_httpapi_use_ssl: true |
| 303 |
ansible_httpapi_validate_certs: false |
| 304 |
ansible_zabbix_url_path: "zabbixeu" # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu |
| 305 |
ansible_host: zabbix-example-fqdn.org |
| 306 |
community.zabbix.zabbix_item: |
| 307 |
name: agent_ping |
| 308 |
template_name: example_template |
| 309 |
params: |
| 310 |
type: zabbix_agent |
| 311 |
key: agent.ping |
| 312 |
value_type: numeric_unsigned |
| 313 |
interval: 1m |
| 314 |
tags: |
| 315 |
- tag: class |
| 316 |
value: application |
| 317 |
state: present |
| 318 |
|
| 319 |
- name: create a dependent item |
| 320 |
# set task level variables as we change ansible_connection plugin here |
| 321 |
vars: |
| 322 |
ansible_network_os: community.zabbix.zabbix |
| 323 |
ansible_connection: httpapi |
| 324 |
ansible_httpapi_port: 443 |
| 325 |
ansible_httpapi_use_ssl: true |
| 326 |
ansible_httpapi_validate_certs: false |
| 327 |
ansible_zabbix_url_path: "zabbixeu" # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu |
| 328 |
ansible_host: zabbix-example-fqdn.org |
| 329 |
community.zabbix.zabbix_item: |
| 330 |
name: depend_item |
| 331 |
host_name: example_host |
| 332 |
params: |
| 333 |
type: dependent_item |
| 334 |
key: vfs.fs.pused |
| 335 |
value_type: numeric_float |
| 336 |
units: '%' |
| 337 |
master_item: |
| 338 |
item_name: example_item |
| 339 |
host_name: example_host |
| 340 |
preprocessing: |
| 341 |
- type: jsonpath |
| 342 |
params: '$[?(@.fstype == "ext4")]' |
| 343 |
error_handler: zabbix_server |
| 344 |
- type: jsonpath |
| 345 |
params: "$[*].['bytes', 'inodes'].pused.max()" |
| 346 |
error_handler: zabbix_server |
| 347 |
state: present |
| 348 |
|
| 349 |
- name: Delete Zabbix item |
| 350 |
# set task level variables as we change ansible_connection plugin here |
| 351 |
vars: |
| 352 |
ansible_network_os: community.zabbix.zabbix |
| 353 |
ansible_connection: httpapi |
| 354 |
ansible_httpapi_port: 443 |
| 355 |
ansible_httpapi_use_ssl: true |
| 356 |
ansible_httpapi_validate_certs: false |
| 357 |
ansible_zabbix_url_path: "zabbixeu" # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu |
| 358 |
ansible_host: zabbix-example-fqdn.org |
| 359 |
community.zabbix.zabbix_item: |
| 360 |
name: agent_ping |
| 361 |
template_name: example_template |
| 362 |
state: absent |
| 363 |
|
| 364 |
- name: Rename Zabbix item |
| 365 |
# set task level variables as we change ansible_connection plugin here |
| 366 |
vars: |
| 367 |
ansible_network_os: community.zabbix.zabbix |
| 368 |
ansible_connection: httpapi |
| 369 |
ansible_httpapi_port: 443 |
| 370 |
ansible_httpapi_use_ssl: true |
| 371 |
ansible_httpapi_validate_certs: false |
| 372 |
ansible_zabbix_url_path: "zabbixeu" # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu |
| 373 |
ansible_host: zabbix-example-fqdn.org |
| 374 |
community.zabbix.zabbix_item: |
| 375 |
name: agent_ping |
| 376 |
template_name: example_template |
| 377 |
params: |
| 378 |
new_name: new_agent_ping |
| 379 |
state: present |
| 380 |
''' |
| 381 |
|
| 382 |
from ansible.module_utils.basic import AnsibleModule |
| 383 |
|
| 384 |
from ansible_collections.community.zabbix.plugins.module_utils.base import ZabbixBase |
| 385 |
import ansible_collections.community.zabbix.plugins.module_utils.helpers as zabbix_utils |
| 386 |
|
| 387 |
|
| 388 |
class Item(ZabbixBase): |
| 389 |
ITEM_TYPES = {'zabbix_agent': 0, |
| 390 |
'zabbix_trapper': 2, |
| 391 |
'simple_check': 3, |
| 392 |
'zabbix_internal': 5, |
| 393 |
'zabbix_agent_active': 7, |
| 394 |
'web_item': 9, |
| 395 |
'external_check': 10, |
| 396 |
'database_monitor': 11, |
| 397 |
'ipmi_agent': 12, |
| 398 |
'ssh_agent': 13, |
| 399 |
'telnet_agent': 14, |
| 400 |
'calculated': 15, |
| 401 |
'jmx_agent': 16, |
| 402 |
'snmp_trap': 17, |
| 403 |
'dependent_item': 18, |
| 404 |
'http_agent': 19, |
| 405 |
'snmp_agent': 20, |
| 406 |
'script': 21} |
| 407 |
|
| 408 |
ITEM_TYPES_REQUIRING_INTERFACE_IF_HOST = {ITEM_TYPES['zabbix_agent']: 1, |
| 409 |
ITEM_TYPES['snmp_trap']: 2, |
| 410 |
ITEM_TYPES['snmp_agent']: 2, |
| 411 |
ITEM_TYPES['ipmi_agent']: 3, |
| 412 |
ITEM_TYPES['jmx_agent']: 4} |
| 413 |
|
| 414 |
VALUE_TYPES = {'numeric_float': 0, |
| 415 |
'character': 1, |
| 416 |
'log': 2, |
| 417 |
'numeric_unsigned': 3, |
| 418 |
'text': 4} |
| 419 |
|
| 420 |
PREPROCESSING_TYPES = {'custom_multiplier': 1, |
| 421 |
'right_trim': 2, |
| 422 |
'left_trim': 3, |
| 423 |
'trim': 4, |
| 424 |
'regular_expressions': 5, |
| 425 |
'regex': 5, |
| 426 |
'boolean_to_decimal': 6, |
| 427 |
'octal_to_decimal': 7, |
| 428 |
'hexadecimal_to_decimal': 8, |
| 429 |
'simple_change': 9, |
| 430 |
'change_per_second': 10, |
| 431 |
'xml_xpath': 11, |
| 432 |
'jsonpath': 12, |
| 433 |
'in_range': 13, |
| 434 |
'matches_regular_expression': 14, |
| 435 |
'matches_regex': 14, |
| 436 |
'does_not_match_regular_expression': 15, |
| 437 |
'not_match_regex': 15, |
| 438 |
'check_for_error_in_json': 16, |
| 439 |
'check_for_json_error': 16, |
| 440 |
'check_for_error_in_xml': 17, |
| 441 |
'check_for_xml_error': 17, |
| 442 |
'check_for_error_using_regular_expression': 18, |
| 443 |
'check_for_error_regex': 18, |
| 444 |
'discard_unchanged': 19, |
| 445 |
'discard_unchanged_with_heartbeat': 20, |
| 446 |
'javascript': 21, |
| 447 |
'prometheus_pattern': 22, |
| 448 |
'prometheus_to_json': 23, |
| 449 |
'csv_to_json': 24, |
| 450 |
'replace': 25, |
| 451 |
'check_unsupported': 26, |
| 452 |
'xml_to_json': 27, |
| 453 |
'snmp_walk_value': 28, |
| 454 |
'snmp_walk_to_json': 29} |
| 455 |
|
| 456 |
PREPROCESSING_ERROR_HANDLERS = {'zabbix_server': 0, |
| 457 |
'discard': 1, |
| 458 |
'set_custom_value': 2, |
| 459 |
'set_custom_error_message': 3} |
| 460 |
|
| 461 |
def get_hosts_templates(self, host_name, template_name): |
| 462 |
if host_name is not None: |
| 463 |
try: |
| 464 |
return self._zapi.host.get({"filter": {"host": host_name}}) |
| 465 |
except Exception as e: |
| 466 |
self._module.fail_json(msg="Failed to get host: %s" % e) |
| 467 |
else: |
| 468 |
try: |
| 469 |
return self._zapi.template.get({"filter": {"host": template_name}}) |
| 470 |
except Exception as e: |
| 471 |
self._module.fail_json(msg="Failed to get template: %s" % e) |
| 472 |
|
| 473 |
def get_items(self, item_name, host_name, template_name): |
| 474 |
if host_name is not None: |
| 475 |
host = host_name |
| 476 |
else: |
| 477 |
host = template_name |
| 478 |
items = [] |
| 479 |
try: |
| 480 |
items = self._zapi.item.get({'filter': {'name': item_name, 'host': host}}) |
| 481 |
except Exception as e: |
| 482 |
self._module.fail_json(msg="Failed to get item: %s" % e) |
| 483 |
return items |
| 484 |
|
| 485 |
def sanitize_params(self, name, params): |
| 486 |
params['name'] = name |
| 487 |
if 'key' in params: |
| 488 |
params['key_'] = params['key'] |
| 489 |
params.pop("key") |
| 490 |
if 'type' in params: |
| 491 |
item_type_int = self.ITEM_TYPES[params['type']] |
| 492 |
params['type'] = item_type_int |
| 493 |
if 'value_type' in params: |
| 494 |
value_type_int = self.VALUE_TYPES[params['value_type']] |
| 495 |
params['value_type'] = value_type_int |
| 496 |
if 'interval' in params: |
| 497 |
params['delay'] = params['interval'] |
| 498 |
params.pop("interval") |
| 499 |
if 'enabled' in params: |
| 500 |
if params['enabled']: |
| 501 |
params['status'] = 'enabled' |
| 502 |
else: |
| 503 |
params['status'] = 'disabled' |
| 504 |
params.pop("enabled") |
| 505 |
if 'status' in params: |
| 506 |
status = params['status'] |
| 507 |
if status == 'enabled': |
| 508 |
params['status'] = 0 |
| 509 |
elif status == 'disabled': |
| 510 |
params['status'] = 1 |
| 511 |
else: |
| 512 |
self._module.fail_json(msg="Status must be 'enabled' or 'disabled', got %s" % status) |
| 513 |
if 'master_item' in params: |
| 514 |
if 'host_name' not in params['master_item']: |
| 515 |
params['master_item']['host_name'] = None |
| 516 |
if 'template_name' not in params['master_item']: |
| 517 |
params['master_item']['template_name'] = None |
| 518 |
master_items = self.get_items(params['master_item']['item_name'], params['master_item']['host_name'], params['master_item']['template_name']) |
| 519 |
if len(master_items) == 0: |
| 520 |
self._module.fail_json(msg="No items with the name %s exist to depend on" % params['master_item']['item_name']) |
| 521 |
params['master_itemid'] = master_items[0]['itemid'] |
| 522 |
params.pop('master_item') |
| 523 |
if 'preprocessing' in params: |
| 524 |
for param in params['preprocessing']: |
| 525 |
preprocess_type_int = self.PREPROCESSING_TYPES[param['type']] |
| 526 |
param['type'] = preprocess_type_int |
| 527 |
if 'error_handler' in param: |
| 528 |
error_handler_int = self.PREPROCESSING_ERROR_HANDLERS[param['error_handler']] |
| 529 |
param['error_handler'] = error_handler_int |
| 530 |
if 'interface' in params and params['interface'] is not None: |
| 531 |
if 'main' not in params['interface'] or params['interface']['main'] is not True: |
| 532 |
params['interface']['main'] = False |
| 533 |
if 'ip' not in params['interface']: |
| 534 |
params['interface']['ip'] = '' |
| 535 |
if 'dns' not in params['interface']: |
| 536 |
params['interface']['dns'] = '' |
| 537 |
|
| 538 |
def add_item(self, params): |
| 539 |
if self._module.check_mode: |
| 540 |
self._module.exit_json(changed=True) |
| 541 |
try: |
| 542 |
results = self._zapi.item.create(params) |
| 543 |
except Exception as e: |
| 544 |
self._module.fail_json(msg="Failed to create item: %s" % e) |
| 545 |
return results |
| 546 |
|
| 547 |
def update_item(self, params): |
| 548 |
if self._module.check_mode: |
| 549 |
self._module.exit_json(changed=True) |
| 550 |
try: |
| 551 |
results = self._zapi.item.update(params) |
| 552 |
except Exception as e: |
| 553 |
self._module.fail_json(msg="Failed to update item: %s" % e) |
| 554 |
return results |
| 555 |
|
| 556 |
def check_item_changed(self, old_item): |
| 557 |
try: |
| 558 |
new_item = self._zapi.item.get({'itemids': "%s" % old_item['itemid']})[0] |
| 559 |
except Exception as e: |
| 560 |
self._module.fail_json(msg="Failed to get item: %s" % e) |
| 561 |
return old_item != new_item |
| 562 |
|
| 563 |
def delete_item(self, item_id): |
| 564 |
if self._module.check_mode: |
| 565 |
self._module.exit_json(changed=True) |
| 566 |
try: |
| 567 |
results = self._zapi.item.delete(item_id) |
| 568 |
except Exception as e: |
| 569 |
self._module.fail_json(msg="Failed to delete item: %s" % e) |
| 570 |
return results |
| 571 |
|
| 572 |
def get_interfaceid_by_params(self, host_id, params): |
| 573 |
if 'interface' not in params or params['interface'] is None: |
| 574 |
self._module.fail_json(msg="'interface' is mandatory if host is set and type is one of zabbix_agent, snmp_trap, snmp_agent, ipmi_agent, jmx_agent") |
| 575 |
if params['interface']['main'] is not True and "".__eq__(params['interface']['ip']) and "".__eq__(params['interface']['dns']): |
| 576 |
self._module.fail_json(msg="Set main = 1 if you want to use the default interface of the host, otherwise you must either specify ip or dns") |
| 577 |
filter = {"type": self.ITEM_TYPES_REQUIRING_INTERFACE_IF_HOST[params['type']]} |
| 578 |
if params['interface']['main'] is True: |
| 579 |
filter['main'] = "1" |
| 580 |
elif not "".__eq__(params['interface']['ip']): |
| 581 |
filter['ip'] = params['interface']['ip'] |
| 582 |
elif not "".__eq__(params['interface']['dns']): |
| 583 |
filter['dns'] = params['interface']['dns'] |
| 584 |
try: |
| 585 |
host_interfaces = self._zapi.hostinterface.get({"hostids": host_id, "filter": filter}) |
| 586 |
if (len(host_interfaces) > 0): |
| 587 |
return host_interfaces[0]['interfaceid'] |
| 588 |
else: |
| 589 |
self._module.fail_json(msg="interface not found on host") |
| 590 |
except Exception as e: |
| 591 |
self._module.fail_json(msg="Failed to get interfaces for host: %s" % e) |
| 592 |
|
| 593 |
|
| 594 |
def main(): |
| 595 |
argument_spec = zabbix_utils.zabbix_common_argument_spec() |
| 596 |
argument_spec.update(dict( |
| 597 |
name=dict(type='str', required=True), |
| 598 |
host_name=dict(type='str', required=False), |
| 599 |
template_name=dict(type='str', required=False), |
| 600 |
params=dict(type='dict', required=False), |
| 601 |
state=dict(type='str', default="present", choices=['present', 'absent']), |
| 602 |
)) |
| 603 |
module = AnsibleModule( |
| 604 |
argument_spec=argument_spec, |
| 605 |
required_one_of=[ |
| 606 |
['host_name', 'template_name'] |
| 607 |
], |
| 608 |
mutually_exclusive=[ |
| 609 |
['host_name', 'template_name'] |
| 610 |
], |
| 611 |
required_if=[ |
| 612 |
['state', 'present', ['params']] |
| 613 |
], |
| 614 |
supports_check_mode=True |
| 615 |
) |
| 616 |
|
| 617 |
name = module.params['name'] |
| 618 |
host_name = module.params['host_name'] |
| 619 |
template_name = module.params['template_name'] |
| 620 |
params = module.params['params'] |
| 621 |
state = module.params['state'] |
| 622 |
|
| 623 |
item = Item(module) |
| 624 |
|
| 625 |
if state == "absent": |
| 626 |
items = item.get_items(name, host_name, template_name) |
| 627 |
if len(items) == 0: |
| 628 |
module.exit_json(changed=False, result="No item to delete.") |
| 629 |
else: |
| 630 |
delete_ids = [] |
| 631 |
for i in items: |
| 632 |
delete_ids.append(i['itemid']) |
| 633 |
results = item.delete_item(delete_ids) |
| 634 |
module.exit_json(changed=True, result=results) |
| 635 |
|
| 636 |
elif state == "present": |
| 637 |
item.sanitize_params(name, params) |
| 638 |
items = item.get_items(name, host_name, template_name) |
| 639 |
if 'new_name' in params: |
| 640 |
new_name_item = item.get_items(params['new_name'], host_name, template_name) |
| 641 |
if len(new_name_item) > 0: |
| 642 |
module.exit_json(changed=False, result=[{'itemids': [new_name_item[0]['itemid']]}]) |
| 643 |
results = [] |
| 644 |
if len(items) == 0: |
| 645 |
if 'new_name' in params: |
| 646 |
module.fail_json('Cannot rename item: %s is not found' % name) |
| 647 |
hosts_templates = item.get_hosts_templates(host_name, template_name) |
| 648 |
for host_template in hosts_templates: |
| 649 |
if 'hostid' in host_template: |
| 650 |
params['hostid'] = host_template['hostid'] |
| 651 |
if params['type'] in item.ITEM_TYPES_REQUIRING_INTERFACE_IF_HOST: |
| 652 |
params['interfaceid'] = item.get_interfaceid_by_params(host_template['hostid'], params) |
| 653 |
elif 'templateid' in host_template: |
| 654 |
params['hostid'] = host_template['templateid'] |
| 655 |
else: |
| 656 |
module.fail_json(msg="host/template did not return id") |
| 657 |
if 'interface' in params: |
| 658 |
del params['interface'] |
| 659 |
results.append(item.add_item(params)) |
| 660 |
module.exit_json(changed=True, result=results) |
| 661 |
else: |
| 662 |
changed = False |
| 663 |
for i in items: |
| 664 |
params['itemid'] = i['itemid'] |
| 665 |
if 'new_name' in params: |
| 666 |
params['name'] = params['new_name'] |
| 667 |
params.pop("new_name") |
| 668 |
if host_name is not None and 'type' in params and params['type'] in item.ITEM_TYPES_REQUIRING_INTERFACE_IF_HOST: |
| 669 |
hosts_templates = item.get_hosts_templates(host_name, template_name) |
| 670 |
for host_template in hosts_templates: |
| 671 |
params['interfaceid'] = item.get_interfaceid_by_params(host_template['hostid'], params) |
| 672 |
if 'interface' in params: |
| 673 |
del params['interface'] |
| 674 |
results.append(item.update_item(params)) |
| 675 |
changed_item = item.check_item_changed(i) |
| 676 |
if changed_item: |
| 677 |
changed = True |
| 678 |
module.exit_json(changed=changed, result=results) |
| 679 |
|
| 680 |
|
| 681 |
if __name__ == '__main__': |
| 682 |
main() |