2013-08-22 20:55:48 +00:00
|
|
|
#!/usr/bin/env python
|
2013-08-06 16:19:06 +00:00
|
|
|
|
|
|
|
# (c) 2013, Greg Buehler
|
|
|
|
#
|
|
|
|
# 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 09:41:44 +00:00
|
|
|
Zabbix Server external inventory script.
|
2013-08-22 20:55:48 +00:00
|
|
|
========================================
|
2013-08-06 16:19:06 +00:00
|
|
|
|
2013-08-22 20:55:48 +00:00
|
|
|
Returns hosts and hostgroups from Zabbix Server.
|
2013-08-06 16:19:06 +00:00
|
|
|
|
2013-08-22 20:55:48 +00:00
|
|
|
Configuration is read from `zabbix.ini`.
|
|
|
|
|
|
|
|
Tested with Zabbix Server 2.0.6.
|
2013-08-06 16:19:06 +00:00
|
|
|
"""
|
|
|
|
|
2015-08-28 06:18:13 +00:00
|
|
|
from __future__ import print_function
|
|
|
|
|
2017-01-27 21:04:59 +00:00
|
|
|
import os
|
|
|
|
import sys
|
2013-08-06 16:19:06 +00:00
|
|
|
import argparse
|
|
|
|
import ConfigParser
|
2013-08-22 20:55:48 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
from zabbix_api import ZabbixAPI
|
|
|
|
except:
|
2015-08-28 06:18:13 +00:00
|
|
|
print("Error: Zabbix API library must be installed: pip install zabbix-api.",
|
|
|
|
file=sys.stderr)
|
2013-08-22 20:55:48 +00:00
|
|
|
sys.exit(1)
|
2013-08-06 16:19:06 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
import json
|
|
|
|
except:
|
|
|
|
import simplejson as json
|
|
|
|
|
2017-05-09 21:38:08 +00:00
|
|
|
|
2013-08-06 16:19:06 +00:00
|
|
|
class ZabbixInventory(object):
|
|
|
|
|
|
|
|
def read_settings(self):
|
|
|
|
config = ConfigParser.SafeConfigParser()
|
2015-10-08 08:44:27 +00:00
|
|
|
conf_path = './zabbix.ini'
|
|
|
|
if not os.path.exists(conf_path):
|
2017-01-28 08:12:11 +00:00
|
|
|
conf_path = os.path.dirname(os.path.realpath(__file__)) + '/zabbix.ini'
|
2015-10-08 08:44:27 +00:00
|
|
|
if os.path.exists(conf_path):
|
2017-01-28 08:12:11 +00:00
|
|
|
config.read(conf_path)
|
2013-08-06 16:19:06 +00:00
|
|
|
# server
|
|
|
|
if config.has_option('zabbix', 'server'):
|
|
|
|
self.zabbix_server = config.get('zabbix', 'server')
|
|
|
|
|
2014-11-10 09:41:44 +00:00
|
|
|
# login
|
2013-08-06 16:19:06 +00: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')
|
|
|
|
|
|
|
|
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):
|
2017-02-20 18:36:19 +00:00
|
|
|
data = {'ansible_ssh_host': name}
|
2013-08-06 16:19:06 +00:00
|
|
|
return data
|
|
|
|
|
|
|
|
def get_list(self, api):
|
|
|
|
hostsData = api.host.get({'output': 'extend', 'selectGroups': 'extend'})
|
|
|
|
|
|
|
|
data = {}
|
|
|
|
data[self.defaultgroup] = self.hoststub()
|
|
|
|
|
|
|
|
for host in hostsData:
|
|
|
|
hostname = host['name']
|
2014-11-10 09:41:44 +00:00
|
|
|
data[self.defaultgroup]['hosts'].append(hostname)
|
2013-08-06 16:19:06 +00:00
|
|
|
|
|
|
|
for group in host['groups']:
|
|
|
|
groupname = group['name']
|
|
|
|
|
2017-05-09 21:38:08 +00:00
|
|
|
if groupname not in data:
|
2013-08-06 16:19:06 +00:00
|
|
|
data[groupname] = self.hoststub()
|
|
|
|
|
|
|
|
data[groupname]['hosts'].append(hostname)
|
|
|
|
|
|
|
|
return data
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
|
|
|
self.defaultgroup = 'group_all'
|
|
|
|
self.zabbix_server = None
|
|
|
|
self.zabbix_username = None
|
|
|
|
self.zabbix_password = None
|
|
|
|
|
|
|
|
self.read_settings()
|
|
|
|
self.read_cli()
|
|
|
|
|
|
|
|
if self.zabbix_server and self.zabbix_username:
|
2013-08-22 20:55:48 +00:00
|
|
|
try:
|
|
|
|
api = ZabbixAPI(server=self.zabbix_server)
|
|
|
|
api.login(user=self.zabbix_username, password=self.zabbix_password)
|
2015-08-27 18:58:51 +00:00
|
|
|
except BaseException as e:
|
2015-08-28 06:18:13 +00:00
|
|
|
print("Error: Could not login to Zabbix server. Check your zabbix.ini.", file=sys.stderr)
|
2013-08-22 20:55:48 +00:00
|
|
|
sys.exit(1)
|
2013-08-06 16:19:06 +00:00
|
|
|
|
|
|
|
if self.options.host:
|
|
|
|
data = self.get_host(api, self.options.host)
|
2015-08-28 06:18:13 +00:00
|
|
|
print(json.dumps(data, indent=2))
|
2013-08-06 16:19:06 +00:00
|
|
|
|
|
|
|
elif self.options.list:
|
|
|
|
data = self.get_list(api)
|
2015-08-28 06:18:13 +00:00
|
|
|
print(json.dumps(data, indent=2))
|
2013-08-22 20:55:48 +00:00
|
|
|
|
|
|
|
else:
|
2015-08-28 06:18:13 +00:00
|
|
|
print("usage: --list ..OR.. --host <hostname>", file=sys.stderr)
|
2013-08-22 20:55:48 +00:00
|
|
|
sys.exit(1)
|
|
|
|
|
2013-08-06 16:19:06 +00:00
|
|
|
else:
|
2015-08-28 06:18:13 +00:00
|
|
|
print("Error: Configuration of server and credentials are required. See zabbix.ini.", file=sys.stderr)
|
2013-08-22 20:55:48 +00:00
|
|
|
sys.exit(1)
|
2013-08-06 16:19:06 +00:00
|
|
|
|
2013-08-22 20:55:48 +00:00
|
|
|
ZabbixInventory()
|