-
Notifications
You must be signed in to change notification settings - Fork 286
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add zabbix_regexp_info module #1415
Open
masa-orca
wants to merge
2
commits into
ansible-collections:main
Choose a base branch
from
masa-orca:add_regexp_info
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
minor_changes: | ||
- zabbix_regexp_info module added |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
#!/usr/bin/python | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright: (c) 2024, ONODERA Masaru <[email protected]> | ||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) | ||
|
||
|
||
from __future__ import absolute_import, division, print_function | ||
|
||
__metaclass__ = type | ||
|
||
|
||
DOCUMENTATION = """ | ||
--- | ||
module: zabbix_regexp_info | ||
|
||
short_description: Retrieve Zabbix regular expression | ||
|
||
|
||
description: | ||
- This module allows you to retrieve Zabbix regular expression. | ||
|
||
author: | ||
- ONODERA Masaru(@masa-orca) | ||
|
||
requirements: | ||
- "python >= 3.9" | ||
|
||
version_added: 3.2.0 | ||
|
||
options: | ||
name: | ||
description: | ||
- Name of this regular expression | ||
type: str | ||
required: true | ||
|
||
extends_documentation_fragment: | ||
- community.zabbix.zabbix | ||
""" | ||
|
||
EXAMPLES = """ | ||
# If you want to use Username and Password to be authenticated by Zabbix Server | ||
- name: Set credentials to access Zabbix Server API | ||
ansible.builtin.set_fact: | ||
ansible_user: Admin | ||
ansible_httpapi_pass: zabbix | ||
|
||
# If you want to use API token to be authenticated by Zabbix Server | ||
# https://www.zabbix.com/documentation/current/en/manual/web_interface/frontend_sections/administration/general#api-tokens | ||
- name: Set API token | ||
ansible.builtin.set_fact: | ||
ansible_zabbix_auth_key: 8ec0d52432c15c91fcafe9888500cf9a607f44091ab554dbee860f6b44fac895 | ||
|
||
- name: Retrieve regexp of 'File systems for discovery' | ||
# 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_regexp_info: | ||
name: File systems for discovery | ||
""" | ||
|
||
RETURN = """ | ||
regexp: | ||
description: A Zabbix regular expression which is converted value to be used in community.zabbix.zabbix_regexp module. | ||
returned: success | ||
type: dict | ||
contains: | ||
name: | ||
description: Name of the regular expression | ||
type: str | ||
sample: File systems for discovery | ||
expressions: | ||
description: Expressions of the regular expression | ||
type: list | ||
sample: [{"case_sensitive": false, "expression_type": "result_is_true", ...}] | ||
test_string: | ||
description: Test string of the regular expression | ||
type: str | ||
sample: ext3 | ||
""" | ||
|
||
|
||
from ansible.module_utils.basic import AnsibleModule | ||
|
||
from ansible_collections.community.zabbix.plugins.module_utils.base import ZabbixBase | ||
|
||
import ansible_collections.community.zabbix.plugins.module_utils.helpers as zabbix_utils | ||
|
||
|
||
class RegularExpression(ZabbixBase): | ||
expression_type_values = [ | ||
"character_string_included", | ||
"any_character_string_included", | ||
"character_string_not_included", | ||
"result_is_true", | ||
"result_is_false", | ||
] | ||
|
||
exp_delimiter_values = [",", ".", "/"] | ||
|
||
def __init__(self, module, zbx=None, zapi_wrapper=None): | ||
super(RegularExpression, self).__init__(module, zbx, zapi_wrapper) | ||
|
||
def get_regexp(self, regexp_name): | ||
try: | ||
regexps = self._zapi.regexp.get( | ||
{ | ||
"output": "extend", | ||
"selectExpressions": [ | ||
"expression", | ||
"expression_type", | ||
"exp_delimiter", | ||
"case_sensitive", | ||
], | ||
"filter": {"name": regexp_name}, | ||
} | ||
) | ||
if len(regexps) >= 2: | ||
self._module.fail_json("Too many regexps are matched.") | ||
except Exception as e: | ||
self._module.fail_json( | ||
msg="Failed to get regular expression setting: %s" % e | ||
) | ||
regexp_json = regexps[0] | ||
|
||
expressions = [] | ||
|
||
for _expression in regexp_json['expressions']: | ||
case_sensitive = True | ||
if _expression['case_sensitive'] == '0': | ||
case_sensitive = False | ||
|
||
expression = { | ||
'expression': _expression['expression'], | ||
'expression_type': self.expression_type_values[int(_expression['expression_type'])], | ||
'case_sensitive': case_sensitive | ||
} | ||
|
||
if _expression['expression_type'] == '1': | ||
expression['exp_delimiter'] = _expression['exp_delimiter'] | ||
|
||
expressions.append(expression) | ||
|
||
regexp = { | ||
'name': regexp_name, | ||
'test_string': regexp_json['test_string'], | ||
'expressions': expressions | ||
} | ||
|
||
self._module.exit_json(regexp=regexp) | ||
|
||
|
||
def main(): | ||
"""Main ansible module function""" | ||
|
||
argument_spec = zabbix_utils.zabbix_common_argument_spec() | ||
argument_spec.update( | ||
dict(name=dict(type="str", required=True)) | ||
) | ||
|
||
module = AnsibleModule( | ||
argument_spec=argument_spec, | ||
supports_check_mode=True | ||
) | ||
|
||
name = module.params["name"] | ||
|
||
regexp_class_obj = RegularExpression(module) | ||
regexp_class_obj.get_regexp(name) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
tests/integration/targets/test_zabbix_regexp_info/meta/main.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
--- | ||
dependencies: | ||
- setup_zabbix |
31 changes: 31 additions & 0 deletions
31
tests/integration/targets/test_zabbix_regexp_info/tasks/main.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
--- | ||
- name: test - try to update regexp (present) | ||
community.zabbix.zabbix_regexp: | ||
name: File systems for discovery | ||
test_string: ext3 | ||
expressions: | ||
- expression: "^(btrfs|ext2|ext3|ext4|reiser|xfs|ffs|ufs|jfs|jfs2|vxfs|hfs|apfs|refs|ntfs|fat32|zfs)$" | ||
expression_type: result_is_true | ||
register: zbxregexp_update | ||
|
||
- name: assert that regexp was NOT updated | ||
ansible.builtin.assert: | ||
that: | ||
- zbxregexp_update.changed is sameas False | ||
|
||
- name: test - try to retrieve regexp | ||
community.zabbix.zabbix_regexp_info: | ||
name: File systems for discovery | ||
register: zbxregexp_get | ||
|
||
- name: test - try to update regexp with retrieved values | ||
community.zabbix.zabbix_regexp: | ||
name: "{{ zbxregexp_get.regexp.name }}" | ||
test_string: "{{ zbxregexp_get.regexp.test_string }}" | ||
expressions: "{{ zbxregexp_get.regexp.expressions }}" | ||
register: zbxregexp_update | ||
|
||
- name: assert that regexp was NOT updated | ||
ansible.builtin.assert: | ||
that: | ||
- zbxregexp_update.changed is sameas False |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't use zbxregexp_get anywhere, should you create an "assert" for this?