forked from Freifunk-Potsdam/ffp-monitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapdb.py
230 lines (208 loc) · 7.93 KB
/
apdb.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
#!/usr/bin/env python3
from base import *
import shlex
import ips
import random
DEFQUERYFIELDS = ["hostname","sysinfo.location","sysinfo.mail","ips","routes","senderip"]
QQUERIES = {
"***": {"state":{"$in":["dead"]}},
"**" : {"state":{"$in":["dead","offline"]}},
"*" : {"state":{"$in":["dead","offline","late"]}},
}
DEFLON = 13.054457
DEFLAT = 52.389375
class ApDb:
@cherrypy.expose
def apdb(self):
return self.serve_site("apdb", cssprefix = "apdb", jsonurl = "apdb.json" )
@cherrypy.expose
def apmap(self):
return self.serve_site("apmap", gjsurl = "apmap.geojson" )
@cherrypy.expose
def apinfo(self,name,**kwargs):
for ap in self.get_aps({"hostname": { "$regex": "^%s" % name }}):
return self.serve_site("apinfomap", ap = ap, DEFLAT = DEFLAT, DEFLON = DEFLON,
gjsurl = "../aproutes.geojson?node=%s" % ap["hostname"], **kwargs )
raise HTTPError(404)
@cherrypy.expose
def apdb_json(self,q=""):
aps = self.get_aps( buildquery(q) )
cols = [
["state","style",{"role":"style"}],
["hostname","Hostname"],
["ips","IP(s)"],
["routes","Gateways"],
["location","Standort"],
["uptime","Uptime",{"type":"duration"}],
["device","Gerät"],
["firmware","Firmware"],
["scriptver","Script"],
["contact","Kontakt"],
["delay","Letzte Daten",{"type":"duration"}],
["links","Links"],
]
cherrypy.response.headers['Content-Type'] = 'application/json'
return self.gen_apdb_json(aps,cols)
@cherrypy.expose
def apmap_geojson(self,maxetx=None,etxbase=None):
maxetx = floatdef(maxetx,10)
etxbase = etxbase if etxbase in ["etx3","etx24"] else "etx24"
gjs = { "type": "FeatureCollection","features": [] }
for d in self.get_links( { etxbase: { "$lte": maxetx } }, sort = [(etxbase,-1)] ):
gjs["features"].append( self.link2gjs(d,etxbase) )
docs = self.get_aps( {"state":{"$ne":"renamed"}}, sort = [("last_ts",-1)] )
for d in docs:
gjs["features"].append( self.ap2gjs(d) )
cherrypy.response.headers['Content-Type'] = 'application/json'
return bytes(json.dumps(gjs),"utf-8")
@cherrypy.expose
def aproutes_geojson(self,node):
gjs = { "type": "FeatureCollection","features": [] }
reachable = [node]
docs = list(self._mdb["routes"].find({"start":node},sort=[("etx",-1)]))
if len(docs) > 0:
maxetx = docs[0]["etx"]
for d in docs:
reachable.append(d["target"])
gjs["features"].append( self.route2gjs(d,maxetx) )
docs = self.get_aps( {"state":{"$ne":"renamed"}}, sort = [("last_ts",-1)] )
for d in docs:
if d["hostname"] not in reachable:
d["state"] = "unreachable"
elif d["hostname"] == node:
d["state"] = "selected"
gjs["features"].append( self.ap2gjs(d) )
cherrypy.response.headers['Content-Type'] = 'application/json'
return bytes(json.dumps(gjs),"utf-8")
def get_aps(self, q = {}, sort = None):
now = time.time()
for ap in self._mdb["nodes"].find( q, sort = sort ):
ap["delay"] = now - ap["last_ts"] if "last_ts" in ap else None
yield ap
def get_links(self, q = {}, sort = None):
return self._mdb["linkperf"].find( q, sort = sort )
def get_routes(self, start):
return self._mdb["routes"].find({"start":start},sort=[("etx",-1)])
def gen_apdb_json(self,aps,cols):
now = time.time()
data = []
for ap in aps:
adata = []
for c in cols:
tpl = self.get_tpl( "apdb/%s.html" % c[0] )
if tpl is None:
v = ap.get( c[0], None )
t = c[2]["type"] if len(c) > 2 and "type" in c[2] else "string"
if t == "string":
if c[0] == "hostname":
xv = str(v).split("-")
for i in range(min(len(xv),2)):
xv[i] = xv[i].zfill(3)
adata.append({ "v": "-".join(xv), "f": str(v) })
else:
adata.append( str( v ) )
elif t == "number":
adata.append({ "v": floatdef(v), "f": str(v) })
elif t == "duration":
adata.append({ "v": floatdef(v), "f": formatDuration( intdef(v) ) })
else:
adata.append( v )
else:
adata.append( tpl.render( ap = ap ) )
data.append(adata)
res = { "data": data, "cols": []}
for c in cols:
col = { "label": c[1], "type": "string", "class":c[0] }
if len(c) > 2:
for k,v in c[2].items():
col[k] = v
if col["type"] in ["duration"]:
col["type"] = "number"
res["cols"].append( col )
return bytes(json.dumps( res ),"utf-8")
def ap2gjs(self,ap):
random.seed(ap["hostname"])
f = {
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
floatdef(getelem(ap,"sysinfo.longitude"),DEFLON - 0.001 + random.random() * 0.002),
floatdef(getelem(ap,"sysinfo.latitude" ),DEFLAT - 0.001 + random.random() * 0.002),
]
},
"properties": {
"hostname": ap["hostname"],
"popup": self.get_tpl( "apmap/popup.html" ).render( ap = ap ),
"info": self.get_tpl( "apmap/info.html" ).render( ap = ap ),
"state": ap["state"],
"uplink": ips.anyextgateway( *ap.get("routes",[]) ),
}
}
return f
def link2gjs(self,l,etxbase="etx24"):
l["length"] = 1000 * haversine(l["c"][0][0],l["c"][0][1],l["c"][1][0],l["c"][1][1])
f = {
"type": "Feature",
"geometry": {
"type": "LineString",
"coordinates": l["c"],
},
"properties": {
"info": self.get_tpl( "apmap/linkinfo.html" ).render(
link = l,
etxbase = etxbase,
lqbase = {"etx3": "lq3","etx24": "lq24"}[etxbase],
nlqbase = {"etx3":"nlq3","etx24":"nlq24"}[etxbase]
),
"etx": l[etxbase],
}
}
return f
def route2gjs(self,l,maxetx):
l["length"] = 0
for i in range(1,len(l["c"])):
l["length"] += 1000 * haversine(l["c"][i-1][0],l["c"][i-1][1],l["c"][i][0],l["c"][i][1])
f = {
"type": "Feature",
"geometry": {
"type": "LineString",
"coordinates": l["c"]
},
"properties": {
"info": self.get_tpl( "apmap/routeinfo.html" ).render( route = l ),
"etx": l["etx"],
"maxetx":maxetx,
}
}
return f
def buildquery(q,over={}):
if q in QQUERIES:
return QQUERIES[q]
try:
qs = shlex.split(q)
except ValueError as ex:
print(ex, q)
qs = q.split()
query = {}
i = 0
while i < len(qs):
if "=" in qs[i]:
k,_,v = qs[i].partition("=")
query[k] = v
del qs[i]
else:
i += 1
fields = DEFQUERYFIELDS
ored = []
for f in fields:
anded = []
for qa in qs:
qa = {f:{"$regex":qa, "$options":"i"}}
anded.append(qa)
if len(anded) > 0:
ored.append({"$and":anded})
if len(ored)>0:
query["$or"] = ored
query.update(over)
return query