-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathauto-add-itservices.py
331 lines (282 loc) · 11.5 KB
/
auto-add-itservices.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# @author: Daniel Carvalho
# @email : daniel.carvalho@prodest.es.gov.br
# Update: 25/06/2019
from zabbix_api import ZabbixAPI
import os
import re
import io
import sys
import ssl
# Change to UTF8 Encoding in Python 2
if sys.version_info.major < 3:
reload(sys)
sys.setdefaultencoding('utf8')
# ##Classes## #
class ServiceGroup:
def __init__(self, name, groupId):
self.name = name
self.id = groupId
self.srvHosts = []
self.stillExist = False
class ServiceHost:
def __init__(self, name, hostsId, parentId):
self.name = name
self.id = hostsId
self.parentId = parentId
self.srvTriggers = []
self.stillExist = False
class ServiceTrigger:
def __init__(self, name, triggerId, parentId):
self.name = name
self.id = triggerId
self.parentId = parentId
self.stillExist = False
# ##Functions Utils## #
def contains_name(name, listObj):
for idx in range(len(listObj)):
if(name == listObj[idx].name):
return listObj[idx]
return None
def contains_hostID(hostID, listObj):
for idx in range(len(listObj)):
matchObj = re.match(r'.*\|HostID=([0-9]+)\|', listObj[idx].name)
if(matchObj and not matchObj.end() == -1):
if(matchObj.group(1) == hostID):
return listObj[idx]
return None
def contains_groupID(groupID, listObj):
for idx in range(len(listObj)):
matchObj = re.match(r'.*\|GroupID=([0-9]+)\|', listObj[idx].name)
if(matchObj and not matchObj.end() == -1):
if(matchObj.group(1) == groupID):
return listObj[idx]
return None
def delete_servicegroup(group, zabbixAPI):
for host in group.srvHosts:
for trigger in host.srvTriggers:
sts = zabbixAPI.service.delete([trigger.id])
sts = zapi.service.delete([host.id])
sts = zapi.service.delete([group.id])
def delete_servicehost(host, zabbixAPI):
for trigger in host.srvTriggers:
sts = zabbixAPI.service.delete([trigger.id])
sts = zapi.service.delete([host.id])
def fullmatch(regex, string, flags=0):
"""Emulate python-3.4 re.fullmatch()."""
return re.match("(?:" + regex + r")\Z", string, flags=flags)
# ##Coding###
# Parameters
SERVER = "http://127.0.0.1" # Your Zabbix IP
USERNAME = "your_username"
PASSWORD = "your_pass"
TAG_NAME = "SLA"
# Template HOST NAME
TPL_HOSTNAME = '{} |HostID={}|'
TPL_GROUPNAME = '{} |GroupID={}|'
# Open File Config
scriptPath = os.path.dirname(__file__)
configFile = open(os.path.join(scriptPath, 'config'), 'r')
config_lines = []
if(not configFile):
print("File 'config' not found")
exit(-1)
else:
for idxL, line in enumerate(configFile.readlines()):
matchObj = re.match('^(.+);([0-2])', line)
if(not matchObj):
print("Erro in 'config' line {} : {}".format(idxL, line))
exit(-1)
elif(matchObj.lastindex < 2):
print("Erro in 'config' line {} : {}".format(idxL, line))
exit(-1)
config_lines.append(line)
# Zabbix API
if sys.version_info.major < 3:
ssl._create_default_https_context = ssl._create_unverified_context
zapi = ZabbixAPI(server=SERVER, ssl_verify=False)
zapi.session.verify = False
zapi.login(USERNAME, PASSWORD)
# Service Variables
srvGroups = []
srvTriggers = []
srvHosts = []
# GET ALL Services
itServices = zapi.service.get(
{
"selectDependencies": ["serviceid"],
"selectParent": ["name"], "sortfield": ['name'],
"output": "extend"
})
for service in itServices:
# Nivel 1
if(not service['parent']):
newServiceGroup = ServiceGroup(
service['name'],
service['serviceid'])
srvGroups.append(newServiceGroup)
# Nivel 3
elif (not service['dependencies'] and service['parent']):
newServiceTrigger = ServiceTrigger(service['name'],
service['serviceid'],
service['parent']['serviceid'])
srvTriggers.append(newServiceTrigger)
# Nivel 2
else:
newServiceHost = ServiceHost(
service['name'],
service['serviceid'],
service['parent']['serviceid'])
srvHosts.append(newServiceHost)
# Filling ServiceGroup srvGroup-> srvHosts-> srvTriggers
for idxT in range(len(srvTriggers)):
for idxH in range(len(srvHosts)):
if(srvTriggers[idxT].parentId == srvHosts[idxH].id):
srvHosts[idxH].srvTriggers.append(srvTriggers[idxT])
for idxH in range(len(srvHosts)):
for idxG in range(len(srvGroups)):
if(srvHosts[idxH].parentId == srvGroups[idxG].id):
srvGroups[idxG].srvHosts.append(srvHosts[idxH])
# GET ALL GROUP
hostgroups = zapi.hostgroup.get(
{
"output": ["name", "groupid"],
"sortfield": "name"
})
# Looping in config lines
for idxL, line in enumerate(config_lines):
matchObj = re.match('^(.+);([0-2])', line)
pattern, algth = matchObj.group(1), matchObj.group(2)
# Looping in GROUP
for group in hostgroups:
# If not match pattern then continue
if(not fullmatch(pattern, (group['name']))):
continue
# Check ServiceGroup with same name
groupname = TPL_GROUPNAME.format(group['name'], group['groupid'])
srvgroup_matched = None
srvgroup_matchedName = contains_name(groupname, srvGroups)
srvgroup_matchedID = contains_groupID(group['groupid'], srvGroups)
if(not srvgroup_matchedName and not srvgroup_matchedID):
# CREATE SERVICE GROUP
print("Create Service(GROUP) : " + groupname)
status = zapi.service.create(
{
"name": groupname,
"algorithm": algth,
"showsla": "1",
"sortorder": "0",
"goodsla": "99.5"})
srvgroup_matched = ServiceGroup(groupname,
status['serviceids'][0])
srvGroups.append(srvgroup_matched)
elif(not srvgroup_matchedName and srvgroup_matchedID):
# UPDATE SERVICE GROUP
print("Update Service(GROUP) : " + groupname)
status = zapi.service.update(
{
"name": groupname,
"serviceid ": srvgroup_matchedID.id})
srvgroup_matched = srvgroup_matchedID
srvgroup_matched.name = groupname
elif(srvgroup_matchedName and srvgroup_matchedID):
srvgroup_matched = srvgroup_matchedName
srvgroup_matched.stillExist = True
# GET ALL HOST in HOSTGROUP
hosts_in_group = zapi.host.get(
{
"groupids": group['groupid'],
"output": ["name", "host", "hostid"]})
# Looping in HOST
for host in hosts_in_group:
hostname = TPL_HOSTNAME.format(host['name'], host['hostid'])
srvhost_matched = None
# Check ServiceGroup with same ID
srvhost_matchedID = contains_hostID(host['hostid'],
srvgroup_matched.srvHosts)
srvhost_matchedName = contains_name(hostname,
srvgroup_matched.srvHosts)
# GET ALL TRIGGER WITH TAG = $TAG_NAME
triggers_in_host = zapi.trigger.get(
{
"hostids": host['hostid'],
"tags": [{"tag": TAG_NAME}],
"output": "extend"})
if(not triggers_in_host):
continue
elif(not srvhost_matchedID and not srvhost_matchedName):
# CREATE SERVICE HOST
print("Create Service(HOST) : " + hostname)
status = zapi.service.create(
{
"name": hostname,
"algorithm": algth,
"parentid": srvgroup_matched.id,
"showsla": "1",
"sortorder": "0",
"goodsla": "99.5"})
srvhost_matched = ServiceHost(hostname,
status['serviceids'][0],
srvgroup_matched.id)
srvhost_matched.stillExist = True
srvgroup_matched.srvHosts.append(srvhost_matched)
elif(srvhost_matchedID and not srvhost_matchedName):
# UPDATE SERVICE HOST
print(
"Update Service(HOST) Name : {} - {} ".format(
srvhost_matchedID.name,
hostname))
status = zapi.service.update(
{
"serviceid ": srvhost_matchedID.id,
"name": hostname})
srvhost_matchedID.name = hostname
srvhost_matched = srvhost_matchedID
elif(srvhost_matchedID and srvhost_matchedName):
srvhost_matched = srvhost_matchedID
srvhost_matched.stillExist = True
# Looping in Triggers
for trigger in triggers_in_host:
srvTriggerMatch = contains_name(trigger['description'],
srvhost_matched.srvTriggers)
if(not srvTriggerMatch):
print("Create Service(TRIGGER) :" + trigger['description'])
# CREATE SERVICE TRIGGER
status = zapi.service.create(
{
"name": trigger['description'],
"algorithm": algth,
"parentid": srvhost_matched.id,
"triggerid": trigger['triggerid'],
"showsla": "1",
"sortorder": "0",
"goodsla": "99.5"})
srvTriggerMatch = ServiceTrigger(trigger['description'],
status['serviceids'][0],
srvhost_matched.id)
srvhost_matched.srvTriggers.append(srvTriggerMatch)
srvTriggerMatch.stillExist = True
# Delete service trigger that was removed tag SLA or was deleted
for trigger in srvhost_matched.srvTriggers:
if(not trigger.stillExist):
print("Delete Service(TRIGGER) :" + trigger.name)
sts = zapi.service.delete([trigger.id])
# Delete service host that was deleted
for host in srvgroup_matched.srvHosts:
if(not host.stillExist):
for trigger in host.srvTriggers:
print("Delete Service(TRIGGER):" + trigger.name)
sts = zapi.service.delete([trigger.id])
print("Delete Service(Host) :" + host.name)
sts = zapi.service.delete([host.id])
# Delete service group that was deleted
for group in srvGroups:
if(not group.stillExist):
pattern_tmpl = r".*\s\|GroupID=[0-9]+\|"
if(not re.match(pattern_tmpl, group.name)):
print(
"Service(GROUP) not deleted, not in pattern:" + group.name)
continue
print("Delete Service(GROUP) :" + group.name)
delete_servicegroup(group, zapi)