-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoll_ap.py
476 lines (434 loc) · 21.6 KB
/
poll_ap.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
import time
from datetime import datetime
import json
from helper import meraki_api
from models import db, APStatus, APClient, APBandwidth, Client, SwitchStatus, SwitchPort, AP, Switch
from dotenv import load_dotenv
import os
import smtplib, ssl
import requests
# environment variables
load_dotenv()
SMTP_SERVER = os.getenv("SMTP_SERVER")
SENDER_EMAIL = os.getenv("SENDER_EMAIL")
RECEIVER_EMAIL = os.getenv("RECEIVER_EMAIL")
EMAIL_PASSWORD = os.getenv("EMAIL_PASSWORD")
AP_STATUS_POLLING_INTERVAL = os.getenv("AP_STATUS_POLLING_INTERVAL")
AP_CLIENT_POLLING_INTERVAL = os.getenv("AP_CLIENT_POLLING_INTERVAL")
SWITCHPORT_POLLING_INTERVAL = os.getenv("SWITCHPORT_POLLING_INTERVAL")
SWITCH_STATUS_POLLING_INTERVAL = os.getenv("SWITCH_STATUS_POLLING_INTERVAL")
AP_BANDWIDTH_POLLING_INTERVAL = os.getenv("AP_BANDWIDTH_POLLING_INTERVAL")
CLIENT_PERFORMANCE_POLLING_INTERVAL = os.getenv("CLIENT_PERFORMANCE_POLLING_INTERVAL")
CLIENT_COUNT_THRESHOLD = os.getenv("CLIENT_COUNT_THRESHOLD")
BANDWIDTH_THRESHOLD = os.getenv("BANDWIDTH_THRESHOLD")
CLIENT_PERFORMANCE_THRESHOLD = os.getenv("CLIENT_PERFORMANCE_THRESHOLD")
SERVICENOW_INSTANCE = os.getenv('SERVICENOW_INSTANCE')
SERVICENOW_USERNAME = os.getenv('SERVICENOW_USERNAME')
SERVICENOW_PASSWORD = os.getenv('SERVICENOW_PASSWORD')
SERVICENOW_INCIDENT_DEFAULT_IMPACT = os.getenv('SERVICENOW_INCIDENT_DEFAULT_IMPACT')
SERVICENOW_INCIDENT_DEFAULT_URGENCY = os.getenv('SERVICENOW_INCIDENT_DEFAULT_URGENCY')
DEVICE_POLLING_INTERVAL = os.getenv('DEVICE_POLLING_INTERVAL')
# global variables
polled_APClient = False
polled_APBandwidth = False
polled_Client = False
polled_Switchport = False
def get_all_orgs():
all_orgs = meraki_api('GET', '/organizations')
orgs = json.loads(all_orgs.text)
return orgs
def get_all_aps():
orgs = get_all_orgs()
macs = []
names = []
serials = []
network_ids = []
for org in orgs:
try:
org_id = org['id']
devices = meraki_api('GET', f'/organizations/{org_id}/devices')
if devices.status_code == 200:
devices = json.loads(devices.text)
for device in devices:
if "MR" in device['model']:
macs.append(device['mac'])
names.append(device['name'])
serials.append(device['serial'])
network_ids.append(device['networkId'])
except Exception as e:
print(org)
print(f"An error has errored during getting AP from {org['name']}. Error: {e}")
return [names, macs, serials, network_ids]
def get_all_switches():
orgs = get_all_orgs()
macs = []
names = []
serials = []
network_ids = []
for org in orgs:
try:
org_id = org['id']
devices = meraki_api('GET', f'/organizations/{org_id}/devices')
if devices.status_code == 200:
devices = json.loads(devices.text)
for device in devices:
if "MS" in device['model']:
macs.append(device['mac'])
names.append(device['name'])
serials.append(device['serial'])
network_ids.append(device['networkId'])
except Exception as e:
print(f"An error has errored during getting Switch from {org['name']}. Error: {e}")
return [names, macs, serials, network_ids]
def poll_devices(network_map):
print("Start polling devices...")
while True:
aps = get_all_aps()
switches = get_all_switches()
# Example for adding APs. You'd do something similar for switches and other models.
for name, mac, serial, network_id in zip(aps[0], aps[1], aps[2], aps[3]):
existing_ap = AP.query.filter_by(mac=mac).first()
if not existing_ap:
new_ap = AP(name=name, mac=mac, network=network_map.get_map()[network_id]['name'])
db.session.add(new_ap)
for name, mac, serial, network_id in zip(switches[0], switches[1], switches[2], switches[3]):
existing_switch = Switch.query.filter_by(mac=mac).first()
if not existing_switch:
new_switch = Switch(name=name, mac=mac, network=network_map.get_map()[network_id]['name'])
db.session.add(new_switch)
db.session.commit()
# Sleep for some time before next poll
time.sleep(int(DEVICE_POLLING_INTERVAL)) # Poll every 10 minutes
def poll_switch_status():
orgs = get_all_orgs()
switches = get_all_switches()
print("Start polling Switch Status...")
down_switches = dict()
while True:
try:
for org in orgs:
org_id = org['id']
device_statuses = meraki_api('GET', f'/organizations/{org_id}/devices/statuses')
if device_statuses.status_code == 200:
device_statuses = json.loads(device_statuses.text)
for device_status in device_statuses:
if device_status['mac'] in switches[1]:
switch_name = device_status['name']
switch_status = device_status['status']
if device_status['status'] == "online":
if device_status['mac'] in down_switches:
# Switch comes back online; record this event.
new_SwitchStatus = SwitchStatus(
name=switch_name,
mac=device_status['mac'],
start_time=down_switches[device_status['mac']],
end_time=datetime.now() # Record the current time as the end_time
)
db.session.add(new_SwitchStatus)
db.session.commit()
del down_switches[device_status['mac']]
if device_status['status'] == "offline":
if device_status['mac'] not in down_switches:
down_switches[device_status['mac']] = datetime.now()
except Exception as e:
print(f"An error has errored during Switch status polling. Error: {e}")
time.sleep(int(SWITCH_STATUS_POLLING_INTERVAL))
def poll_ap_status():
orgs = get_all_orgs()
aps = get_all_aps()
print("Start polling AP Status...")
down_aps = dict()
while True:
try:
for org in orgs:
org_id = org['id']
device_statuses = meraki_api('GET', f'/organizations/{org_id}/devices/statuses')
if device_statuses.status_code == 200:
device_statuses = json.loads(device_statuses.text)
for device_status in device_statuses:
if device_status['mac'] in aps[1]:
ap_name = device_status['name']
ap_status = device_status['status']
if device_status['status'] == "online":
if device_status['mac'] in down_aps:
new_APStatus = APStatus(name=device_status['name'], mac=device_status['mac'],
start_time=down_aps[device_status['mac']],
end_time=datetime.now())
db.session.add(new_APStatus)
db.session.commit()
del down_aps[device_status['mac']]
if device_status['status'] == "offline":
if device_status['mac'] not in down_aps:
down_aps[device_status['mac']] = datetime.now()
# print(f"AP name: {ap_name}\nAP Status: {ap_status}")
# print(f"Down APs: {down_aps}")
except Exception as e:
print(f"An error has errored during AP status polling. Error: {e}")
time.sleep(int(AP_STATUS_POLLING_INTERVAL))
def poll_ap_client():
global polled_APClient
aps = get_all_aps()
names = aps[0]
macs = aps[1]
serials = aps[2]
print("Start polling AP Client...")
while True:
try:
for i in range(len(serials)):
payload = {
"timespan": int(AP_CLIENT_POLLING_INTERVAL),
"resolution": int(AP_CLIENT_POLLING_INTERVAL)
}
ap = AP.query.filter_by(mac=macs[i]).first()
get_clients = meraki_api('GET', f'/devices/{serials[i]}/clients', payload)
if get_clients.status_code == 200:
get_clients = json.loads(get_clients.text)
if len(get_clients) >= int(CLIENT_COUNT_THRESHOLD):
alert = True
else:
alert = False
# Check if AP Client already exists in database
check_APClient = APClient.query.filter_by(mac=macs[i])
if check_APClient.count() == 0:
new_APClient = APClient(name=names[i], mac=macs[i], count=len(get_clients), alert=alert,
ap_id=ap.id)
db.session.add(new_APClient)
else:
check_APClient.first().count = len(get_clients)
check_APClient.first().alert = alert
db.session.commit()
except Exception as e:
print(f"An error has errored during AP client polling. Error: {e}")
polled_APClient = True
time.sleep(int(AP_CLIENT_POLLING_INTERVAL))
def poll_switch_ports():
global polled_Switchport
switches = get_all_switches()
switch_names = switches[0]
switch_macs = switches[1]
switch_serials = switches[2]
print("Start polling Switch Ports...")
while True:
for i in range(len(switch_serials)):
try:
get_ports_response = meraki_api("GET", f'/devices/{switch_serials[i]}/switch/ports')
if get_ports_response.status_code == 200:
ports_data = get_ports_response.json()
for port_data in ports_data:
# Assuming you have a method to find the SwitchStatus by serial
switch = Switch.query.filter_by(mac=switch_macs[i]).first()
if switch:
port = SwitchPort(
switch_id=switch.id,
enabled=port_data['enabled'],
allowed_vlans=port_data['allowedVlans'],
# Add other fields as necessary
)
db.session.add(port)
db.session.commit()
except Exception as e:
print(f"An error occurred during switch port polling. Error: {e}")
polled_Switchport = True
time.sleep(int(SWITCHPORT_POLLING_INTERVAL))
def poll_ap_bandwidth():
global polled_APBandwidth
aps = get_all_aps()
names = aps[0]
macs = aps[1]
serials = aps[2]
print("Start polling AP Bandwidth...")
while True:
try:
for i in range(len(serials)):
get_device = meraki_api('GET', f'/devices/{serials[i]}')
get_device = json.loads(get_device.text)
payload = {
"timespan": int(AP_BANDWIDTH_POLLING_INTERVAL),
"resolution": int(AP_BANDWIDTH_POLLING_INTERVAL),
"deviceSerial": serials[i]
}
ap = AP.query.filter_by(mac=macs[i]).first()
net_id = get_device['networkId']
get_usage = meraki_api('GET', f'/networks/{net_id}/wireless/usageHistory', payload)
if get_usage.status_code == 200:
get_usage = json.loads(get_usage.text)
if get_usage[0]['totalKbps'] == None:
get_usage[0]['totalKbps'] = 0
if get_usage[0]['totalKbps'] >= int(BANDWIDTH_THRESHOLD):
alert = True
else:
alert = False
check_APBandwidth = APBandwidth.query.filter_by(mac=macs[i])
if check_APBandwidth.count() == 0:
new_APBandwidth = APBandwidth(name=names[i], mac=macs[i], bandwidth=get_usage[0]['totalKbps'],
alert=alert, ap_id=ap.id)
db.session.add(new_APBandwidth)
else:
check_APBandwidth.first().bandwidth = get_usage[0]['totalKbps']
check_APBandwidth.first().alert = alert
db.session.commit()
except Exception as e:
print(f"An error has errored during AP bandwidth polling. Error: {e}")
polled_APBandwidth = True
time.sleep(int(AP_BANDWIDTH_POLLING_INTERVAL))
def poll_client_performance():
global polled_Client
orgs = get_all_orgs()
print("Start polling Client performance...")
while True:
try:
ap_clients = dict()
for org in orgs:
org_id = org['id']
networks = meraki_api('GET', f'/organizations/{org_id}/networks')
if networks.status_code == 200:
networks = json.loads(networks.text)
for network in networks:
net_id = network['id']
clients = meraki_api('GET', f'/networks/{net_id}/clients')
if clients.status_code == 200:
clients = json.loads(clients.text)
for client in clients:
if not client['ssid'] == None and client['status'] == "Online":
client_id = client['id']
payload = {
"timespan": int(CLIENT_PERFORMANCE_POLLING_INTERVAL),
"resolution": int(CLIENT_PERFORMANCE_POLLING_INTERVAL),
"clientId": client_id
}
get_signal = meraki_api('GET', f'/networks/{net_id}/wireless/signalQualityHistory',
payload)
if get_signal.status_code == 200:
get_signal = json.loads(get_signal.text)
ap_clients[client['mac']] = {
"id": client['id'],
"name": client['description'],
"ip": client['ip'],
"ap": client['recentDeviceName'],
"ssid": client['ssid'],
"snr": get_signal[0]['snr'],
"rssi": get_signal[0]['rssi']
}
clients = Client.query.all()
for client in clients:
if client.mac not in ap_clients:
if client.vip == True:
client.ip = None
client.ap = None
client.ssid = None
client.snr = None
client.rssi = None
db.session.commit()
else:
db.session.delete(client)
db.session.commit()
for key in ap_clients:
if ap_clients[key]['name'] == None:
ap_clients[key]['name'] = ""
check_client = Client.query.filter_by(mac=key)
if check_client.count() == 0:
new_client = Client(
mac=key,
name=ap_clients[key]['name'],
client_id=ap_clients[key]['id'],
ip=ap_clients[key]['ip'],
ap=ap_clients[key]['ap'],
ssid=ap_clients[key]['ssid'],
snr=ap_clients[key]['snr'],
rssi=ap_clients[key]['rssi'],
vip=False
)
if ap_clients[key]['rssi'] == None:
new_client.alert = False
else:
if ap_clients[key]['rssi'] <= int(CLIENT_PERFORMANCE_THRESHOLD):
new_client.alert = True
else:
new_client.alert = False
db.session.add(new_client)
else:
check_client = check_client.first()
check_client.ip = ap_clients[key]['ip']
check_client.ap = ap_clients[key]['ap']
check_client.ssid = ap_clients[key]['ssid']
check_client.snr = ap_clients[key]['snr']
check_client.rssi = ap_clients[key]['rssi']
if ap_clients[key]['rssi'] == None:
check_client.alert = False
else:
if ap_clients[key]['rssi'] <= int(CLIENT_PERFORMANCE_THRESHOLD):
check_client.alert = True
else:
check_client.alert = False
db.session.commit()
except Exception as e:
print(f"An error has errored during Client performance polling. Error: {e}")
polled_Client = True
time.sleep(int(CLIENT_PERFORMANCE_POLLING_INTERVAL))
def alert():
global polled_APClient
global polled_APBandwidth
global polled_Client
time.sleep(120)
while True:
print("Checking for alert...")
try:
# sending alert email
port = 465 # For SSL
smtp_server = SMTP_SERVER
sender_email = SENDER_EMAIL
receiver_email = RECEIVER_EMAIL
password = EMAIL_PASSWORD
message = "Subject: Meraki Alert\n\n"
if polled_APClient == True:
alert_APClient = APClient.query.filter_by(alert=True)
if alert_APClient.count() > 0:
message += "Alerting AP Client Count:\n\n"
for ap_client in alert_APClient:
message += f"Name: {ap_client.name}\nMAC Address: {ap_client.mac}\nClient Count: {ap_client.count}\n\n"
if polled_APBandwidth == True:
alert_APBandwidth = APBandwidth.query.filter_by(alert=True)
if alert_APBandwidth.count() > 0:
message += "Alerting AP Bandwidth:\n\n"
for ap_bandwidth in alert_APBandwidth:
message += f"Name: {ap_bandwidth.name}\nMAC Address: {ap_bandwidth.mac}\nUsage: {ap_bandwidth.bandwidth}\n\n"
if polled_Client == True:
alert_Client = Client.query.filter_by(alert=True)
if alert_Client.count() > 0:
message += "Alerting Client Performance:\n\n"
for client in alert_Client:
message += f"Name: {client.name}\nMAC Address: {client.mac}\nIP Address: {client.ip}\nConnected AP: {client.ap}\nSSID: {client.ssid}\nSNR: {client.snr}\nRSSI: {client.rssi}\n\n"
if polled_APClient or polled_APBandwidth or polled_Client:
context = ssl.create_default_context()
with smtplib.SMTP_SSL(smtp_server, port, context=context) as server:
server.login(sender_email, password)
server.sendmail(sender_email, receiver_email, message)
print("Alert email sent")
except Exception as e:
print(f"An error has errored during alert checking. Error: {e}")
try:
# create ServiceNow incident for alerting VIP Client
alert_VIPClient = Client.query.filter_by(vip=True, alert=True)
headers = {
"Content-Type": "application/json",
"Accept": "application/json"
}
auth = (SERVICENOW_USERNAME, SERVICENOW_PASSWORD)
for vip_client in alert_VIPClient:
ticket = {
"impact": SERVICENOW_INCIDENT_DEFAULT_IMPACT,
"urgency": SERVICENOW_INCIDENT_DEFAULT_URGENCY,
"category": "Network",
"short_description": f"Alert for VIP Client Performance - {vip_client.name}",
"description": f"Name: {client.name}\nMAC Address: {client.mac}\nIP Address: {client.ip}\nConnected AP: {client.ap}\nSSID: {client.ssid}\nSNR: {client.snr}\nRSSI: {client.rssi}"
}
ticket_creation = requests.post(SERVICENOW_INSTANCE + "/api/now/table/incident", auth=auth,
headers=headers, json=ticket)
print(json.loads(ticket_creation.text))
print(f"ServiceNow ticket created for {vip_client.name}")
except Exception as e:
print(f"An error has errored during alert checking for VIP Clients. Error: {e}")
polled_APClient = False
polled_APBandwidth = False
polled_Client = False
time.sleep(300)