-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
executable file
·236 lines (175 loc) · 9.04 KB
/
app.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
from flask import Flask, jsonify, request,redirect
import json
import requests
import traceback
# RESOLVE IP ADDRESS
import ipaddress
import lib_zabbix as za
from credentials import *
import nb_local_lib as ntbx
# ZABBIXs
from pyzabbix import ZabbixAPI, ZabbixAPIException
app = Flask(__name__)
ZABBIX_SERVER = Zabbix_Url
zapi = ZabbixAPI(ZABBIX_SERVER)
zapi.login(api_token=Zabbix_Token)
@app.route('/create', methods=['POST'])
def zabbix_host_create():
## LEER REQUEST DE NETBOX
if(request.data):
reading_post = request.get_json()
nb_device_name = reading_post['data']['name']
nb_site_name = reading_post['data']['site']['name']
# HOSTGROUP - SITES |
try:
# HOSTGROUP - SITES | TEMPLATE - PLATFORM
nb_platform = reading_post['data']['platform']['name']
template = zapi.template.get(filter={'host': nb_platform})
print()
print(template)
find_hostgroup = zapi.hostgroup.get(filter={"name": nb_site_name},output=['groupid'])
# Validate HOSTGROPS
if(len(template) != 0):
zapi.host.create(
host=nb_device_name,
interfaces=[{
'type': 1,
'main': 1,
'useip': 0,
'ip': '',
'dns': 'netbox-zabbix-PrimaryIP',
'port': '9999'
}],
groups=[{'groupid': find_hostgroup[0]['groupid']}],
templates=[{'templateid': template[0]['templateid']}]
)
print(f"ZABBIX | SUCCESS | Created ({nb_device_name})")
else:
print(f'HostGroup {nb_site_name} not found in ZABBIX')
# HOSTGROUP - SITES
except :
print("NO PLATFORM SELECTED")
try:
find_hostgroup = zapi.hostgroup.get(filter={"name": nb_site_name}, output=['groupid'])
if(len(find_hostgroup) != 0):
zapi.host.create(
host = nb_device_name,
interfaces=[{
'type': 1,
'main': 1,
'useip': 0,
'ip': '',
'dns': 'netbox-zabbix-PrimaryIP',
'port': '9999'
}],
groups = [{"groupid": find_hostgroup[0]['groupid']}])
print(f"ZABBIX | SUCCESS | Creado ({nb_device_name})")
else:
print(f'HostGroup {nb_site_name} not found in ZABBIX')
except ZabbixAPIException as e:
print("DEVICE NOT CREATED")
return jsonify({"msg":"Correct"})
@app.route('/update', methods=['POST'])
def zabbix_host_update():
if(request.data):
# LEER NOMBRE DEL DEVICE NETBOX
reading_post = request.get_json()
#print(reading_post)
# Mandatory
nb_device_name = reading_post['data']['name']
pre_site = reading_post['snapshots']['prechange']['site']
post_site = reading_post['snapshots']['postchange']['site']
# OPTIONAL
pre_platform = reading_post['snapshots']['prechange']['platform']
post_platform = reading_post['snapshots']['postchange']['platform']
pre_primary_ipv4 = reading_post['snapshots']['prechange']['primary_ip4']
post_primary_ipv4 = reading_post['snapshots']['postchange']['primary_ip4']
# Change name
if(reading_post['snapshots']['prechange']['name'] != nb_device_name):
before_nb_device_name = reading_post['snapshots']['prechange']['name']
za.update_Hostname(before_nb_device_name, nb_device_name)
# Validade if the host needs to be created in Zabbix
if (reading_post['data']['name'] != None ):
nb_device_name_ori = reading_post['data']['name']
if (za.create_hosts_if_necessary(reading_post,nb_device_name_ori) != False ):
print(f"SUCCESS | No host found in Zabbix, creating one....")
reading_post = request.get_json()
nb_site_name = reading_post['data']['site']['name']
# HOSTGROUP - SITES |
try:
# HOSTGROUP - SITES | TEMPLATE - PLATFORM
nb_platform = reading_post['data']['platform']['name']
print(nb_platform)
template = zapi.template.get(filter={'host': nb_platform})
find_hostgroup = zapi.hostgroup.get(filter={"name": nb_site_name},output=['groupid'])
# Validate HOSTGROPS
if(len(find_hostgroup) != 0):
zapi.host.create(
host=nb_device_name,
interfaces=[{
'type': 1,
'main': 1,
'useip': 0,
'ip': '',
'dns': 'netbox-zabbix-PrimaryIP',
'port': '9999'
}],
groups=[{'groupid': find_hostgroup[0]['groupid']}],
templates=[{'templateid': template[0]['templateid']}]
)
print(f"ZABBIX | SUCCESS | Created ({nb_device_name})")
else:
print(f'HostGroup {nb_site_name} not found in ZABBIX')
# HOSTGROUP - SITES
except :
print("NO PLATFORM SELECTED")
try:
find_hostgroup = zapi.hostgroup.get(filter={"name": nb_site_name}, output=['groupid'])
if(len(find_hostgroup) != 0):
zapi.host.create(
host = nb_device_name,
interfaces=[{
'type': 1,
'main': 1,
'useip': 0,
'ip': '',
'dns': 'netbox-zabbix-PrimaryIP',
'port': '9999'
}],
groups = [{"groupid": find_hostgroup[0]['groupid']}])
print(f"ZABBIX | SUCCESS | Creado ({nb_device_name})")
else:
print(f'HostGroup {nb_site_name} not found in ZABBIX')
except ZabbixAPIException as e:
print("DEVICE NOT CREATED")
return jsonify({"msg":"Correct"})
# Update Platform
za.update_Template_Host(nb_device_name, pre_platform, post_platform)
# Update Site
za.update_HostGroup_Host(nb_device_name, pre_site, post_site)
# Manage IP Address
try:
za.update_ips_host(nb_device_name, pre_primary_ipv4, post_primary_ipv4)
except:
print("WARNING | IP ADDRESS | NO IPs")
return jsonify({"msg":"Correct"})
@app.route('/delete', methods=['DELETE'])
def zabbix_host_delete():
## LEER REQUEST DE NETBOX
if(request.data):
reading_post = request.get_json()
nb_device_name = reading_post['data']['name']
hosts_zabbix = zapi.host.get(output=["hostid", "name", "interfaces"], filter={"name": nb_device_name}, selectInterfaces=["interfaceid", "ip", "dns"], selectGroups=["groupid"])
zab_host_id = hosts_zabbix[0]['hostid'] # IDENTIFY The host ID
print("")
print(f"ZABBIX | Eliminando ... ({nb_device_name})")
try:
zapi.host.delete(hostid = zab_host_id)
print(f"ZABBIX | SUCCESS | Eliminado ... ({nb_device_name}) ")
except Exception as e:
# ESTO SE EJECUTA SI YA EXISTE EN ZABBIX
print("ZABBIX | ERROR | ", e.__class__, f"ocurrio para ({nb_device_name}).")
print(f"ZABBIX | SUCCESS | ({nb_device_name}) Device no existia en Zabbix, Pero se creo Correctamente")
return jsonify({"msg": "Correct"})
if __name__ == '__main__':
app.run(debug=True,host='0.0.0.0',port= PORT)