forked from brontide/unifiapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbw_check.py
80 lines (66 loc) · 2.08 KB
/
bw_check.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
#!/usr/bin/env python3
#
# Script to show bandwidth usage by client over a definable threshold
#
# The 5 minutes averages seem to poorly catch the peaks that you might see on the
# dashboard graph.
#
'''
Logging into controller
Fetching and processing client lists
Fetching bandwidth per user report
05-30 04:00 PM
iPad 1416 kbps
05-30 05:50 PM
iPad 1344 kbps
05-30 05:55 PM
iPad 1432 kbps
05-30 06:00 PM
iPad 1424 kbps
05-30 10:15 PM
Basselope 3344 kbps
05-30 10:20 PM
Basselope 11896 kbps
05-30 10:25 PM
Basselope 6568 kbps
05-31 12:15 AM
DESKTOP-20CSORF 3336 kbps
'''
from unifiapi import controller
import time
from datetime import datetime
tracking = 'rx_bytes' # download = tx_bytes / upload = rx_bytes
threshold = (1*1024*1024)/8 # 1.5 mbps
interval = '5minutes'
interval_sec = 5*60 # 300 seconds in 5 mintues to calculate bandwidth
print("Logging into controller")
c = controller()
s = c.sites['default']()
print("Fetching and processing client lists")
clients = s.clients()
def best_name(client):
if 'name' in client:
return "{name}".format(**client)
if 'hostname' in client:
return "{hostname}".format(**client)
return "UKN ({mac})".format(**client)
mac_to_name = dict(( (x['mac'], best_name(x)) for x in clients ))
end = time.time()*1000
start = end-(60*60*24*1000)
print("Fetching bandwidth per user report")
bandwidth_per_user = s.user_report(interval=interval, end=end, start=start)
timestamps = set((x['time'] for x in bandwidth_per_user))
users_per_time = {}
for timestamp in sorted(timestamps):
users_per_time[timestamp] = []
# Let's filter our records for ones above our threshold
for record in bandwidth_per_user:
if record[tracking] > ( threshold * interval_sec):
users_per_time[record['time']].append(record)
for timestamp in sorted(timestamps):
if users_per_time[timestamp]:
print(datetime.fromtimestamp(timestamp/1000).strftime('%m-%d %I:%M %p'))
for user in users_per_time[timestamp]:
speed = int((user[tracking]/interval_sec)/1024)*8
name = mac_to_name[user['user']]
print(name, speed, "kbps")