forked from ansible-collections/community.zabbix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zabbix_inventory.py
387 lines (359 loc) · 17.8 KB
/
zabbix_inventory.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#
# Copyright: (c), Ansible Project
#
# (c) 2013, Greg Buehler
# (c) 2018, Filippo Ferrazini
# (c) 2021, Timothy Test
# Modified from ServiceNow Inventory Plugin and Zabbix inventory Script
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
from __future__ import absolute_import, division, print_function
__metaclass__ = type
DOCUMENTATION = r'''
name: zabbix_inventory
author:
- Timothy Test (@ttestscripting)
short_description: Zabbix Inventory Plugin
version_added: 1.4.0
description:
- Zabbix Inventory plugin
- All vars from zabbix are prefixed with zbx_
requirements:
- "python >= 2.6"
- "zabbix-api >= 0.5.4"
options:
server_url:
description:
- URL of Zabbix server, with protocol (http or https).
C(url) is an alias for C(server_url).
required: true
type: str
aliases: [ url ]
env:
- name: ZABBIX_SERVER
proxy:
description: Proxy server to use for reaching zabbix API
type: string
default: ''
host_zapi_query:
description:
- API query for hosts - see zabbix documentation for more details U(https://www.zabbix.com/documentation/current/manual/api/reference/host/get)
type: dict
default: {}
suboptions:
selectApplications:
type: str
description:
- query
- Return an applications property with host applications.
- To return all values specify 'extend'
- Can be limited to different fields for example setting the vaule to ['name'] will only return the name
- Additional fields can be specified by comma seperated value ['name', 'field2']
- Please see U(https://www.zabbix.com/documentation/current/manual/api/reference/application/object) for more details on field names
selectDiscoveries:
type: str
description:
- query
- Return a discoveries property with host low-level discovery rules.
- To return all values specify 'extend'
- Can be limited to different fields for example setting the vaule to ['name'] will only return the name
- Additional fields can be specified by comma seperated value ['name', 'field2']
- Please see U(https://www.zabbix.com/documentation/current/manual/api/reference/discoveryrule/object) for more details on field names
selectDiscoveryRule:
type: str
description:
- query
- Return a discoveryRule property with the low-level discovery rule that created the host (from host prototype in VMware monitoring).
- To return all values specify 'extend'
- Can be limited to different fields for example setting the vaule to ['name'] will only return the name
- Additional fields can be specified by comma seperated value ['name', 'field2']
- please see U(https://www.zabbix.com/documentation/current/manual/api/reference/discoveryrule/object) for more details on field names
selectGraphs:
type: str
description:
- query
- Return a discoveries property with host low-level discovery rules.
- To return all values specify 'extend'
- Can be limited to different fields for example setting the vaule to ['name'] will only return the name
- Additional fields can be specified by comma seperated value ['name', 'field2']
- Please see U(https://www.zabbix.com/documentation/current/manual/api/reference/graph/object) for more details on field names
selectGroups:
type: str
description:
- query
- Return a groups property with host groups data that the host belongs to.
- To return all values specify 'extend'
- Can be limited to different fields for example setting the vaule to ['name'] will only return the name
- Additional fields can be specified by comma seperated value ['name', 'field2']
- Please see U(https://www.zabbix.com/documentation/current/manual/api/reference/hostgroup/object) for more details on field names
selectHostDiscovery:
type: str
description:
- query
- Return a hostDiscovery property with host discovery object data.
- To return all values specify 'extend'
- Can be limited to different fields for example setting the vaule to ['name'] will only return the name
- Additional fields can be specified by comma seperated value ['name', 'field2']
- Please see U(https://www.zabbix.com/documentation/current/manual/api/reference/host/get) for more details on field names
selectHttpTests:
type: str
description:
- query
- Return an httpTests property with host web scenarios.
- To return all values specify 'extend'
- Can be limited to different fields for example setting the vaule to ['name'] will only return the name
- Additional fields can be specified by comma seperated value ['name', 'field2']
- Please see U(https://www.zabbix.com/documentation/current/manual/api/reference/httptest/object) for more details on field names
selectInterfaces:
type: str
description:
- query
- Return an interfaces property with host interfaces.
- To return all values specify 'extend'
- Can be limited to different fields for example setting the vaule to ['name'] will only return the name
- Additional fields can be specified by comma seperated value ['name', 'field2']
- Please see U(https://www.zabbix.com/documentation/current/manual/api/reference/hostinterface/object) for more details on field names
selectInventory:
type: str
description:
- query
- Return an inventory property with host inventory data.
- To return all values specify 'extend'
- Can be limited to different fields for example setting the vaule to ['name'] will only return the name
- Additional fields can be specified by comma seperated value ['name', 'field2']
- Please see U(https://www.zabbix.com/documentation/current/manual/api/reference/host/object#host_inventory) for more details on field names
selectItems:
type: str
description:
- query
- Return an items property with host items.
- To return all values specify 'extend'
- Can be limited to different fields for example setting the vaule to ['name'] will only return the name
- Additional fields can be specified by comma seperated value ['name', 'field2']
- Please see U(https://www.zabbix.com/documentation/current/manual/api/reference/item/object) for more details on field names
selectMacros:
type: str
description:
- query
- Return a macros property with host macros.
- To return all values specify 'extend'
- Can be limited to different fields for example setting the vaule to ['name'] will only return the name
- Additional fields can be specified by comma seperated value ['name', 'field2']
- Please see U(https://www.zabbix.com/documentation/current/manual/api/reference/usermacro/object) for more details on field names
selectParentTemplates:
type: str
description:
- query
- Return a parentTemplates property with templates that the host is linked to
- To return all values specify 'extend'
- Can be limited to different fields for example setting the vaule to ['name'] will only return the name
- Additional fields can be specified by comma seperated value ['name', 'field2']
- Please see U(https://www.zabbix.com/documentation/current/manual/api/reference/template/object) for more details on field names
selectDashboards:
type: str
description:
- query
- Return a dashboards property.
- To return all values specify 'extend'
- Can be limited to different fields for example setting the vaule to ['name'] will only return the name
- Additional fields can be specified by comma seperated value ['name', 'field2']
- Please see U(https://www.zabbix.com/documentation/current/manual/api/reference/templatedashboard/object) for more details on field names
selectTags:
type: str
description:
- query
- Return a tags property with host tags.
- To return all values specify 'extend'
- Can be limited to different fields for example setting the vaule to ['name'] will only return the name
- Additional fields can be specified by comma seperated value ['name', 'field2']
- Please see U(https://www.zabbix.com/documentation/current/manual/api/reference/host/object#host_tag) for more details on field names
selectInheritedTags:
type: str
description:
- query
- Return an inheritedTags property with tags that are on all templates which are linked to host.
- To return all values specify 'extend'
- Can be limited to different fields for example setting the vaule to ['name'] will only return the name
- Additional fields can be specified by comma seperated value ['name', 'field2']
- Please see U(https://www.zabbix.com/documentation/current/manual/api/reference/host/object#host_tag) for more details on field names
selectTriggers:
type: str
description:
- query
- Return a triggers property with host triggers.
- To return all values specify 'extend'
- Can be limited to different fields for example setting the vaule to ['name'] will only return the name
- Additional fields can be specified by comma seperated value ['name', 'field2']
- Please see U(https://www.zabbix.com/documentation/current/manual/api/reference/host/object#host_tag) for more details on field names
login_user:
description:
- Zabbix user name.
type: str
required: true
env:
- name: ZABBIX_USERNAME
login_password:
description:
- Zabbix user password.
type: str
required: true
env:
- name: ZABBIX_PASSWORD
http_login_user:
description:
- Basic Auth login
type: str
http_login_password:
description:
- Basic Auth password
type: str
timeout:
description:
- The timeout of API request (seconds).
type: int
default: 10
validate_certs:
description:
- If set to False, SSL certificates will not be validated. This should only be used on personally controlled sites using self-signed certificates.
type: bool
default: true
env:
- name: ZABBIX_VALIDATE_CERTS
add_zabbix_groups:
description:
- If set to True, hosts will be added to groups based on their zabbix groups
type: bool
default: false
extends_documentation_fragment:
- constructed
- inventory_cache
'''
EXAMPLES = r'''
# Simple Inventory Plugin example
# This will create an inventory with details from zabbix such as applications name, applicaitonids, Parent Template Name, and group membership name
#It will also create 2 ansible inventory groups for enabled and disabled hosts in zabbix based on the status field.
plugin: community.zabbix.zabbix_inventory
server_url: https://zabbix.com
login_user: Admin
login_password: password
host_zapi_query:
selectApplications: ['name', 'applicationid']
selectParentTemplates: ['name']
selectGroups: ['name']
validate_certs: false
groups:
enabled: zbx_status == "0"
disabled: zbx_status == "1"
#Using Keyed Groups
plugin: community.zabbix.zabbix_inventory
server_url: https://zabbix.com
login_user: Admin
login_password: password
validate_certs: false
keyed_groups:
- key: zbx_status | lower
prefix: 'env'
- key: zbx_description | lower
prefix: 'test'
separator: ''
#Using proxy format of proxy is 'http://<user>:<pass>@<proxy>:<port>' or 'http://<proxy>:<port>'
plugin: community.zabbix.zabbix_inventory
server_url: https://zabbix.com
proxy: http://someproxy:8080
login_user: Admin
login_password: password
validate_certs: false
#Organize inventory groups based on zabbix host groups
plugin: community.zabbix.zabbix_inventory
server_url: https://zabbix.com
add_zabbix_groups: true
login_user: Admin
login_password: password
validate_certs: false
#Using compose to modify vars
plugin: community.zabbix.zabbix_inventory
server_url: https://zabbix.com
login_user: Admin
login_password: password
validate_certs: false
compose:
zbx_testvar: zbx_status.replace("1", "Disabled")
'''
from ansible.plugins.inventory import BaseInventoryPlugin, Constructable, Cacheable, to_safe_group_name
import os
import atexit
import traceback
try:
from zabbix_api import ZabbixAPI
HAS_ZABBIX_API = True
except ImportError:
ZBX_IMP_ERR = traceback.format_exc()
HAS_ZABBIX_API = False
class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
NAME = 'community.zabbix.zabbix_inventory'
def login_zabbix(self):
# set proxy information if required
proxy = self.get_option('proxy')
os.environ['http_proxy'] = proxy
os.environ['HTTP_PROXY'] = proxy
os.environ['https_proxy'] = proxy
os.environ['HTTPS_PROXY'] = proxy
server_url = self.get_option('server_url')
http_login_user = self.get_option('login_user')
http_login_password = self.get_option('login_password')
validate_certs = self.get_option('validate_certs')
timeout = self.get_option('timeout')
self._zapi = ZabbixAPI(server_url, timeout=timeout, user=http_login_user, passwd=http_login_password, validate_certs=validate_certs)
self.login()
self._zbx_api_version = self._zapi.api_version()[:5]
def login(self):
# check if api already logged in
if not self._zapi.auth != '':
try:
login_user = self.get_option('login_user')
login_password = self.get_option('login_password')
self._zapi.login(login_user, login_password)
atexit.register(self._zapi.logout)
except Exception as e:
self.display.vvv(msg="Failed to connect to Zabbix server: %s" % e)
def verify_file(self, path):
valid = False
if super(InventoryModule, self).verify_file(path):
if path.endswith(('zabbix_inventory.yaml', 'zabbix_inventory.yml')):
valid = True
else:
self.display.vvv(
'Skipping due to inventory source not ending in "zabbix_inventory.yaml" nor "zabbix_inventory.yml"')
return valid
def parse(self, inventory, loader, path,
cache=True): # Plugin interface (2)
super(InventoryModule, self).parse(inventory, loader, path)
self._read_config_data(path)
self.cache_key = self.get_cache_key(path)
self.use_cache = self.get_option('cache') and cache
self.update_cache = self.get_option('cache') and not cache
self.login_zabbix()
zapi_query = self.get_option('host_zapi_query')
content = self._zapi.host.get(zapi_query)
strict = self.get_option('strict')
for record in content:
# add host to inventory
host_name = self.inventory.add_host(record['host'])
# set variables for host
for k in record.keys():
self.inventory.set_variable(host_name, 'zbx_%s' % k, record[k])
# added for compose vars and keyed groups
self._set_composite_vars(
self.get_option('compose'),
self.inventory.get_host(host_name).get_vars(), host_name, strict)
self._add_host_to_composed_groups(self.get_option('groups'), dict(), host_name, strict)
self._add_host_to_keyed_groups(self.get_option('keyed_groups'), dict(), host_name, strict)
# organize inventory by zabbix groups
if self.get_option('add_zabbix_groups'):
content = self._zapi.host.get({'selectGroups': ['name']})
for record in content:
host_name = record['host']
if len(record['groups']) >= 1:
for group in record['groups']:
group_name = to_safe_group_name(group['name'])
self.inventory.add_group(group_name)
self.inventory.add_child(group_name, host_name)