This repository has been archived by the owner on Aug 1, 2019. It is now read-only.
forked from ffnord/ffmap-backend
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathalfred.py
executable file
·87 lines (83 loc) · 2.65 KB
/
alfred.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
#!/usr/bin/env python3
import subprocess
import json
class alfred:
def __init__(self,request_data_type = 158, request_statistics_data_type = 159):
self.request_data_type = request_data_type
self.request_statistics_data_type = request_statistics_data_type
def aliases(self):
output = subprocess.check_output(["alfred-json","-r",str(self.request_data_type),"-f","json","-z"])
alfred_data = json.loads(output.decode("utf-8"))
alias = {}
for mac,node in alfred_data.items():
node_alias = {}
if 'location' in node:
try:
node_alias['gps'] = str(node['location']['latitude']) + ' ' + str(node['location']['longitude'])
except:
pass
if 'statistics' in node:
try:
node_alias['uptime'] = node['statistics']['uptime']
except:
pass
try:
node_alias['tx_bytes'] = node['statistics']['traffic']['tx']['bytes']
node_alias['rx_bytes'] = node['statistics']['traffic']['rx']['bytes']
except:
pass
try:
node_alias['loadavg'] = node['statistics']['loadavg']
except:
pass
try:
node_alias['firmware'] = node['software']['firmware']['release']
node_alias['autoupdater'] = node['software']['autoupdater']['enabled']
node_alias['branch'] = node['software']['autoupdater']['branch']
except KeyError:
pass
try:
node_alias['hardware'] = node['hardware']['model']
except KeyError:
pass
try:
node_alias['id'] = node['network']['mac']
except KeyError:
pass
if 'hostname' in node:
node_alias['name'] = node['hostname']
elif 'name' in node:
node_alias['name'] = node['name']
if len(node_alias):
alias[mac] = node_alias
return alias
def statistics(self):
output = subprocess.check_output(["alfred-json","-r",str(self.request_statistics_data_type),"-f","json","-z"])
alfred_data = json.loads(output.decode("utf-8"))
alias = {}
for mac,node in alfred_data.items():
node_alias = {}
try:
node_alias['gateway'] = node['gateway']
except:
pass
try:
node_alias['uptime'] = node['uptime']
except:
pass
try:
node_alias['tx_bytes'] = node['traffic']['tx']['bytes']
node_alias['rx_bytes'] = node['traffic']['rx']['bytes']
except:
pass
try:
node_alias['loadavg'] = node['loadavg']
except:
pass
if len(node_alias):
alias[mac] = node_alias
return alias
if __name__ == "__main__":
ad = alfred()
al = ad.aliases()
print(al)