forked from stjohn96/unifipy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch_usage.py
72 lines (50 loc) · 2 KB
/
fetch_usage.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
from unifi import UnifiPy
from datetime import datetime, timedelta
# https://domain:8443
CONTROLLER = ""
# Controller Username, we created a seperate user for this but you dont need to.
USERNAME = ""
PASSWORD = ""
start = datetime.now() - timedelta(days=7)
end = datetime.now()
epoch = datetime.utcfromtimestamp(0)
# Datetime to epoch milliseconds
def unix_time_millis(dt):
return int((dt - epoch).total_seconds() * 1000.0)
def main():
unifipy = UnifiPy(
controller=CONTROLLER,
username=USERNAME,
password=PASSWORD
)
# Fetch all APs from the Unifi Controller, Site name is optional. If not provided, devices from all sites will be returned.
devices = unifipy.devices.get(site_name="aoewakfu")
devices_by_mac = {device.mac: device for device in devices}
daily_usage = unifipy.usage.get_daily_usage(
site="aoewakfu",
start=unix_time_millis(start),
end=unix_time_millis(end),
devices=list(devices_by_mac.keys()) # Usage is fetched from the Unifi Controller using mac addresses.
)
for x in daily_usage:
utc_time = datetime(1970, 1, 1) + timedelta(milliseconds=x["time"])
download = x.get("tx_bytes")/1000000/1000
upload = x.get("rx_bytes")/1000000/1000
ap = x.get("ap")
device = devices_by_mac.get(ap)
print(utc_time, ap, device.name, download, upload)
hourly = unifipy.usage.get_hourly_usage(
site="aoewakfu",
start=unix_time_millis(start),
end=unix_time_millis(end),
devices=list(devices_by_mac.keys()) # Usage is fetched from the Unifi Controller using mac addresses.
)
for x in hourly:
utc_time = datetime(1970, 1, 1) + timedelta(milliseconds=x["time"])
download = x.get("tx_bytes")/1000000/1000
upload = x.get("rx_bytes")/1000000/1000
ap = x.get("ap")
device = devices_by_mac.get(ap)
print(utc_time, ap, device.name, download, upload)
if __name__ == "__main__":
main()