-
Notifications
You must be signed in to change notification settings - Fork 0
/
zbxtool_zabbix_api.py
executable file
·274 lines (253 loc) · 6.63 KB
/
zbxtool_zabbix_api.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
#!/usr/bin/python
#
# Copyright (C) 2012
#
# Marcelo Barbosa <[email protected]>
#
# This program 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, version 2 of the License.
#
# This program 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.
#
# This is script depend this other source:
# https://github.com/gescheit/scripts/tree/master/zabbix/
#
# Supported and tested to Zabbix 1.8.15
from zabbix_api import ZabbixAPI
""" Required configuration
server = YOUR URL SERVER
sender = YOUR IP to zabbix_sender
username = User with privileges API access
password = Password set to username
templaterbl = Parameter optional(use monitoring RBL)
"""
server = "http://192.168.25.12/zabbix/"
sender = "192.168.25.12"
username = "admin"
password = "zabbix"
templaterbl = "Template RBL"
""" Connection in Zabbix Server API
"""
zapi = ZabbixAPI(server = server, path = "", log_level = 0)
zapi.login(username, password)
""" Zabbix API - Functions supported this script:
hostgroup.exists
hostgroup.get
hostgroup.create
template.exists
template.get
template.create
host.exists
host.get
host.create
proxy.get
"""
def HostGroupExists(hostgroup):
""" Check this host group exist
Args:
hostgroup (str): name this host group
Returns:
True = host group exist
False = host group not exist
Example:
print HostGroupExists(hostgroup="Your Host Group")
"""
exists = zapi.hostgroup.exists({
"name": hostgroup
})
return exists
def HostGroupCreate(hostgroup):
""" Create new host group
Args:
hostgroup (str): name this new host group
Returns:
groupids = id from new host group created
False = host group exist
Example:
print HostGroupCreate(hostgroup="Your New Host Group")
"""
if HostGroupExist(hostgroup) == False:
create = zapi.hostgroup.create({
"name": hostgroup
})
result = create['groupids']
else:
result = False
return result
def TemplateExists(template):
""" Check this template exist
Args:
template (str): name this template
Returns:
True = template exist
False = template not exist
Example:
print TemplateExists(template="Your Template")
"""
exists = zapi.template.exists({
"host": template
})
return exists
def TemplateGet(template):
""" Get this template exist
Args:
template (str): name this template
Returns:
templateid = id from template
False = template not exist
Example:
print TemplateGet(template="Your Template")
"""
if TemplateExists(template) == True:
get = zapi.template.get({
"output": "extend",
"filter": {
"host": [template]
}
})
result = get[0]['templateid']
else:
result = False
return result
def TemplateCreate(template, hostgroup):
""" Create new template
Args:
template (str): name this new template
hostgroup (str): name this host group to template
Returns:
templateids = id from new template created
False = template exist
Example:
print TemplateCreate(template="Your New Template", hostgroup="Your Host Group to Template")
"""
if TemplateExists(template) == False:
create = zapi.template.create({
"host": template,
"groups": [{
"groupid": HostGroupGet(hostgroup)
}]
})
result = create[0]['templateids']
else:
result = False
return result
def HostExists(host):
""" Check this host exist
Args:
host (str): name this host
Returns:
True = host exist
False = host not exist
Example:
print HostExists(host="Your Host")
"""
exists = zapi.host.exists({
"host": host
})
return exists
def HostGet(host):
""" Get this host exist
Args:
host (str): name this host
Returns:
hostid = id from host
False = host not exist
Example:
print HostGet(host="Your Host")
"""
if HostExists(host) == True:
get = zapi.host.get({
"output": "extend",
"filter": {
"host": [host]
}
})
result = get[0]['hostid']
else:
result = False
return result
def HostCreate(host, hostgroup, useip, dns, ip, template, port):
""" Create new host
Args:
host (str): name this new host
hostgroup (str): name this host group to new host
useip (bool): 1 - use ip, 0 use dns
dns (str): name this dns to new host
ip (str): ip to new host
template (str): name this template to new host
port (str): port to new host use
Returns:
hostids = id from new host created
False Host = host exist
False Host Group = host group not exist
False Template = template not exist
Example:
print HostCreate(host="Your new host", hostgroup="Host Group to new host", useip="1 or 0", dns="Your dns name to new host", ip="ip to new host", template="Template to new host", port="por Zabbix to new host")
"""
if HostExists(host) == False:
if HostGroupExists(hostgroup) == True:
if TemplateExists(template) == True:
hostgroup = HostGroupGet(hostgroup)
template = TemplateGet(template)
create = zapi.host.create({
"host": host,
"useip": useip,
"dns": dns,
"ip": ip,
"port": port,
"groups":[{
"groupid": hostgroup
}],
"templates":[{
"templateid": template
}]
})
result = create['hostids']
else:
result = "False Template"
else:
result = "False Host Group"
else:
result = "False Host"
return result
def ProxyHA(getproxy, movetoproxy):
""" Move hosts from getproxy to movetoproxy
Args:
getproxy (str): get all hosts to proxy
movetoproxy (str): move all hosts to proxy
Returns:
hosts moved to proxy
Example:
print ProxyHA(getproxy="proxyname", movetoproxy="proxyname")
"""
get = zapi.proxy.get({
"output": "extend"
})
if getproxy != "":
for i in range(len(get)):
if get[i]['host'] == getproxy:
proxyname = get[i]['host']
proxyid = get[i]['proxyid']
otherget = zapi.proxy.get({
"select_hosts":"extend",
})
if movetoproxy != "":
for i in range(len(otherget)):
if otherget[i]['proxyid'] == proxyid:
for y in range(len(otherget[i]['hosts'])):
print otherget[i]['hosts'][y]['host']
if movetoproxy != "":
for z in range(len(get)):
if get[z]['host'] == movetoproxy:
movetoproxyid = get[z]['proxyid']
host = otherget[i]['hosts'][y]['host']
hostid = HostGet(host)
moreget = zapi.host.update({
"hostid": hostid,
"proxy_hostid": movetoproxyid
})
#END