Zabbix facts (#41084)
* added validate_certs option to zabbix_*_facts modules and fixed documentation to use doc fragment * removed code duplication from zabbix_host_facts module
This commit is contained in:
parent
457c813d46
commit
6ef2ffe310
2 changed files with 26 additions and 82 deletions
|
@ -34,38 +34,13 @@ requirements:
|
||||||
- "python >= 2.6"
|
- "python >= 2.6"
|
||||||
- zabbix-api
|
- zabbix-api
|
||||||
options:
|
options:
|
||||||
server_url:
|
|
||||||
description:
|
|
||||||
- Url of Zabbix server, with protocol (http or https).
|
|
||||||
required: true
|
|
||||||
aliases: [ "url" ]
|
|
||||||
login_user:
|
|
||||||
description:
|
|
||||||
- Zabbix user name, used to authenticate against the server.
|
|
||||||
required: true
|
|
||||||
login_password:
|
|
||||||
description:
|
|
||||||
- Zabbix user password.
|
|
||||||
required: true
|
|
||||||
http_login_user:
|
|
||||||
description:
|
|
||||||
- Basic Auth login
|
|
||||||
required: false
|
|
||||||
default: null
|
|
||||||
http_login_password:
|
|
||||||
description:
|
|
||||||
- Basic Auth password
|
|
||||||
required: false
|
|
||||||
default: null
|
|
||||||
hostgroup_name:
|
hostgroup_name:
|
||||||
description:
|
description:
|
||||||
- Name of the hostgroup in Zabbix.
|
- Name of the hostgroup in Zabbix.
|
||||||
- hostgroup is the unique identifier used and cannot be updated using this module.
|
- hostgroup is the unique identifier used and cannot be updated using this module.
|
||||||
required: true
|
required: true
|
||||||
timeout:
|
extends_documentation_fragment:
|
||||||
description:
|
- zabbix
|
||||||
- The timeout of API request (seconds).
|
|
||||||
default: 10
|
|
||||||
'''
|
'''
|
||||||
|
|
||||||
EXAMPLES = '''
|
EXAMPLES = '''
|
||||||
|
@ -92,8 +67,8 @@ try:
|
||||||
class ZabbixAPIExtends(ZabbixAPI):
|
class ZabbixAPIExtends(ZabbixAPI):
|
||||||
hostinterface = None
|
hostinterface = None
|
||||||
|
|
||||||
def __init__(self, server, timeout, user, passwd, **kwargs):
|
def __init__(self, server, timeout, user, passwd, validate_certs, **kwargs):
|
||||||
ZabbixAPI.__init__(self, server, timeout=timeout, user=user, passwd=passwd)
|
ZabbixAPI.__init__(self, server, timeout=timeout, user=user, passwd=passwd, validate_certs=validate_certs)
|
||||||
self.hostinterface = ZabbixAPISubClass(self, dict({"prefix": "hostinterface"}, **kwargs))
|
self.hostinterface = ZabbixAPISubClass(self, dict({"prefix": "hostinterface"}, **kwargs))
|
||||||
|
|
||||||
HAS_ZABBIX_API = True
|
HAS_ZABBIX_API = True
|
||||||
|
@ -122,6 +97,7 @@ def main():
|
||||||
hostgroup_name=dict(type='list', required=True),
|
hostgroup_name=dict(type='list', required=True),
|
||||||
http_login_user=dict(type='str', required=False, default=None),
|
http_login_user=dict(type='str', required=False, default=None),
|
||||||
http_login_password=dict(type='str', required=False, default=None, no_log=True),
|
http_login_password=dict(type='str', required=False, default=None, no_log=True),
|
||||||
|
validate_certs=dict(type='bool', required=False, default=True),
|
||||||
timeout=dict(type='int', default=10)
|
timeout=dict(type='int', default=10)
|
||||||
),
|
),
|
||||||
supports_check_mode=True
|
supports_check_mode=True
|
||||||
|
@ -135,13 +111,15 @@ def main():
|
||||||
login_password = module.params['login_password']
|
login_password = module.params['login_password']
|
||||||
http_login_user = module.params['http_login_user']
|
http_login_user = module.params['http_login_user']
|
||||||
http_login_password = module.params['http_login_password']
|
http_login_password = module.params['http_login_password']
|
||||||
|
validate_certs = module.params['validate_certs']
|
||||||
hostgroup_name = module.params['hostgroup_name']
|
hostgroup_name = module.params['hostgroup_name']
|
||||||
timeout = module.params['timeout']
|
timeout = module.params['timeout']
|
||||||
|
|
||||||
zbx = None
|
zbx = None
|
||||||
# login to zabbix
|
# login to zabbix
|
||||||
try:
|
try:
|
||||||
zbx = ZabbixAPIExtends(server_url, timeout=timeout, user=http_login_user, passwd=http_login_password)
|
zbx = ZabbixAPIExtends(server_url, timeout=timeout, user=http_login_user, passwd=http_login_password,
|
||||||
|
validate_certs=validate_certs)
|
||||||
zbx.login(login_user, login_password)
|
zbx.login(login_user, login_password)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
module.fail_json(msg="Failed to connect to Zabbix server: %s" % e)
|
module.fail_json(msg="Failed to connect to Zabbix server: %s" % e)
|
||||||
|
|
|
@ -35,29 +35,6 @@ requirements:
|
||||||
- "python >= 2.6"
|
- "python >= 2.6"
|
||||||
- zabbix-api
|
- zabbix-api
|
||||||
options:
|
options:
|
||||||
server_url:
|
|
||||||
description:
|
|
||||||
- Url of Zabbix server, with protocol (http or https).
|
|
||||||
required: true
|
|
||||||
aliases: [ "url" ]
|
|
||||||
login_user:
|
|
||||||
description:
|
|
||||||
- Zabbix user name, used to authenticate against the server.
|
|
||||||
required: true
|
|
||||||
login_password:
|
|
||||||
description:
|
|
||||||
- Zabbix user password.
|
|
||||||
required: true
|
|
||||||
http_login_user:
|
|
||||||
description:
|
|
||||||
- Basic Auth login.
|
|
||||||
required: false
|
|
||||||
default: null
|
|
||||||
http_login_password:
|
|
||||||
description:
|
|
||||||
- Basic Auth password.
|
|
||||||
required: false
|
|
||||||
default: null
|
|
||||||
host_name:
|
host_name:
|
||||||
description:
|
description:
|
||||||
- Name of the host in Zabbix.
|
- Name of the host in Zabbix.
|
||||||
|
@ -67,10 +44,6 @@ options:
|
||||||
description:
|
description:
|
||||||
- Host interface IP of the host in Zabbix.
|
- Host interface IP of the host in Zabbix.
|
||||||
required: false
|
required: false
|
||||||
timeout:
|
|
||||||
description:
|
|
||||||
- The timeout of API request (seconds).
|
|
||||||
default: 10
|
|
||||||
exact_match:
|
exact_match:
|
||||||
description:
|
description:
|
||||||
- Find the exact match
|
- Find the exact match
|
||||||
|
@ -81,6 +54,8 @@ options:
|
||||||
- Remove duplicate host from host result
|
- Remove duplicate host from host result
|
||||||
type: bool
|
type: bool
|
||||||
default: yes
|
default: yes
|
||||||
|
extends_documentation_fragment:
|
||||||
|
- zabbix
|
||||||
'''
|
'''
|
||||||
|
|
||||||
EXAMPLES = '''
|
EXAMPLES = '''
|
||||||
|
@ -109,8 +84,8 @@ try:
|
||||||
class ZabbixAPIExtends(ZabbixAPI):
|
class ZabbixAPIExtends(ZabbixAPI):
|
||||||
hostinterface = None
|
hostinterface = None
|
||||||
|
|
||||||
def __init__(self, server, timeout, user, passwd, **kwargs):
|
def __init__(self, server, timeout, user, passwd, validate_certs, **kwargs):
|
||||||
ZabbixAPI.__init__(self, server, timeout=timeout, user=user, passwd=passwd)
|
ZabbixAPI.__init__(self, server, timeout=timeout, user=user, passwd=passwd, validate_certs=validate_certs)
|
||||||
self.hostinterface = ZabbixAPISubClass(self, dict({"prefix": "hostinterface"}, **kwargs))
|
self.hostinterface = ZabbixAPISubClass(self, dict({"prefix": "hostinterface"}, **kwargs))
|
||||||
|
|
||||||
HAS_ZABBIX_API = True
|
HAS_ZABBIX_API = True
|
||||||
|
@ -123,14 +98,6 @@ class Host(object):
|
||||||
self._module = module
|
self._module = module
|
||||||
self._zapi = zbx
|
self._zapi = zbx
|
||||||
|
|
||||||
def is_host_exist(self, host_name, exact_match):
|
|
||||||
""" Check host exists """
|
|
||||||
search_key = 'search'
|
|
||||||
if exact_match:
|
|
||||||
search_key = 'filter'
|
|
||||||
result = self._zapi.host.get({search_key: {'host': host_name}})
|
|
||||||
return result
|
|
||||||
|
|
||||||
def get_hosts_by_host_name(self, host_name, exact_match):
|
def get_hosts_by_host_name(self, host_name, exact_match):
|
||||||
""" Get host by host name """
|
""" Get host by host name """
|
||||||
search_key = 'search'
|
search_key = 'search'
|
||||||
|
@ -186,6 +153,7 @@ def main():
|
||||||
host_ip=dict(type='list', default=[], required=False),
|
host_ip=dict(type='list', default=[], required=False),
|
||||||
http_login_user=dict(type='str', required=False, default=None),
|
http_login_user=dict(type='str', required=False, default=None),
|
||||||
http_login_password=dict(type='str', required=False, default=None, no_log=True),
|
http_login_password=dict(type='str', required=False, default=None, no_log=True),
|
||||||
|
validate_certs=dict(type='bool', required=False, default=True),
|
||||||
timeout=dict(type='int', default=10),
|
timeout=dict(type='int', default=10),
|
||||||
exact_match=dict(type='bool', required=False, default=False),
|
exact_match=dict(type='bool', required=False, default=False),
|
||||||
remove_duplicate=dict(type='bool', required=False, default=True)
|
remove_duplicate=dict(type='bool', required=False, default=True)
|
||||||
|
@ -201,6 +169,7 @@ def main():
|
||||||
login_password = module.params['login_password']
|
login_password = module.params['login_password']
|
||||||
http_login_user = module.params['http_login_user']
|
http_login_user = module.params['http_login_user']
|
||||||
http_login_password = module.params['http_login_password']
|
http_login_password = module.params['http_login_password']
|
||||||
|
validate_certs = module.params['validate_certs']
|
||||||
host_name = module.params['host_name']
|
host_name = module.params['host_name']
|
||||||
host_ips = module.params['host_ip']
|
host_ips = module.params['host_ip']
|
||||||
timeout = module.params['timeout']
|
timeout = module.params['timeout']
|
||||||
|
@ -210,7 +179,8 @@ def main():
|
||||||
zbx = None
|
zbx = None
|
||||||
# login to zabbix
|
# login to zabbix
|
||||||
try:
|
try:
|
||||||
zbx = ZabbixAPIExtends(server_url, timeout=timeout, user=http_login_user, passwd=http_login_password)
|
zbx = ZabbixAPIExtends(server_url, timeout=timeout, user=http_login_user, passwd=http_login_password,
|
||||||
|
validate_certs=validate_certs)
|
||||||
zbx.login(login_user, login_password)
|
zbx.login(login_user, login_password)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
module.fail_json(msg="Failed to connect to Zabbix server: %s" % e)
|
module.fail_json(msg="Failed to connect to Zabbix server: %s" % e)
|
||||||
|
@ -218,21 +188,17 @@ def main():
|
||||||
host = Host(module, zbx)
|
host = Host(module, zbx)
|
||||||
|
|
||||||
if host_name:
|
if host_name:
|
||||||
is_host_exist = host.is_host_exist(host_name, exact_match)
|
hosts = host.get_hosts_by_host_name(host_name, exact_match)
|
||||||
|
if is_remove_duplicate:
|
||||||
|
hosts = host.delete_duplicate_hosts(hosts)
|
||||||
|
extended_hosts = []
|
||||||
|
for zabbix_host in hosts:
|
||||||
|
zabbix_host['hostinterfaces'] = host._zapi.hostinterface.get({
|
||||||
|
'output': 'extend', 'hostids': zabbix_host['hostid']
|
||||||
|
})
|
||||||
|
extended_hosts.append(zabbix_host)
|
||||||
|
module.exit_json(ok=True, hosts=extended_hosts)
|
||||||
|
|
||||||
if is_host_exist:
|
|
||||||
hosts = host.get_hosts_by_host_name(host_name, exact_match)
|
|
||||||
if is_remove_duplicate:
|
|
||||||
hosts = host.delete_duplicate_hosts(hosts)
|
|
||||||
extended_hosts = []
|
|
||||||
for zabbix_host in hosts:
|
|
||||||
zabbix_host['hostinterfaces'] = host._zapi.hostinterface.get({
|
|
||||||
'output': 'extend', 'hostids': zabbix_host['hostid']
|
|
||||||
})
|
|
||||||
extended_hosts.append(zabbix_host)
|
|
||||||
module.exit_json(ok=True, hosts=extended_hosts)
|
|
||||||
else:
|
|
||||||
module.exit_json(ok=False, hosts=[], result="No Host present")
|
|
||||||
elif host_ips:
|
elif host_ips:
|
||||||
extended_hosts = host.get_hosts_by_ip(host_ips)
|
extended_hosts = host.get_hosts_by_ip(host_ips)
|
||||||
if is_remove_duplicate:
|
if is_remove_duplicate:
|
||||||
|
|
Loading…
Reference in a new issue