forked from calmh/unifi-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unifi-save-statistics
38 lines (29 loc) · 1.44 KB
/
unifi-save-statistics
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
#!/usr/bin/env python
import argparse
import time
from unifi.controller import Controller
parser = argparse.ArgumentParser()
parser.add_argument('-c', '--controller', default='unifi', help='the controller address (default "unifi")')
parser.add_argument('-u', '--username', default='admin', help='the controller usernane (default("admin")')
parser.add_argument('-p', '--password', default='', help='the controller password')
parser.add_argument('-b', '--port', default='8443', help='the controller port (default "8443")')
parser.add_argument('-v', '--version', default='v2', help='the controller base version (default "v2")')
parser.add_argument('-s', '--siteid', default='default', help='the site ID, UniFi >=3.x only (default "default")')
parser.add_argument('-f', '--file', default='statistics-unifi.csv', help='the filename of write statistics')
args = parser.parse_args()
c = Controller(args.controller, args.username, args.password, args.port, args.version, args.siteid)
statistics = c.get_statistics_last_24h()
#open file
fo = open(args.file, "wb")
FORMAT_CSV = '%15s;%10s;%25s;%10s\n'
fo.write(FORMAT_CSV % ('Bytes', 'num_sta', 'time', 'time int'));
for stat in statistics:
bytes = stat['bytes']
num_sta = stat['num_sta']
timeint = stat['time']/1000
timestap = time.ctime(int(stat['time'])/1000)
fo.write(FORMAT_CSV % (bytes, num_sta, timestap, timeint));
# Close file
fo.close()
# Print result of file
print(open(args.file,"rb").read())