luci-olsr: add interface and wifi stats to public olsr-neighb site
[project/luci.git] / applications / luci-olsr / luasrc / controller / olsr.lua
1 module("luci.controller.olsr", package.seeall)
2
3 function index()
4         if not nixio.fs.access("/etc/config/olsrd") then
5                 return
6         end
7
8         require("luci.model.uci")
9         local uci = luci.model.uci.cursor_state()
10
11         uci:foreach("olsrd", "olsrd", function(s)
12                 if s.SmartGateway and s.SmartGateway == "yes" then has_smartgw  = true end
13         end)
14
15         local page  = node("admin", "status", "olsr")
16         page.target = template("status-olsr/overview")
17         page.title  = _("OLSR")
18         page.subindex = true
19
20         local page  = node("admin", "status", "olsr", "json")
21         page.target = call("action_json")
22         page.title = nil
23         page.leaf = true
24
25         local page  = node("admin", "status", "olsr", "neighbors")
26         page.target = call("action_neigh")
27         page.title  = _("Neighbours")
28         page.subindex = true
29         page.order  = 5
30
31         local page  = node("admin", "status", "olsr", "routes")
32         page.target = call("action_routes")
33         page.title  = _("Routes")
34         page.order  = 10
35
36         local page  = node("admin", "status", "olsr", "topology")
37         page.target = call("action_topology")
38         page.title  = _("Topology")
39         page.order  = 20
40
41         local page  = node("admin", "status", "olsr", "hna")
42         page.target = call("action_hna")
43         page.title  = _("HNA")
44         page.order  = 30
45
46         local page  = node("admin", "status", "olsr", "mid")
47         page.target = call("action_mid")
48         page.title  = _("MID")
49         page.order  = 50
50
51         if has_smartgw then
52                 local page  = node("admin", "status", "olsr", "smartgw")
53                 page.target = call("action_smartgw")
54                 page.title  = _("SmartGW")
55                 page.order  = 60
56         end
57
58         local page  = node("admin", "status", "olsr", "interfaces")
59         page.target = call("action_interfaces")
60         page.title  = _("Interfaces")
61         page.order  = 70
62
63         local ol = entry(
64                 {"admin", "services", "olsrd"},
65                 cbi("olsr/olsrd"), "OLSR"
66         )
67         ol.subindex = true
68
69         entry(
70                 {"admin", "services", "olsrd", "iface"},
71                 cbi("olsr/olsrdiface")
72         ).leaf = true
73
74         entry(
75                 {"admin", "services", "olsrd", "hna"},
76                 cbi("olsr/olsrdhna"), _("HNA Announcements")
77         )
78
79         oplg = entry(
80                 {"admin", "services", "olsrd", "plugins"},
81                 cbi("olsr/olsrdplugins"), _("Plugins")
82         )
83
84         odsp = entry(
85                 {"admin", "services", "olsrd", "display"},
86                 cbi("olsr/olsrddisplay"), _("Display")
87         )
88
89         oplg.leaf = true
90         oplg.subindex = true
91
92         local uci = require("luci.model.uci").cursor()
93         uci:foreach("olsrd", "LoadPlugin",
94                 function (section)
95                         local lib = section.library
96                         entry(
97                                 {"admin", "services", "olsrd", "plugins", lib },
98                                 cbi("olsr/olsrdplugins"),
99                                 nil --'Plugin "%s"' % lib:gsub("^olsrd_",""):gsub("%.so.+$","")
100                         )
101                 end
102         )
103 end
104
105 function action_json()
106         local http = require "luci.http"
107         local utl = require "luci.util"
108
109         local jsonreq4 = utl.exec("echo /status | nc 127.0.0.1 9090")
110         local jsonreq6 = utl.exec("echo /status | nc ::1 9090")
111         http.prepare_content("application/json")
112
113         if #jsonreq4 < 1 then
114                 jsonreq4 = "{}"
115         end
116
117         if #jsonreq6 < 1 then
118                 jsonreq6 = "{}"
119         end
120
121         http.write("{v4:" .. jsonreq4 .. ", v6:" .. jsonreq6 .. "}")
122 end
123
124 function action_neigh(json)
125         local data, has_v4, has_v6, error = fetch_jsoninfo('links')
126
127         if error then
128                 return
129         end
130
131         local uci = require "luci.model.uci".cursor_state()
132         local resolve = uci:get("luci_olsr", "general", "resolve")
133         local ntm = require "luci.model.network".init()
134         local devices  = ntm:get_wifidevs()
135         local sys = require "luci.sys"
136         local assoclist = {}
137
138         luci.sys.net.routes(function(r) 
139                 if r.dest:prefix() == 0 then 
140                         defaultgw = r.gateway:string() 
141                 end
142         end)
143
144         local function compare(a,b)
145                 if a.proto == b.proto then
146                         return a.linkCost < b.linkCost
147                 else
148                         return a.proto < b.proto
149                 end
150         end
151
152         for _, dev in ipairs(devices) do
153                 for _, net in ipairs(dev:get_wifinets()) do
154                         assoclist[#assoclist+1] = {} 
155                         assoclist[#assoclist]['ifname'] = net.iwdata.ifname
156                         assoclist[#assoclist]['network'] = net.iwdata.network
157                         assoclist[#assoclist]['device'] = net.iwdata.device
158                         assoclist[#assoclist]['list'] = net.iwinfo.assoclist
159                 end
160         end
161
162         for k, v in ipairs(data) do
163                 local interface
164                 local snr = 1
165                 local signal = 1
166                 local noise = 1
167                 local arptable = sys.net.arptable()
168                 local mac
169                 local rmac
170                 local lmac
171                 local ip
172                 
173                 if resolve == "1" then
174                         hostname = nixio.getnameinfo(v.remoteIP, nil, 100)
175                         if hostname then
176                                 v.hostname = hostname
177                         end
178                 end
179                 if v.proto == '4' then
180                         uci:foreach("network", "interface",function(vif)
181                                 if vif.ipaddr and vif.ipaddr == v.localIP then
182                                         interface = vif['.name'] or vif.interface
183                                         lmac = string.lower(vif.macaddr or "")
184                                         return
185                                 end
186                         end)
187                         for _, arpt in ipairs(arptable) do
188                                 ip = arpt['IP address']
189                                 if ip == v.remoteIP then
190                                         rmac = string.lower(arpt['HW address'] or "")
191                                 end
192                         end
193                         for _, val in ipairs(assoclist) do
194                                 if val.network == interface and val.list then
195                                         for assocmac, assot in pairs(val.list) do
196                                                 assocmac = string.lower(assocmac or "")
197                                                 if rmac == assocmac then
198                                                         signal = tonumber(assot.signal)
199                                                         noise = tonumber(assot.noise)
200                                                         snr = signal/noise
201                                                 end
202                                         end
203                                 end
204                         end
205                 elseif v.proto == '6' then
206                         uci:foreach("network", "interface",function(vif)
207                                 if vif.ip6addr and string.gsub(vif.ip6addr, "/64", "") == v.localIP then
208                                         interface = vif['.name'] or vif.interface
209                                         return
210                                 end
211                         end)
212                 end
213                 if interface then
214                         v.interface = interface
215                 end
216                 v.snr = snr
217                 v.signal = signal
218                 v.noise = noise
219                 if rmac then
220                         v.remoteMAC = rmac
221                 end
222                 if lmac then
223                         v.localMAC = lmac
224                 end
225
226                 if defaultgw == v.remoteIP then
227                         v.defaultgw = 1
228                 end
229         end
230
231         table.sort(data, compare)
232         luci.template.render("status-olsr/neighbors", {links=data, has_v4=has_v4, has_v6=has_v6})
233 end
234
235 function action_routes()
236         local data, has_v4, has_v6, error = fetch_jsoninfo('routes')
237         if error then
238                 return
239         end
240
241         local uci = require "luci.model.uci".cursor_state()
242         local resolve = uci:get("luci_olsr", "general", "resolve")
243
244         for k, v in ipairs(data) do
245                 if resolve == "1" then
246                         local hostname = nixio.getnameinfo(v.gateway, nil, 100)
247                         if hostname then
248                                 v.hostname = hostname
249                         end
250                 end
251         end
252
253         local function compare(a,b)
254                 if a.proto == b.proto then
255                         return a.rtpMetricCost < b.rtpMetricCost
256                 else
257                         return a.proto < b.proto
258                 end
259         end
260
261         table.sort(data, compare)
262         luci.template.render("status-olsr/routes", {routes=data, has_v4=has_v4, has_v6=has_v6})
263 end
264
265 function action_topology()
266         local data, has_v4, has_v6, error = fetch_jsoninfo('topology')
267         if error then
268                 return
269         end
270
271         local function compare(a,b)
272                 if a.proto == b.proto then
273                         return a.tcEdgeCost < b.tcEdgeCost
274                 else
275                         return a.proto < b.proto
276                 end
277         end
278
279         table.sort(data, compare)
280         luci.template.render("status-olsr/topology", {routes=data, has_v4=has_v4, has_v6=has_v6})
281 end
282
283 function action_hna()
284         local data, has_v4, has_v6, error = fetch_jsoninfo('hna')
285         if error then
286                 return
287         end
288
289         local uci = require "luci.model.uci".cursor_state()
290         local resolve = uci:get("luci_olsr", "general", "resolve")
291
292         local function compare(a,b)
293                 if a.proto == b.proto then
294                         return a.genmask < b.genmask
295                 else
296                         return a.proto < b.proto
297                 end
298         end
299
300         for k, v in ipairs(data) do
301                 if resolve == "1" then
302                         hostname = nixio.getnameinfo(v.gateway, nil, 100)
303                         if hostname then
304                                 v.hostname = hostname
305                         end
306                 end
307                 if v.validityTime then
308                         v.validityTime = tonumber(string.format("%.0f", v.validityTime / 1000))
309                 end
310         end
311
312         table.sort(data, compare)
313         luci.template.render("status-olsr/hna", {hna=data, has_v4=has_v4, has_v6=has_v6})
314 end
315
316 function action_mid()
317         local data, has_v4, has_v6, error = fetch_jsoninfo('mid')
318         if error then
319                 return
320         end
321
322         local function compare(a,b)
323                 if a.proto == b.proto then
324                         return a.ipAddress < b.ipAddress
325                 else
326                         return a.proto < b.proto
327                 end
328         end
329
330         table.sort(data, compare)
331         luci.template.render("status-olsr/mid", {mids=data, has_v4=has_v4, has_v6=has_v6})
332 end
333
334 function action_smartgw()
335         local data, has_v4, has_v6, error = fetch_jsoninfo('gateways')
336         if error then
337                 return
338         end
339
340         local function compare(a,b)
341                 if a.proto == b.proto then
342                         return a.tcPathCost < b.tcPathCost
343                 else
344                         return a.proto < b.proto
345                 end
346         end
347
348         table.sort(data, compare)
349         luci.template.render("status-olsr/smartgw", {gws=data, has_v4=has_v4, has_v6=has_v6})
350 end
351
352 function action_interfaces()
353         local data, has_v4, has_v6, error = fetch_jsoninfo('interfaces')
354         if error then
355                 return
356         end
357
358         local function compare(a,b)
359                 return a.proto < b.proto
360         end
361
362         table.sort(data, compare)
363         luci.template.render("status-olsr/interfaces", {iface=data, has_v4=has_v4, has_v6=has_v6})
364 end
365
366 -- Internal
367 function fetch_jsoninfo(otable)
368         local uci = require "luci.model.uci".cursor_state()
369         local utl = require "luci.util"
370         local json = require "luci.json"
371         local jsonreq4 = utl.exec("echo /" .. otable .. " | nc 127.0.0.1 9090")
372         local jsondata4 = {}
373         local jsonreq6 = utl.exec("echo /" .. otable .. " | nc ::1 9090")
374         local jsondata6 = {}
375         local data4 = {}
376         local data6 = {}
377         local has_v4 = False
378         local has_v6 = False
379
380         if jsonreq4 == '' and jsonreq6 == '' then
381                 luci.template.render("status-olsr/error_olsr")
382                 return nil, 0, 0, true
383         end
384
385         if #jsonreq4 ~= 0 then
386                 has_v4 = 1
387                 jsondata4 = json.decode(jsonreq4)
388                 if otable == 'status' then
389                         data4 = jsondata4 or {}
390                 else
391                         data4 = jsondata4[otable] or {}
392                 end
393
394                 for k, v in ipairs(data4) do
395                         data4[k]['proto'] = '4'
396                 end
397
398         end
399         if #jsonreq6 ~= 0 then
400                 has_v6 = 1
401                 jsondata6 = json.decode(jsonreq6)
402                 if otable == 'status' then
403                         data6 = jsondata6 or {}
404                 else
405                         data6 = jsondata6[otable] or {}
406                 end
407                 for k, v in ipairs(data6) do
408                         data6[k]['proto'] = '6'
409                 end
410         end
411
412         for k, v in ipairs(data6) do
413                 table.insert(data4, v)
414         end
415
416         return data4, has_v4, has_v6, false
417 end
418