forked from janssenlima/api-zabbix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
itservices_zabbix.py
193 lines (148 loc) · 5.39 KB
/
itservices_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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# """
# Created on Tue Aug 11 22:36:58 2015
# @author: Janssen dos reis lima
# @email : [email protected]
# @contributor: Sansao Simonton
# @telegram: @sansaoipb
# """
import sys
from zabbix_api import ZabbixAPI
SLA = "99.99"
zbx_server = "http://127.0.0.1/zabbix"
user = "Admin"
password = "zabbix"
# add the IP of your Zabbix Server
zapi = ZabbixAPI(server=zbx_server)
# add your access credentials
zapi.login(user, password)
def get_hostgroups(hostGroups='*'):
hostgroups = zapi.hostgroup.get(
{"output": "extend", "search": {"name": hostGroups}, "searchByAny": True, "searchWildcardsEnabled": True})
listaGrupos = []
for x in hostgroups:
if 'template' not in str(x['name']).lower():
listaGrupos += [x['name']]
return listaGrupos
def get_hostgroups_id(grupo):
try:
groupId = zapi.hostgroup.get({"output": "extend", "filter": {"name": grupo}})[0]['groupid']
return groupId
except:
print "Host group not found!!!"
exit(0)
def get_hosts(grupo):
hosts_grupo = zapi.host.get(
{"groupids": get_hostgroups_id(grupo), "output": ["host", "name"], "filter": {"status": 0}})
listaHosts = []
for x in hosts_grupo:
listaHosts += [x['name']]
return listaHosts
def get_hostid(host):
hostId = zapi.host.get({"output": "hostid", "filter": [{"name": host}]})[0]['hostid']
return hostId
def get_triggers_hosts(host):
triggers = zapi.trigger.get(
{"hostids": get_hostid(host), "expandDescription": "true", "expandComment": "true", "expandExpression": "true"})
for x in triggers:
print (x['description'])
def get_items_hosts(host):
items = zapi.item.get({"hostids": get_hostid(host), "with_triggers": True, "selectTriggers": "extend"})
listaItems = []
for x in items:
print (x['name'])
listaItems += [x['name']]
return listaItems
def get_item_triggerid(host, item):
triggerId = zapi.item.get(
{"output": "triggers", "hostids": get_hostid(host), "with_triggers": True,
"selectTriggers": "triggers", "filter": {"name": item}})[0]['triggers'][0]['triggerid']
return triggerId
def mk_father_itservices(grupo):
teste = get_itservices_raiz(grupo)
if teste != []:
pass
else:
if get_hosts(grupo) != []:
zapi.service.create({"name": grupo, "algorithm": "1", "showsla": "1", "goodsla": SLA, "sortorder": "1"})
def get_itservice_pid(grupo):
parentId = zapi.service.get(
{"selectParent": "extend", "selectTrigger": "extend", "expandExpression": "true",
"filter": {"name": grupo}})[0]['serviceid']
return parentId
def mk_child_itservices(host, grupo):
zapi.service.create({"name": host, "algorithm": "1", "showsla": "1", "goodsla": SLA, "sortorder": "1",
"parentid": get_itservice_pid(grupo)})
def get_itservice_pid_child(host):
parentIdChild = zapi.service.get(
{"selectParent": "extend", "selectTrigger": "extend", "expandExpression": "true",
"filter": {"name": host}})[0]['serviceid']
return parentIdChild
def mk_child_itservices_trigger(host, item):
zapi.service.create({"name": item, "algorithm": "1", "showsla": "1", "goodsla": SLA, "sortorder": "1",
"parentid": get_itservice_pid_child(host), "triggerid": get_item_triggerid(host, item)})
def get_itservices(hostGroups):
hostgroups = ["{0}".format(hostsW).rstrip().strip() for hostsW in hostGroups.split(",")]
itServices = zapi.service.get({"selectParent": ["serviceid", "name"],
"selectDependencies": ["serviceid", "servicedownid", "serviceupid", "linkid"],
"output": ["serviceid", "name"]})
listaServicos = []
hosts = []
text = ""
try:
for host in hostgroups:
if "*" != host:
hosts += [host.upper()]
for x in itServices:
if host.lower() in str(x['name']).lower():
listaServicos += [x['serviceid']]
for i in x['dependencies']:
if i['serviceupid'] in listaServicos:
listaServicos += [i['serviceid']]
else:
text += "\nTodos os grupos excluidos pelo uso do \"*\""
for x in itServices:
listaServicos += [x['serviceid']]
if [] != hosts:
if len(hosts) == 2:
text += "\nGrupos excluidos: {0}".format(' e '.join(hosts))
elif len(hosts) > 2:
ultimoNome = hosts[-1:]
todosAntes = hosts[:-1]
text += "\nGrupos excluidos: {0} e {1}".format(', '.join(todosAntes), ultimoNome[0])
else:
text += "\nGrupo excluido: {0}".format(hosts[0])
except Exception as msg:
print (msg)
return listaServicos, text
def get_itservices_raiz(hostGroups):
hostgroups = ["{0}".format(hostsW).rstrip().strip() for hostsW in hostGroups.split(",")]
itServices = zapi.service.get({"output": ["serviceid", "name"]})
listaRaiz = []
try:
for host in hostgroups:
if "*" != host:
for x in itServices:
if host in str(x['name']) and host not in listaRaiz:
listaRaiz += [host]
except Exception as msg:
print (msg)
return listaRaiz
def delete_tree_itservices(hostGroups='*'):
try:
ids, result = get_itservices(hostGroups)
print (result)
zapi.service.deletedependencies(ids)
zapi.service.delete(ids)
except Exception as msg:
print (msg)
def mk_populate(hostGroups='*'):
hostgroups = ["{0}".format(hostsW).rstrip().strip() for hostsW in hostGroups.split(",")]
for nomeGrupo in get_hostgroups(hostgroups):
mk_father_itservices(nomeGrupo)
for nomeHost in get_hosts(nomeGrupo):
print ("\n{0} ({1})\n".format(nomeGrupo, nomeHost))
mk_child_itservices(nomeHost, nomeGrupo)
for nomeItem in get_items_hosts(nomeHost):
mk_child_itservices_trigger(nomeHost, nomeItem)