2013-08-22 22:55:48 +02:00
|
|
|
#!/usr/bin/env python
|
2013-08-06 18:19:06 +02:00
|
|
|
|
|
|
|
# (c) 2013, Greg Buehler
|
2018-12-01 01:49:57 +01:00
|
|
|
# (c) 2018, Filippo Ferrazini
|
2013-08-06 18:19:06 +02:00
|
|
|
#
|
|
|
|
# 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 <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
"""
|
2014-11-10 10:41:44 +01:00
|
|
|
Zabbix Server external inventory script.
|
2013-08-22 22:55:48 +02:00
|
|
|
========================================
|
2013-08-06 18:19:06 +02:00
|
|
|
|
2013-08-22 22:55:48 +02:00
|
|
|
Returns hosts and hostgroups from Zabbix Server.
|
2017-06-28 18:41:03 +02:00
|
|
|
If you want to run with --limit against a host group with space in the
|
2017-06-28 17:48:27 +02:00
|
|
|
name, use asterisk. For example --limit="Linux*servers".
|
2013-08-06 18:19:06 +02:00
|
|
|
|
2013-08-22 22:55:48 +02:00
|
|
|
Configuration is read from `zabbix.ini`.
|
|
|
|
|
2018-12-01 01:49:57 +01:00
|
|
|
Tested with Zabbix Server 2.0.6, 3.2.3 and 3.4.
|
2013-08-06 18:19:06 +02:00
|
|
|
"""
|
|
|
|
|
2015-08-28 08:18:13 +02:00
|
|
|
from __future__ import print_function
|
|
|
|
|
2017-01-27 22:04:59 +01:00
|
|
|
import os
|
|
|
|
import sys
|
2013-08-06 18:19:06 +02:00
|
|
|
import argparse
|
2017-08-28 20:12:32 +02:00
|
|
|
try:
|
|
|
|
import ConfigParser as configparser
|
|
|
|
except ImportError:
|
|
|
|
import configparser
|
2013-08-22 22:55:48 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
from zabbix_api import ZabbixAPI
|
2018-09-08 02:59:46 +02:00
|
|
|
except Exception:
|
2015-08-28 08:18:13 +02:00
|
|
|
print("Error: Zabbix API library must be installed: pip install zabbix-api.",
|
|
|
|
file=sys.stderr)
|
2013-08-22 22:55:48 +02:00
|
|
|
sys.exit(1)
|
2013-08-06 18:19:06 +02:00
|
|
|
|
2018-08-10 18:13:29 +02:00
|
|
|
import json
|
2013-08-06 18:19:06 +02:00
|
|
|
|
2017-05-09 23:38:08 +02:00
|
|
|
|
2013-08-06 18:19:06 +02:00
|
|
|
class ZabbixInventory(object):
|
|
|
|
|
|
|
|
def read_settings(self):
|
2017-08-28 20:12:32 +02:00
|
|
|
config = configparser.SafeConfigParser()
|
2015-10-08 10:44:27 +02:00
|
|
|
conf_path = './zabbix.ini'
|
|
|
|
if not os.path.exists(conf_path):
|
2017-01-28 09:12:11 +01:00
|
|
|
conf_path = os.path.dirname(os.path.realpath(__file__)) + '/zabbix.ini'
|
2015-10-08 10:44:27 +02:00
|
|
|
if os.path.exists(conf_path):
|
2017-01-28 09:12:11 +01:00
|
|
|
config.read(conf_path)
|
2013-08-06 18:19:06 +02:00
|
|
|
# server
|
|
|
|
if config.has_option('zabbix', 'server'):
|
|
|
|
self.zabbix_server = config.get('zabbix', 'server')
|
|
|
|
|
2014-11-10 10:41:44 +01:00
|
|
|
# login
|
2013-08-06 18:19:06 +02:00
|
|
|
if config.has_option('zabbix', 'username'):
|
|
|
|
self.zabbix_username = config.get('zabbix', 'username')
|
|
|
|
if config.has_option('zabbix', 'password'):
|
|
|
|
self.zabbix_password = config.get('zabbix', 'password')
|
2018-07-14 15:10:16 +02:00
|
|
|
# ssl certs
|
|
|
|
if config.has_option('zabbix', 'validate_certs'):
|
|
|
|
if config.get('zabbix', 'validate_certs') in ['false', 'False', False]:
|
|
|
|
self.validate_certs = False
|
2018-12-01 01:49:57 +01:00
|
|
|
# host inventory
|
|
|
|
if config.has_option('zabbix', 'read_host_inventory'):
|
|
|
|
if config.get('zabbix', 'read_host_inventory') in ['true', 'True', True]:
|
|
|
|
self.read_host_inventory = True
|
|
|
|
# host interface
|
|
|
|
if config.has_option('zabbix', 'use_host_interface'):
|
|
|
|
if config.get('zabbix', 'use_host_interface') in ['false', 'False', False]:
|
|
|
|
self.use_host_interface = False
|
2013-08-06 18:19:06 +02:00
|
|
|
|
|
|
|
def read_cli(self):
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument('--host')
|
|
|
|
parser.add_argument('--list', action='store_true')
|
|
|
|
self.options = parser.parse_args()
|
|
|
|
|
|
|
|
def hoststub(self):
|
|
|
|
return {
|
|
|
|
'hosts': []
|
|
|
|
}
|
|
|
|
|
|
|
|
def get_host(self, api, name):
|
2018-12-01 01:49:57 +01:00
|
|
|
api_query = {'output': 'extend', 'selectGroups': 'extend', "filter": {"host": [name]}}
|
|
|
|
if self.use_host_interface:
|
|
|
|
api_query['selectInterfaces'] = ['useip', 'ip', 'dns']
|
|
|
|
if self.read_host_inventory:
|
|
|
|
api_query['selectInventory'] = "extend"
|
|
|
|
|
2017-02-20 19:36:19 +01:00
|
|
|
data = {'ansible_ssh_host': name}
|
2018-12-01 01:49:57 +01:00
|
|
|
if self.use_host_interface or self.read_host_inventory:
|
|
|
|
try:
|
|
|
|
hosts_data = api.host.get(api_query)[0]
|
|
|
|
if 'interfaces' in hosts_data:
|
|
|
|
# use first interface only
|
|
|
|
if hosts_data['interfaces'][0]['useip'] == 0:
|
|
|
|
data['ansible_ssh_host'] = hosts_data['interfaces'][0]['dns']
|
|
|
|
else:
|
|
|
|
data['ansible_ssh_host'] = hosts_data['interfaces'][0]['ip']
|
|
|
|
if ('inventory' in hosts_data) and (hosts_data['inventory']):
|
|
|
|
data.update(hosts_data['inventory'])
|
|
|
|
except IndexError:
|
|
|
|
# Host not found in zabbix
|
|
|
|
pass
|
2013-08-06 18:19:06 +02:00
|
|
|
return data
|
|
|
|
|
|
|
|
def get_list(self, api):
|
2018-12-01 01:49:57 +01:00
|
|
|
api_query = {'output': 'extend', 'selectGroups': 'extend'}
|
|
|
|
if self.use_host_interface:
|
|
|
|
api_query['selectInterfaces'] = ['useip', 'ip', 'dns']
|
|
|
|
if self.read_host_inventory:
|
|
|
|
api_query['selectInventory'] = "extend"
|
2013-08-06 18:19:06 +02:00
|
|
|
|
2018-12-01 01:49:57 +01:00
|
|
|
hosts_data = api.host.get(api_query)
|
|
|
|
data = {'_meta': {'hostvars': {}}}
|
2013-08-06 18:19:06 +02:00
|
|
|
|
2018-12-01 01:49:57 +01:00
|
|
|
data[self.defaultgroup] = self.hoststub()
|
|
|
|
for host in hosts_data:
|
2013-08-06 18:19:06 +02:00
|
|
|
hostname = host['name']
|
2018-12-01 01:49:57 +01:00
|
|
|
hostvars = dict()
|
2014-11-10 10:41:44 +01:00
|
|
|
data[self.defaultgroup]['hosts'].append(hostname)
|
2013-08-06 18:19:06 +02:00
|
|
|
|
|
|
|
for group in host['groups']:
|
|
|
|
groupname = group['name']
|
|
|
|
|
2017-05-09 23:38:08 +02:00
|
|
|
if groupname not in data:
|
2013-08-06 18:19:06 +02:00
|
|
|
data[groupname] = self.hoststub()
|
|
|
|
|
|
|
|
data[groupname]['hosts'].append(hostname)
|
2018-12-01 01:49:57 +01:00
|
|
|
if 'interfaces' in host:
|
|
|
|
# use first interface only
|
|
|
|
if host['interfaces'][0]['useip'] == 0:
|
|
|
|
hostvars['ansible_ssh_host'] = host['interfaces'][0]['dns']
|
|
|
|
else:
|
|
|
|
hostvars['ansible_ssh_host'] = host['interfaces'][0]['ip']
|
|
|
|
if ('inventory' in host) and (host['inventory']):
|
|
|
|
hostvars.update(host['inventory'])
|
|
|
|
data['_meta']['hostvars'][hostname] = hostvars
|
2017-06-28 17:48:27 +02:00
|
|
|
|
2013-08-06 18:19:06 +02:00
|
|
|
return data
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
2017-06-28 18:41:03 +02:00
|
|
|
self.defaultgroup = 'group_all'
|
|
|
|
self.zabbix_server = None
|
2013-08-06 18:19:06 +02:00
|
|
|
self.zabbix_username = None
|
|
|
|
self.zabbix_password = None
|
2018-07-14 15:10:16 +02:00
|
|
|
self.validate_certs = True
|
2018-12-01 01:49:57 +01:00
|
|
|
self.read_host_inventory = False
|
|
|
|
self.use_host_interface = True
|
|
|
|
|
2017-06-28 18:41:03 +02:00
|
|
|
self.meta = {}
|
2013-08-06 18:19:06 +02:00
|
|
|
|
|
|
|
self.read_settings()
|
|
|
|
self.read_cli()
|
|
|
|
|
|
|
|
if self.zabbix_server and self.zabbix_username:
|
2013-08-22 22:55:48 +02:00
|
|
|
try:
|
2018-07-14 15:10:16 +02:00
|
|
|
api = ZabbixAPI(server=self.zabbix_server, validate_certs=self.validate_certs)
|
2013-08-22 22:55:48 +02:00
|
|
|
api.login(user=self.zabbix_username, password=self.zabbix_password)
|
2018-09-10 07:16:05 +02:00
|
|
|
# zabbix_api tries to exit if it cannot parse what the zabbix server returned
|
|
|
|
# so we have to use SystemExit here
|
|
|
|
except (Exception, SystemExit) as e:
|
2015-08-28 08:18:13 +02:00
|
|
|
print("Error: Could not login to Zabbix server. Check your zabbix.ini.", file=sys.stderr)
|
2013-08-22 22:55:48 +02:00
|
|
|
sys.exit(1)
|
2013-08-06 18:19:06 +02:00
|
|
|
|
|
|
|
if self.options.host:
|
|
|
|
data = self.get_host(api, self.options.host)
|
2015-08-28 08:18:13 +02:00
|
|
|
print(json.dumps(data, indent=2))
|
2013-08-06 18:19:06 +02:00
|
|
|
|
|
|
|
elif self.options.list:
|
|
|
|
data = self.get_list(api)
|
2015-08-28 08:18:13 +02:00
|
|
|
print(json.dumps(data, indent=2))
|
2013-08-22 22:55:48 +02:00
|
|
|
|
|
|
|
else:
|
2015-08-28 08:18:13 +02:00
|
|
|
print("usage: --list ..OR.. --host <hostname>", file=sys.stderr)
|
2013-08-22 22:55:48 +02:00
|
|
|
sys.exit(1)
|
|
|
|
|
2013-08-06 18:19:06 +02:00
|
|
|
else:
|
2015-08-28 08:18:13 +02:00
|
|
|
print("Error: Configuration of server and credentials are required. See zabbix.ini.", file=sys.stderr)
|
2013-08-22 22:55:48 +02:00
|
|
|
sys.exit(1)
|
2013-08-06 18:19:06 +02:00
|
|
|
|
2018-07-29 13:46:06 +02:00
|
|
|
|
2013-08-22 22:55:48 +02:00
|
|
|
ZabbixInventory()
|