688e4918d7c63e4472d4cffa460157c882e248fe
[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.i18n").loadc("olsr")
9 local i18n = luci.i18n.translate
10
11 local page = node("admin", "status", "olsr")
12 page.target = template("status-olsr/overview")
13 page.title = "OLSR"
14 page.i18n = "olsr"
15 page.subindex = true
16
17 local page = node("admin", "status", "olsr", "neighbors")
18 page.target = call("action_neigh")
19 page.title = i18n("Neighbors")
20 page.subindex = true
21 page.order = 5
22
23 local page = node("admin", "status", "olsr", "routes")
24 page.target = call("action_routes")
25 page.title = i18n("Routen")
26 page.order = 10
27
28 local page = node("admin", "status", "olsr", "topology")
29 page.target = call("action_topology")
30 page.title = i18n("Topologie")
31 page.order = 20
32
33 local page = node("admin", "status", "olsr", "hna")
34 page.target = call("action_hna")
35 page.title = "HNA"
36 page.order = 30
37
38 local page = node("admin", "status", "olsr", "mid")
39 page.target = call("action_mid")
40 page.title = "MID"
41 page.order = 50
42
43 local page = node("admin", "status", "olsr", "smartgw")
44 page.target = call("action_smartgw")
45 page.title = "SmartGW"
46 page.order = 60
47
48 local page = node("admin", "status", "olsr", "interfaces")
49 page.target = call("action_interfaces")
50 page.title = i18n("Interfaces")
51 page.order = 70
52
53 local ol = entry(
54 {"admin", "services", "olsrd"},
55 cbi("olsr/olsrd"), "OLSR"
56 )
57 ol.i18n = "olsr"
58 ol.subindex = true
59
60 entry(
61 {"admin", "services", "olsrd", "iface"},
62 cbi("olsr/olsrdiface")
63 ).leaf = true
64
65 entry(
66 {"admin", "services", "olsrd", "hna"},
67 cbi("olsr/olsrdhna"), "HNA Announcements"
68 )
69
70 oplg = entry(
71 {"admin", "services", "olsrd", "plugins"},
72 cbi("olsr/olsrdplugins"), "Plugins"
73 )
74 oplg.i18n = "olsr"
75 oplg.leaf = true
76 oplg.subindex = true
77
78 local uci = require("luci.model.uci").cursor()
79 uci:foreach("olsrd", "LoadPlugin",
80 function (section)
81 local lib = section.library
82 entry(
83 {"admin", "services", "olsrd", "plugins", lib },
84 cbi("olsr/olsrdplugins"),
85 nil --'Plugin "%s"' % lib:gsub("^olsrd_",""):gsub("%.so.+$","")
86 )
87 end
88 )
89 end
90
91 function action_neigh()
92 local data = fetch_txtinfo("links")
93
94 if not data or not data.Links then
95 luci.template.render("status-olsr/error_olsr")
96 return nil
97 end
98
99 local function compare(a, b)
100 local c = tonumber(a.Cost)
101 local d = tonumber(b.Cost)
102
103 if not c or c == 0 then
104 return false
105 end
106
107 if not d or d == 0 then
108 return true
109 end
110
111 return c < d
112 end
113
114 table.sort(data.Links, compare)
115
116 luci.template.render("status-olsr/neighbors", {links=data.Links})
117 end
118
119 function action_routes()
120 local data = fetch_txtinfo("routes")
121
122 if not data or not data.Routes then
123 luci.template.render("status-olsr/error_olsr")
124 return nil
125 end
126
127 local function compare(a, b)
128 local c = tonumber(a.ETX)
129 local d = tonumber(b.ETX)
130
131 if not c or c == 0 then
132 return false
133 end
134
135 if not d or d == 0 then
136 return true
137 end
138
139 return c < d
140 end
141
142 table.sort(data.Routes, compare)
143
144 luci.template.render("status-olsr/routes", {routes=data.Routes})
145 end
146
147 function action_topology()
148 local data = fetch_txtinfo("topology")
149
150 if not data or not data.Topology then
151 luci.template.render("status-olsr/error_olsr")
152 return nil
153 end
154
155 local function compare(a, b)
156 return a["Dest. IP"] < b["Dest. IP"]
157 end
158
159 table.sort(data.Topology, compare)
160
161 luci.template.render("status-olsr/topology", {routes=data.Topology})
162 end
163
164 function action_hna()
165 local data = fetch_txtinfo("hna")
166
167 if not data or not data.HNA then
168 luci.template.render("status-olsr/error_olsr")
169 return nil
170 end
171
172 local function compare(a, b)
173 return a.Destination < b.Destination
174 end
175
176 table.sort(data.HNA, compare)
177
178 luci.template.render("status-olsr/hna", {routes=data.HNA})
179 end
180
181 function action_mid()
182 local data = fetch_txtinfo("mid")
183
184 if not data or not data.MID then
185 luci.template.render("status-olsr/error_olsr")
186 return nil
187 end
188
189 local function compare(a, b)
190 return a["IP address"] < b["IP address"]
191 end
192
193 table.sort(data.MID, compare)
194
195 luci.template.render("status-olsr/mid", {mids=data.MID})
196 end
197
198 function action_smartgw()
199 local data = fetch_txtinfo("gateways")
200
201 if not data or not data.Gateways then
202 luci.template.render("status-olsr/error_olsr")
203 return nil
204 end
205
206 local function compare(a, b)
207 return a["ETX"] < b["ETX"]
208 end
209
210 table.sort(data.Gateways, compare)
211
212 luci.template.render("status-olsr/smartgw", {gws=data.Gateways})
213 end
214
215 function action_interfaces()
216 local data = fetch_txtinfo("interfaces")
217
218 if not data or not data.Interfaces then
219 luci.template.render("status-olsr/error_olsr")
220 return nil
221 end
222
223 luci.template.render("status-olsr/interfaces", {iface=data.Interfaces})
224 end
225
226 -- Internal
227 function fetch_txtinfo(otable)
228 require("luci.sys")
229 local uci = require "luci.model.uci".cursor_state()
230 otable = otable or ""
231 local rawdata = luci.sys.httpget("http://127.0.0.1:2006/"..otable)
232 local rawdatav6 = luci.sys.httpget("http://[::1]:2006/"..otable)
233 local data = {}
234 local dataindex = 0
235 local name = ""
236 local defaultgw
237
238 if #rawdata ~= 0 then
239 local tables = luci.util.split(luci.util.trim(rawdata), "\r?\n\r?\n", nil, true)
240
241 if otable == "links" then
242 local route = {}
243 for i, route in ipairs(luci.sys.net.routes()) do
244 if route.dest:prefix() == 0 then
245 defaultgw = route.gateway:string()
246 end
247 end
248 end
249
250 for i, tbl in ipairs(tables) do
251 local lines = luci.util.split(tbl, "\r?\n", nil, true)
252 name = table.remove(lines, 1):sub(8)
253 local keys = luci.util.split(table.remove(lines, 1), "\t")
254 local split = #keys - 1
255 if not data[name] then
256 data[name] = {}
257 end
258
259 for j, line in ipairs(lines) do
260 dataindex = ( dataindex + 1 )
261 di = dataindex
262 local fields = luci.util.split(line, "\t", split)
263 data[name][di] = {}
264 for k, key in pairs(keys) do
265 if key == "Remote IP" or key == "Dest. IP" or key == "Gateway IP" or key == "Gateway" then
266 data[name][di][key] = fields[k]
267 hostname = nixio.getnameinfo(fields[k], "inet")
268 if hostname then
269 data[name][di]["Hostname"] = hostname
270 end
271 if key == "Remote IP" and defaultgw then
272 if defaultgw == fields[k] then
273 data[name][di]["defaultgw"] = 1
274 end
275 end
276 elseif key == "Local IP" then
277 data[name][di][key] = fields[k]
278 data[name][di]['Local Device'] = fields[k]
279 uci:foreach("network", "interface",
280 function(s)
281 localip = string.gsub(fields[k], ' ', '')
282 if s.ipaddr == localip then
283 data[name][di]['Local Device'] = s['.name'] or interface
284 end
285 end)
286 elseif key == "Interface" then
287 data[name][di][key] = fields[k]
288 uci:foreach("network", "interface",
289 function(s)
290 interface = string.gsub(fields[k], ' ', '')
291 if s.ifname == interface then
292 data[name][di][key] = s['.name'] or interface
293 end
294 end)
295 else
296 data[name][di][key] = fields[k]
297 end
298 end
299 if data[name][di].Linkcost then
300 data[name][di].LinkQuality,
301 data[name][di].NLQ,
302 data[name][di].ETX =
303 data[name][di].Linkcost:match("([%w.]+)/([%w.]+)[%s]+([%w.]+)")
304 end
305 end
306 end
307 end
308
309 if #rawdatav6 ~= 0 then
310 local tables = luci.util.split(luci.util.trim(rawdatav6), "\r?\n\r?\n", nil, true)
311 for i, tbl in ipairs(tables) do
312 local lines = luci.util.split(tbl, "\r?\n", nil, true)
313 name = table.remove(lines, 1):sub(8)
314 local keys = luci.util.split(table.remove(lines, 1), "\t")
315 local split = #keys - 1
316 if not data[name] then
317 data[name] = {}
318 end
319 for j, line in ipairs(lines) do
320 dataindex = ( dataindex + 1 )
321 di = dataindex
322 local fields = luci.util.split(line, "\t", split)
323 data[name][di] = {}
324 for k, key in pairs(keys) do
325 if key == "Remote IP" then
326 data[name][di][key] = "[" .. fields[k] .. "]"
327 hostname = nixio.getnameinfo(fields[k], "inet6")
328 if hostname then
329 data[name][di]["Hostname"] = hostname
330 end
331 elseif key == "Local IP" then
332 data[name][di][key] = fields[k]
333 data[name][di]['Local Device'] = fields[k]
334 uci:foreach("network", "interface",
335 function(s)
336 local localip = string.gsub(fields[k], ' ', '')
337 if s.ip6addr then
338 local ip6addr = string.gsub(s.ip6addr, '\/.*', '')
339 if ip6addr == localip then
340 data[name][di]['Local Device'] = s['.name'] or s.interface
341 end
342 end
343 end)
344 elseif key == "Dest. IP" then
345 data[name][di][key] = "[" .. fields[k] .. "]"
346 elseif key == "Last hop IP" then
347 data[name][di][key] = "[" .. fields[k] .. "]"
348 elseif key == "IP address" then
349 data[name][di][key] = "[" .. fields[k] .. "]"
350 elseif key == "Gateway" then
351 data[name][di][key] = "[" .. fields[k] .. "]"
352 else
353 data[name][di][key] = fields[k]
354 end
355 end
356
357 if data[name][di].Linkcost then
358 data[name][di].LinkQuality,
359 data[name][di].NLQ,
360 data[name][di].ETX =
361 data[name][di].Linkcost:match("([%w.]+)/([%w.]+)[%s]+([%w.]+)")
362 end
363 end
364 end
365 end
366
367
368 if data then
369 return data
370 end
371 end