Skip to content

Commit

Permalink
Add Support to Item Modules for setting Master Items by Name (#1234)
Browse files Browse the repository at this point in the history
Co-authored-by: Andrew Lathrop <[email protected]>
  • Loading branch information
aplathrop and Andrew Lathrop authored May 29, 2024
1 parent 7278802 commit c665229
Show file tree
Hide file tree
Showing 5 changed files with 274 additions and 3 deletions.
3 changes: 3 additions & 0 deletions changelogs/fragments/pr_1234.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
minor_changes:
- zabbix_item - add support for setting master items by name
- zabbix_itemprototype - add support for setting master items by name
63 changes: 63 additions & 0 deletions plugins/modules/zabbix_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,29 @@
- log
- numeric_unsigned
- text
master_item:
description:
- item that is the master of the current one
- Overrides "master_itemid" in API docs
required: false
type: dict
suboptions:
item_name:
description:
- name of the master item
required: true
type: str
host_name:
description:
- name of the host the master item belongs to
- Required when I(template_name) is not used.
- Mutually exclusive with I(template_name).
required: false
template_name:
description:
- name of the template the master item belongs to
- Required when I(host_name) is not used.
- Mutually exclusive with I(host_name).
preprocessing:
description:
- Item preprocessing options.
Expand Down Expand Up @@ -260,6 +283,36 @@
value: application
state: present
- name: create a dependent item
# set task level variables as we change ansible_connection plugin here
vars:
ansible_network_os: community.zabbix.zabbix
ansible_connection: httpapi
ansible_httpapi_port: 443
ansible_httpapi_use_ssl: true
ansible_httpapi_validate_certs: false
ansible_zabbix_url_path: "zabbixeu" # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu
ansible_host: zabbix-example-fqdn.org
community.zabbix.zabbix_item:
name: depend_item
host_name: example_host
params:
type: dependent_item
key: vfs.fs.pused
value_type: numeric_float
units: '%'
master_item:
item_name: example_item
host_name: example_host
preprocessing:
- type: jsonpath
params: '$[?(@.fstype == "ext4")]'
error_handler: zabbix_server
- type: jsonpath
params: "$[*].['bytes', 'inodes'].pused.max()"
error_handler: zabbix_server
state: present
- name: Delete Zabbix item
# set task level variables as we change ansible_connection plugin here
vars:
Expand Down Expand Up @@ -401,6 +454,16 @@ def sanitize_params(self, name, params):
params['status'] = 1
else:
self._module.fail_json(msg="Status must be 'enabled' or 'disabled', got %s" % status)
if 'master_item' in params:
if 'host_name' not in params['master_item']:
params['master_item']['host_name'] = None
if 'template_name' not in params['master_item']:
params['master_item']['template_name'] = None
master_items = self.get_items(params['master_item']['item_name'], params['master_item']['host_name'], params['master_item']['template_name'])
if len(master_items) == 0:
self._module.fail_json(msg="No items with the name %s exist to depend on" % params['master_item']['item_name'])
params['master_itemid'] = master_items[0]['itemid']
params.pop('master_item')
if 'preprocessing' in params:
for param in params['preprocessing']:
preprocess_type_int = self.PREPROCESSING_TYPES[param['type']]
Expand Down
68 changes: 68 additions & 0 deletions plugins/modules/zabbix_itemprototype.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,34 @@
- log
- numeric_unsigned
- text
master_item:
description:
- item that is the master of the current one
- Overrides "master_itemid" in API docs
required: false
type: dict
suboptions:
item_name:
description:
- name of the master item
required: true
type: str
discovery_rule:
description:
- name of the discovery rule the master item belongs to
required: true
type: str
host_name:
description:
- name of the host the master item belongs to
- Required when I(template_name) is not used.
- Mutually exclusive with I(template_name).
required: false
template_name:
description:
- name of the template the master item belongs to
- Required when I(host_name) is not used.
- Mutually exclusive with I(host_name).
preprocessing:
description:
- Item preprocessing options.
Expand Down Expand Up @@ -272,6 +300,35 @@
value: application
state: present
- name: create dependent item
# set task level variables as we change ansible_connection plugin here
vars:
ansible_network_os: community.zabbix.zabbix
ansible_connection: httpapi
ansible_httpapi_port: 443
ansible_httpapi_use_ssl: true
ansible_httpapi_validate_certs: false
ansible_zabbix_url_path: 'zabbixeu' # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu
ansible_host: zabbix-example-fqdn.org
community.zabbix.zabbix_itemprototype:
name: '{% raw %}{#FSNAME}:example_depend_item_prototype{% endraw %}'
discoveryrule_name: example_rule
host_name: example_host
params:
type: dependent_item
key: '{% raw %}vfs.fs.size.half[{#FSNAME}]{% endraw %}'
value_type: numeric_float
units: B
master_item:
item_name: '{% raw %}{#FSNAME}:example_item_prototype{% endraw %}'
discoveryrule_name: example_rule
host_name: example_host
preprocessing:
- type: javascript
params: 'return value / 2;'
error_handler: zabbix_server
state: present
- name: Delete Zabbix item prototype
# set task level variables as we change ansible_connection plugin here
vars:
Expand Down Expand Up @@ -423,6 +480,17 @@ def sanitize_params(self, name, discoveryrule_name, params, host_name=None, temp
if 'enabled' in params:
params['status'] = params['enabled']
params.pop('enabled')
if 'master_item' in params:
if 'host_name' not in params['master_item']:
params['master_item']['host_name'] = None
if 'template_name' not in params['master_item']:
params['master_item']['template_name'] = None
master_items = self.get_itemprototypes(params['master_item']['item_name'], params['master_item']['discoveryrule_name'],
params['master_item']['host_name'], params['master_item']['template_name'])
if len(master_items) == 0:
self._module.fail_json(msg="No items with the name %s exist to depend on" % params['master_item']['item_name'])
params['master_itemid'] = master_items[0]['itemid']
params.pop('master_item')
if 'preprocessing' in params:
for param in params['preprocessing']:
preprocess_type_int = self.PREPROCESSING_TYPES[param['type']]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,73 @@
ansible.builtin.assert:
that: not zbxhostitem_missing_delete is changed

- name: test - create new Zabbix master item on host
community.zabbix.zabbix_item:
name: TestItem
host_name: ExampleHost
params:
type: zabbix_agent_active
key: vfs.fs.get
value_type: text
interval: 1m
tags:
- tag: tag
value: value
state: present
register: zbxhostmstitem_new

- name: assert that item was created
ansible.builtin.assert:
that: zbxhostmstitem_new is changed

- name: create dependent item
community.zabbix.zabbix_item:
name: TestDependItem
host_name: ExampleHost
params:
type: dependent_item
key: vfs.fs.pused
value_type: numeric_float
units: '%'
master_item:
item_name: TestItem
host_name: ExampleHost
preprocessing:
- type: jsonpath
params: '$[?(@.fstype == "ext4")]'
error_handler: zabbix_server
- type: jsonpath
params: "$[*].['bytes', 'inodes'].pused.max()"
error_handler: zabbix_server
state: present
register: zbxhostdependitem_new

- name: assert that item was created
ansible.builtin.assert:
that: zbxhostdependitem_new is changed

- name: test - attempt to delete previously created zabbix master item
community.zabbix.zabbix_item:
name: TestItem
host_name: ExampleHost
state: absent
register: zbxhostmstitem_existing_delete

- name: assert that item was deleted
ansible.builtin.assert:
that: zbxhostmstitem_existing_delete is changed

- name: test - attempt to delete dependent item
community.zabbix.zabbix_item:
name: TestDependItem
host_name: ExampleHost
state: absent
register: zbxhostdependitem_delete

- name: assert that the item had been removed with its master
ansible.builtin.assert:
that: not zbxhostdependitem_delete is changed

- name: test - create new Zabbix item on template with many options set
community.zabbix.zabbix_item:
name: TestItem
Expand Down Expand Up @@ -116,7 +183,7 @@
ansible.builtin.assert:
that: zbxtempitem_new is changed

- name: test - create same Zabbix item group once again
- name: test - create same Zabbix item once again
community.zabbix.zabbix_item:
name: TestItem
template_name: ExampleTemplate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,76 @@
ansible.builtin.assert:
that: not zbxhostitem_missing_delete is changed

- name: test - create new Zabbix master item on host
community.zabbix.zabbix_itemprototype:
name: '{% raw %}{#FSNAME}:TestItemPrototype{% endraw %}'
discoveryrule_name: ExampleHostRule
host_name: ExampleHost
params:
type: zabbix_agent_active
key: '{% raw %}vfs.fs.size[{#FSNAME},used]{% endraw %}'
value_type: numeric_unsigned
units: B
interval: 1m
tags:
- tag: tag
value: value
state: present
register: zbxhostmstitem_new

- name: assert that item was created
ansible.builtin.assert:
that: zbxhostmstitem_new is changed

- name: create dependent item
community.zabbix.zabbix_itemprototype:
name: '{% raw %}{#FSNAME}:TestDependItemPrototype{% endraw %}'
discoveryrule_name: ExampleHostRule
host_name: ExampleHost
params:
type: dependent_item
key: '{% raw %}vfs.fs.size.half[{#FSNAME}]{% endraw %}'
value_type: numeric_float
units: B
master_item:
item_name: '{% raw %}{#FSNAME}:TestItemPrototype{% endraw %}'
discoveryrule_name: ExampleHostRule
host_name: ExampleHost
preprocessing:
- type: javascript
params: 'return value / 2;'
error_handler: zabbix_server
state: present
register: zbxhostdependitem_new

- name: assert that item was created
ansible.builtin.assert:
that: zbxhostdependitem_new is changed

- name: test - attempt to delete previously created zabbix item
community.zabbix.zabbix_itemprototype:
name: '{% raw %}{#FSNAME}:TestItemPrototype{% endraw %}'
discoveryrule_name: ExampleHostRule
host_name: ExampleHost
state: absent
register: zbxhostmstitem_existing_delete

- name: assert that item was deleted
ansible.builtin.assert:
that: zbxhostmstitem_existing_delete is changed

- name: test - attempt to delete dependent item
community.zabbix.zabbix_itemprototype:
name: '{% raw %}{#FSNAME}:TestDependItemPrototype{% endraw %}'
discoveryrule_name: ExampleHostRule
host_name: ExampleHost
state: absent
register: zbxhostdependitem_delete

- name: assert that the item had been removed with its master
ansible.builtin.assert:
that: not zbxhostdependitem_delete is changed

- name: remove host rule
community.zabbix.zabbix_discoveryrule:
name: ExampleHostRule
Expand All @@ -106,7 +176,7 @@
type: zabbix_agent_active
key: '{% raw %}vfs.fs.size[{#FSNAME},used]{% endraw %}'
value_type: numeric_unsigned
units: GB
units: B
interval: 1m
tags:
- tag: tag
Expand All @@ -127,7 +197,7 @@
type: zabbix_agent_active
key: '{% raw %}vfs.fs.size[{#FSNAME},used]{% endraw %}'
value_type: numeric_unsigned
units: GB
units: B
interval: 1m
tags:
- tag: tag
Expand Down

0 comments on commit c665229

Please sign in to comment.