-
Notifications
You must be signed in to change notification settings - Fork 285
/
zabbix.py
277 lines (234 loc) · 9.69 KB
/
zabbix.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#!/usr/bin/env python
# (c) 2013, Greg Buehler
# (c) 2018, Filippo Ferrazini
#
# 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/>.
######################################################################
"""
Zabbix Server external inventory script.
========================================
Returns hosts and hostgroups from Zabbix Server.
If you want to run with --limit against a host group with space in the
name, use asterisk. For example --limit="Linux*servers".
Configuration is read from `zabbix.ini`.
Tested with Zabbix Server 2.0.6, 3.2.3 and 3.4.
"""
from __future__ import print_function
import os
import sys
import argparse
import json
import atexit
from ansible.module_utils.six.moves import configparser
from ansible.module_utils.compat.version import LooseVersion
from ansible.module_utils.urls import Request
from ansible.module_utils.six.moves.urllib.error import URLError, HTTPError
class ZabbixInventory(object):
def read_settings(self):
config = configparser.ConfigParser()
conf_path = "./zabbix.ini"
if not os.path.exists(conf_path):
conf_path = os.path.dirname(os.path.realpath(__file__)) + "/zabbix.ini"
if os.path.exists(conf_path):
config.read(conf_path)
# server
if config.has_option("zabbix", "server"):
self.zabbix_server = config.get("zabbix", "server")
# login
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")
if config.has_option("zabbix", "auth_token"):
self.auth_token = config.get("zabbix", "auth_token")
# ssl certs
if config.has_option("zabbix", "validate_certs"):
if config.get("zabbix", "validate_certs") in ["false", "False", False]:
self.validate_certs = False
# timeout
if config.has_option("zabbix", "timeout"):
self.timeout = config.get("zabbix", "timeout")
# 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
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 api_request(self, method, params=None):
server_url = self.zabbix_server
validate_certs = self.validate_certs
timeout = self.timeout
headers = {"Content-Type": "application/json-rpc"}
payload = {
"jsonrpc": "2.0",
"method": method,
"id": "1"
}
if params is None:
payload["params"] = {}
else:
payload["params"] = params
if self.auth != "":
if (LooseVersion(self.zabbix_version) >= LooseVersion("6.4")):
headers["Authorization"] = "Bearer " + self.auth
else:
payload["auth"] = self.auth
api_url = server_url + "/api_jsonrpc.php"
req = Request(
headers=headers,
timeout=timeout,
validate_certs=validate_certs
)
try:
response = req.post(api_url, data=json.dumps(payload))
except ValueError:
print("Error: Something went wrong with JSON loading.", file=sys.stderr)
sys.exit(1)
except (URLError, HTTPError) as error:
print(error, file=sys.stderr)
return response
def get_version(self):
response = self.api_request(
"apiinfo.version"
)
res = json.load(response)
self.zabbix_version = res["result"]
def login_zabbix(self):
auth_token = self.auth_token
if auth_token:
self.auth = auth_token
return
atexit.register(self.logout_zabbix)
login_user = self.zabbix_username
login_password = self.zabbix_password
response = self.api_request(
"user.login",
{
"username": login_user,
"password": login_password
}
)
res = json.load(response)
self.auth = res["result"]
def logout_zabbix(self):
self.api_request(
"user.logout",
[]
)
def get_host(self, name):
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"
data = {"ansible_ssh_host": name}
if self.use_host_interface or self.read_host_inventory:
response = self.api_request("host.get", api_query)
response_obj = json.load(response)
if len(response_obj['result']) > 0:
host_data = response_obj['result'][0]
# check if zabbix api returned interfaces element
if "interfaces" in host_data:
# check for a interfaces list that contains at least interface
if len(host_data["interfaces"]) >= 1:
# use first interface only
if host_data["interfaces"][0]["useip"] == '0':
data["ansible_ssh_host"] = host_data["interfaces"][0]["dns"]
else:
data["ansible_ssh_host"] = host_data["interfaces"][0]["ip"]
if ("inventory" in host_data) and (host_data["inventory"]):
data.update(host_data["inventory"])
return data
def get_list(self):
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"
response = self.api_request("host.get", api_query)
hosts_data = json.load(response)["result"]
data = {"_meta": {"hostvars": {}}}
data[self.defaultgroup] = self.hoststub()
for host in hosts_data:
hostname = host["name"]
hostvars = dict()
data[self.defaultgroup]["hosts"].append(hostname)
for group in host["groups"]:
groupname = group["name"]
if groupname not in data:
data[groupname] = self.hoststub()
data[groupname]["hosts"].append(hostname)
# check if zabbix api returned a interfaces element
if "interfaces" in host:
# check for a interfaces list that contains at least interface
if len(host["interfaces"]) >= 1:
# 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
return data
def __init__(self):
self.defaultgroup = "group_all"
self.zabbix_server = None
self.zabbix_username = None
self.zabbix_password = None
self.auth_token = None
self.auth = ""
self.validate_certs = True
self.timeout = 30
self.read_host_inventory = False
self.use_host_interface = True
self.zabbix_version = ""
self.meta = {}
self.read_settings()
self.read_cli()
if self.zabbix_server and self.zabbix_username:
try:
self.get_version()
self.login_zabbix()
# 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:
print("Error: got the exception '%s'. Check your zabbix.ini." % e, file=sys.stderr)
sys.exit(1)
if self.options.host:
data = self.get_host(self.options.host)
print(json.dumps(data, indent=2))
elif self.options.list:
data = self.get_list()
print(json.dumps(data, indent=2))
else:
print("usage: --list ..OR.. --host <hostname>", file=sys.stderr)
sys.exit(1)
else:
print("Error: Configuration of server and credentials are required. See zabbix.ini.", file=sys.stderr)
sys.exit(1)
ZabbixInventory()