From 2d35caf68b16b69c9de337b00322f429a443ad4e Mon Sep 17 00:00:00 2001 From: Rene Moser Date: Fri, 22 May 2015 14:33:56 +0200 Subject: [PATCH 1/7] zabbix_group: remove current module in favor of PR #346 --- monitoring/zabbix_group.py | 214 ------------------------------------- 1 file changed, 214 deletions(-) delete mode 100644 monitoring/zabbix_group.py diff --git a/monitoring/zabbix_group.py b/monitoring/zabbix_group.py deleted file mode 100644 index f622de5a4f7..00000000000 --- a/monitoring/zabbix_group.py +++ /dev/null @@ -1,214 +0,0 @@ -#!/usr/bin/python -# -*- coding: utf-8 -*- - -# (c) 2014, René Moser -# -# This file is part of Ansible -# -# Ansible is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# Ansible is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with Ansible. If not, see . - - -DOCUMENTATION = ''' ---- -module: zabbix_group -short_description: Add or remove a host group to Zabbix. -description: - - This module uses the Zabbix API to add and remove host groups. -version_added: '1.8' -requirements: - - "python >= 2.6" - - zabbix-api -options: - state: - description: - - Whether the host group should be added or removed. - required: false - default: present - choices: [ 'present', 'absent' ] - host_group: - description: - - Name of the host group to be added or removed. - required: true - default: null - aliases: [ ] - server_url: - description: - - Url of Zabbix server, with protocol (http or https) e.g. - https://monitoring.example.com/zabbix. C(url) is an alias - for C(server_url). If not set environment variable - C(ZABBIX_SERVER_URL) is used. - required: true - default: null - aliases: [ 'url' ] - login_user: - description: - - Zabbix user name. If not set environment variable - C(ZABBIX_LOGIN_USER) is used. - required: true - default: null - login_password: - description: - - Zabbix user password. If not set environment variable - C(ZABBIX_LOGIN_PASSWORD) is used. - required: true -notes: - - The module has been tested with Zabbix Server 2.2. -author: '"René Moser (@resmo)" ' -''' - -EXAMPLES = ''' ---- -# Add a new host group to Zabbix -- zabbix_group: host_group='Linux servers' - server_url=https://monitoring.example.com/zabbix - login_user=ansible - login_password=secure - -# Add a new host group, login data is provided by environment variables: -# ZABBIX_LOGIN_USER, ZABBIX_LOGIN_PASSWORD, ZABBIX_SERVER_URL: -- zabbix_group: host_group=Webservers - -# Remove a host group from Zabbix -- zabbix_group: host_group='Linux servers' - state=absent - server_url=https://monitoring.example.com/zabbix - login_user=ansible - login_password=secure -''' - -try: - from zabbix_api import ZabbixAPI - HAS_ZABBIX_API = True -except ImportError: - HAS_ZABBIX_API = False - - -def create_group(zbx, host_group): - try: - result = zbx.hostgroup.create( - { - 'name': host_group - } - ) - except BaseException as e: - return 1, None, str(e) - return 0, result['groupids'], None - - -def get_group(zbx, host_group): - try: - result = zbx.hostgroup.get( - { - 'filter': - { - 'name': host_group, - } - } - ) - except BaseException as e: - return 1, None, str(e) - - return 0, result[0]['groupid'], None - - -def delete_group(zbx, group_id): - try: - zbx.hostgroup.delete([ group_id ]) - except BaseException as e: - return 1, None, str(e) - return 0, None, None - - -def check_group(zbx, host_group): - try: - result = zbx.hostgroup.exists( - { - 'name': host_group - } - ) - except BaseException as e: - return 1, None, str(e) - return 0, result, None - - -def main(): - module = AnsibleModule( - argument_spec=dict( - state=dict(default='present', choices=['present', 'absent']), - host_group=dict(required=True, default=None), - server_url=dict(default=None, aliases=['url']), - login_user=dict(default=None), - login_password=dict(default=None), - ), - supports_check_mode=True, - ) - - if not HAS_ZABBIX_API: - module.fail_json(msg='Missing requried zabbix-api module (check docs or install with: pip install zabbix-api)') - - try: - login_user = module.params['login_user'] or os.environ['ZABBIX_LOGIN_USER'] - login_password = module.params['login_password'] or os.environ['ZABBIX_LOGIN_PASSWORD'] - server_url = module.params['server_url'] or os.environ['ZABBIX_SERVER_URL'] - except KeyError, e: - module.fail_json(msg='Missing login data: %s is not set.' % e.message) - - host_group = module.params['host_group'] - state = module.params['state'] - - try: - zbx = ZabbixAPI(server_url) - zbx.login(login_user, login_password) - except BaseException as e: - module.fail_json(msg='Failed to connect to Zabbix server: %s' % e) - - changed = False - msg = '' - - if state == 'present': - (rc, exists, error) = check_group(zbx, host_group) - if rc != 0: - module.fail_json(msg='Failed to check host group %s existance: %s' % (host_group, error)) - if not exists: - if module.check_mode: - changed = True - else: - (rc, group, error) = create_group(zbx, host_group) - if rc == 0: - changed = True - else: - module.fail_json(msg='Failed to get host group: %s' % error) - - if state == 'absent': - (rc, exists, error) = check_group(zbx, host_group) - if rc != 0: - module.fail_json(msg='Failed to check host group %s existance: %s' % (host_group, error)) - if exists: - if module.check_mode: - changed = True - else: - (rc, group_id, error) = get_group(zbx, host_group) - if rc != 0: - module.fail_json(msg='Failed to get host group: %s' % error) - - (rc, _, error) = delete_group(zbx, group_id) - if rc == 0: - changed = True - else: - module.fail_json(msg='Failed to remove host group: %s' % error) - - module.exit_json(changed=changed) - -from ansible.module_utils.basic import * -main() From 30832ab4d91275412dfe1acccdf846a4d5b860a0 Mon Sep 17 00:00:00 2001 From: Cove Schneider Date: Sat, 28 Mar 2015 08:44:40 -0700 Subject: [PATCH 2/7] add zabbix_group module --- monitoring/zabbix_group.py | 208 +++++++++++++++++++++++++++++++++++++ 1 file changed, 208 insertions(+) create mode 100644 monitoring/zabbix_group.py diff --git a/monitoring/zabbix_group.py b/monitoring/zabbix_group.py new file mode 100644 index 00000000000..447ad927b0b --- /dev/null +++ b/monitoring/zabbix_group.py @@ -0,0 +1,208 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +# (c) 2013-2014, Epic Games, Inc. +# +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see . +# + +DOCUMENTATION = ''' +--- +module: zabbix_group +short_description: Zabbix host groups creates/deletes +description: + - Create host groups if they don't exist. + - Delete existing host groups if they exist. +version_added: "1.9" +author: Tony Minfei Ding, Harrison Gu +requirements: + - zabbix-api python module +options: + server_url: + description: + - Url of Zabbix server, with protocol (http or https). + C(url) is an alias for C(server_url). + required: true + default: null + aliases: [ "url" ] + login_user: + description: + - Zabbix user name. + required: true + default: null + login_password: + description: + - Zabbix user password. + required: true + default: null + state: + description: + - Create or delete host group. + - Possible values are: present and absent. + required: false + default: "present" + timeout: + description: + - The timeout of API request(seconds). + default: 10 + host_groups: + description: + - List of host groups to create or delete. + required: true +notes: + - Too many concurrent updates to the same group may cause Zabbix to return errors, see examples for a workaround if needed. +''' + +EXAMPLES = ''' +# Base create host groups example +- name: Create host groups + local_action: + module: zabbix_group + server_url: http://monitor.example.com + login_user: username + login_password: password + state: present + host_groups: + - Example group1 + - Example group2 + +# Limit the Zabbix group creations to one host since Zabbix can return an error when doing concurent updates +- name: Create host groups + local_action: + module: zabbix_group + server_url: http://monitor.example.com + login_user: username + login_password: password + state: present + host_groups: + - Example group1 + - Example group2 + when: inventory_hostname==groups['group_name'][0] +''' + +try: + from zabbix_api import ZabbixAPI, ZabbixAPISubClass + from zabbix_api import Already_Exists + + HAS_ZABBIX_API = True +except ImportError: + HAS_ZABBIX_API = False + + +class HostGroup(object): + def __init__(self, module, zbx): + self._module = module + self._zapi = zbx + + # create host group(s) if not exists + def create_host_group(self, group_names): + try: + group_add_list = [] + for group_name in group_names: + result = self._zapi.hostgroup.exists({'name': group_name}) + if not result: + try: + if self._module.check_mode: + self._module.exit_json(changed=True) + self._zapi.hostgroup.create({'name': group_name}) + group_add_list.append(group_name) + except Already_Exists: + return group_add_list + return group_add_list + except Exception, e: + self._module.fail_json(msg="Failed to create host group(s): %s" % e) + + # delete host group(s) + def delete_host_group(self, group_ids): + try: + if self._module.check_mode: + self._module.exit_json(changed=True) + self._zapi.hostgroup.delete(group_ids) + except Exception, e: + self._module.fail_json(msg="Failed to delete host group(s), Exception: %s" % e) + + # get group ids by name + def get_group_ids(self, host_groups): + group_ids = [] + + group_list = self._zapi.hostgroup.get({'output': 'extend', 'filter': {'name': host_groups}}) + for group in group_list: + group_id = group['groupid'] + group_ids.append(group_id) + return group_ids, group_list + + +def main(): + module = AnsibleModule( + argument_spec=dict( + server_url=dict(required=True, default=None, aliases=['url']), + login_user=dict(required=True), + login_password=dict(required=True), + host_groups=dict(required=True), + state=dict(default="present"), + timeout=dict(default=10) + ), + supports_check_mode=True + ) + + if not HAS_ZABBIX_API: + module.fail_json(msg="Missing requried zabbix-api module (check docs or install with: pip install zabbix-api)") + + server_url = module.params['server_url'] + login_user = module.params['login_user'] + login_password = module.params['login_password'] + host_groups = module.params['host_groups'] + state = module.params['state'] + timeout = module.params['timeout'] + + zbx = None + + # login to zabbix + try: + zbx = ZabbixAPI(server_url, timeout=timeout) + zbx.login(login_user, login_password) + except Exception, e: + module.fail_json(msg="Failed to connect to Zabbix server: %s" % e) + + hostGroup = HostGroup(module, zbx) + + group_ids = [] + group_list = [] + if host_groups: + group_ids, group_list = hostGroup.get_group_ids(host_groups) + + if state == "absent": + # delete host groups + if group_ids: + delete_group_names = [] + hostGroup.delete_host_group(group_ids) + for group in group_list: + delete_group_names.append(group['name']) + module.exit_json(changed=True, + result="Successfully deleted host group(s): %s." % ",".join(delete_group_names)) + else: + module.exit_json(changed=False, result="No host group(s) to delete.") + else: + # create host groups + group_add_list = hostGroup.create_host_group(host_groups) + if len(group_add_list) > 0: + module.exit_json(changed=True, result="Successfully created host group(s): %s" % group_add_list) + else: + module.exit_json(changed=False) + +from ansible.module_utils.basic import * +main() + From 8175bd2f781098af0ff1ed8e54e3f74ed5e63d3e Mon Sep 17 00:00:00 2001 From: Cove Schneider Date: Sun, 29 Mar 2015 13:42:25 -0700 Subject: [PATCH 3/7] remove superfluous defaults --- monitoring/zabbix_group.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/monitoring/zabbix_group.py b/monitoring/zabbix_group.py index 447ad927b0b..add8e5e0da4 100644 --- a/monitoring/zabbix_group.py +++ b/monitoring/zabbix_group.py @@ -36,18 +36,15 @@ options: - Url of Zabbix server, with protocol (http or https). C(url) is an alias for C(server_url). required: true - default: null aliases: [ "url" ] login_user: description: - Zabbix user name. required: true - default: null login_password: description: - Zabbix user password. required: true - default: null state: description: - Create or delete host group. @@ -148,7 +145,7 @@ class HostGroup(object): def main(): module = AnsibleModule( argument_spec=dict( - server_url=dict(required=True, default=None, aliases=['url']), + server_url=dict(required=True, aliases=['url']), login_user=dict(required=True), login_password=dict(required=True), host_groups=dict(required=True), From f0eb372e995c748bbdf843cf7d48ca65a08a0246 Mon Sep 17 00:00:00 2001 From: Rene Moser Date: Fri, 22 May 2015 14:42:22 +0200 Subject: [PATCH 4/7] zabbix_group: update authors to new format --- monitoring/zabbix_group.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/monitoring/zabbix_group.py b/monitoring/zabbix_group.py index add8e5e0da4..d54009251ff 100644 --- a/monitoring/zabbix_group.py +++ b/monitoring/zabbix_group.py @@ -1,6 +1,6 @@ #!/usr/bin/python # -*- coding: utf-8 -*- - +# # (c) 2013-2014, Epic Games, Inc. # # This file is part of Ansible @@ -17,7 +17,7 @@ # # You should have received a copy of the GNU General Public License # along with Ansible. If not, see . -# + DOCUMENTATION = ''' --- @@ -27,9 +27,13 @@ description: - Create host groups if they don't exist. - Delete existing host groups if they exist. version_added: "1.9" -author: Tony Minfei Ding, Harrison Gu +author: + - "(@cove)" + - "Tony Minfei Ding" + - "Harrison Gu (@harrisongu)" requirements: - - zabbix-api python module + - "python >= 2.6" + - zabbix-api options: server_url: description: From 1c210abc692d5d965028d8f39312d9592cb593bb Mon Sep 17 00:00:00 2001 From: Rene Moser Date: Fri, 22 May 2015 14:43:14 +0200 Subject: [PATCH 5/7] zabbix_group: update version added to 1.8 Since we intent to replace the current module, we add the version the current module was added. --- monitoring/zabbix_group.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/monitoring/zabbix_group.py b/monitoring/zabbix_group.py index d54009251ff..81fe3be5008 100644 --- a/monitoring/zabbix_group.py +++ b/monitoring/zabbix_group.py @@ -26,7 +26,7 @@ short_description: Zabbix host groups creates/deletes description: - Create host groups if they don't exist. - Delete existing host groups if they exist. -version_added: "1.9" +version_added: "1.8" author: - "(@cove)" - "Tony Minfei Ding" From 3f108732c5101973b97087026e3cd51bc558b66d Mon Sep 17 00:00:00 2001 From: Rene Moser Date: Fri, 22 May 2015 14:44:19 +0200 Subject: [PATCH 6/7] zabbix_group: add backword compatibility * add alias host_group for host_groups * add choices for param state --- monitoring/zabbix_group.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/monitoring/zabbix_group.py b/monitoring/zabbix_group.py index 81fe3be5008..8fc88eb6bf5 100644 --- a/monitoring/zabbix_group.py +++ b/monitoring/zabbix_group.py @@ -55,6 +55,7 @@ options: - Possible values are: present and absent. required: false default: "present" + choices: [ "present", "absent" ] timeout: description: - The timeout of API request(seconds). @@ -63,6 +64,7 @@ options: description: - List of host groups to create or delete. required: true + aliases: [ "host_group" ] notes: - Too many concurrent updates to the same group may cause Zabbix to return errors, see examples for a workaround if needed. ''' @@ -152,8 +154,8 @@ def main(): server_url=dict(required=True, aliases=['url']), login_user=dict(required=True), login_password=dict(required=True), - host_groups=dict(required=True), - state=dict(default="present"), + host_groups=dict(required=True, aliases=['host_group']), + state=dict(default="present", choices=['present','absent']), timeout=dict(default=10) ), supports_check_mode=True @@ -206,4 +208,3 @@ def main(): from ansible.module_utils.basic import * main() - From c0fb48cd390aeb673e465257888c3311b77dc274 Mon Sep 17 00:00:00 2001 From: Rene Moser Date: Fri, 22 May 2015 15:05:15 +0200 Subject: [PATCH 7/7] zabbix_group: improve params --- monitoring/zabbix_group.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/monitoring/zabbix_group.py b/monitoring/zabbix_group.py index 8fc88eb6bf5..8976f9f502f 100644 --- a/monitoring/zabbix_group.py +++ b/monitoring/zabbix_group.py @@ -153,10 +153,10 @@ def main(): argument_spec=dict( server_url=dict(required=True, aliases=['url']), login_user=dict(required=True), - login_password=dict(required=True), + login_password=dict(required=True, no_log=True), host_groups=dict(required=True, aliases=['host_group']), state=dict(default="present", choices=['present','absent']), - timeout=dict(default=10) + timeout=dict(type='int', default=10) ), supports_check_mode=True )