-
Notifications
You must be signed in to change notification settings - Fork 0
/
zabbix_template_update.py
65 lines (55 loc) · 1.78 KB
/
zabbix_template_update.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
#!/bin/python
import salt.client
import sys
if len(sys.argv) != 2:
print("Only specify hostname")
sys.exit(1)
host=sys.argv[1]
def _get_hostid(host=None):
caller = salt.client.Caller()
zab_output=caller.cmd('zabbix.host_get', host=host)
for host in zab_output:
return host['hostid']
def _get_current_templates(hostid=None):
caller = salt.client.Caller()
zab_output=caller.cmd('zabbix.host_get', hostids=hostid,
output='[{"hostid"}]',
selectParentTemplates='["templateid"]'
)
templates = []
print(zab_output)
for host in zab_output:
for template in host['parentTemplates']:
for templateid in template:
templates.append(template['templateid'])
return templates
def _get_monitoring_groups(host=None):
groups = []
caller = salt.client.Caller()
output = caller.cmd('mine.get', host, 'monitoring_groups')
groups = output[host]
return groups
def _get_zabbix_templates(host=None):
result = []
caller = salt.client.Caller()
zab_output=caller.cmd('zabbix.template_get',
host=host,
)
for parameters in zab_output:
result.append(parameters['templateid'])
return result
def _set_host_templates(hostid=None, templateids=None):
caller = salt.client.Caller()
zab_output=caller.cmd('zabbix.host_update',
hostid, templates=templateids
)
return zab_output
current_hostid=_get_hostid(host=host)
requested_template_names=_get_monitoring_groups(host=host)
requested_template_ids=_get_zabbix_templates(host=requested_template_names)
update_result=_set_host_templates(hostid=current_hostid, templateids=requested_template_ids)
if update_result is False:
print("Update Failed")
sys.exit(2)
else:
print(update_result)