1 module("luci.controller.olsr", package.seeall)
4 if not nixio.fs.access("/etc/config/olsrd") then
8 require("luci.i18n").loadc("olsr")
9 local i18n = luci.i18n.translate
11 local page = node("admin", "status", "olsr")
12 page.target = template("status-olsr/overview")
17 local page = node("admin", "status", "olsr", "neighbors")
18 page.target = call("action_neigh")
19 page.title = i18n("Neighbors")
23 local page = node("admin", "status", "olsr", "routes")
24 page.target = call("action_routes")
25 page.title = i18n("Routen")
28 local page = node("admin", "status", "olsr", "topology")
29 page.target = call("action_topology")
30 page.title = i18n("Topologie")
33 local page = node("admin", "status", "olsr", "hna")
34 page.target = call("action_hna")
38 local page = node("admin", "status", "olsr", "mid")
39 page.target = call("action_mid")
43 local page = node("admin", "status", "olsr", "smartgw")
44 page.target = call("action_smartgw")
45 page.title = "SmartGW"
48 local page = node("admin", "status", "olsr", "interfaces")
49 page.target = call("action_interfaces")
50 page.title = i18n("Interfaces")
54 {"admin", "services", "olsrd"},
55 cbi("olsr/olsrd"), "OLSR"
61 {"admin", "services", "olsrd", "iface"},
62 cbi("olsr/olsrdiface")
66 {"admin", "services", "olsrd", "hna"},
67 cbi("olsr/olsrdhna"), "HNA Announcements"
71 {"admin", "services", "olsrd", "plugins"},
72 cbi("olsr/olsrdplugins"), "Plugins"
78 local uci = require("luci.model.uci").cursor()
79 uci:foreach("olsrd", "LoadPlugin",
81 local lib = section.library
83 {"admin", "services", "olsrd", "plugins", lib },
84 cbi("olsr/olsrdplugins"),
85 nil --'Plugin "%s"' % lib:gsub("^olsrd_",""):gsub("%.so.+$","")
91 function action_neigh()
92 local data = fetch_txtinfo("links")
94 if not data or not data.Links then
95 luci.template.render("status-olsr/error_olsr")
99 local function compare(a, b)
100 local c = tonumber(a.Cost)
101 local d = tonumber(b.Cost)
103 if not c or c == 0 then
107 if not d or d == 0 then
114 table.sort(data.Links, compare)
116 luci.template.render("status-olsr/neighbors", {links=data.Links})
119 function action_routes()
120 local data = fetch_txtinfo("routes")
122 if not data or not data.Routes then
123 luci.template.render("status-olsr/error_olsr")
127 local function compare(a, b)
128 local c = tonumber(a.ETX)
129 local d = tonumber(b.ETX)
131 if not c or c == 0 then
135 if not d or d == 0 then
142 table.sort(data.Routes, compare)
144 luci.template.render("status-olsr/routes", {routes=data.Routes})
147 function action_topology()
148 local data = fetch_txtinfo("topology")
150 if not data or not data.Topology then
151 luci.template.render("status-olsr/error_olsr")
155 local function compare(a, b)
156 return a["Dest. IP"] < b["Dest. IP"]
159 table.sort(data.Topology, compare)
161 luci.template.render("status-olsr/topology", {routes=data.Topology})
164 function action_hna()
165 local data = fetch_txtinfo("hna")
167 if not data or not data.HNA then
168 luci.template.render("status-olsr/error_olsr")
172 local function compare(a, b)
173 return a.Destination < b.Destination
176 table.sort(data.HNA, compare)
178 luci.template.render("status-olsr/hna", {routes=data.HNA})
181 function action_mid()
182 local data = fetch_txtinfo("mid")
184 if not data or not data.MID then
185 luci.template.render("status-olsr/error_olsr")
189 local function compare(a, b)
190 return a["IP address"] < b["IP address"]
193 table.sort(data.MID, compare)
195 luci.template.render("status-olsr/mid", {mids=data.MID})
198 function action_smartgw()
199 local data = fetch_txtinfo("gateways")
201 if not data or not data.Gateways then
202 luci.template.render("status-olsr/error_olsr")
206 local function compare(a, b)
207 return a["ETX"] < b["ETX"]
210 table.sort(data.Gateways, compare)
212 luci.template.render("status-olsr/smartgw", {gws=data.Gateways})
215 function action_interfaces()
216 local data = fetch_txtinfo("interfaces")
218 if not data or not data.Interfaces then
219 luci.template.render("status-olsr/error_olsr")
223 luci.template.render("status-olsr/interfaces", {iface=data.Interfaces})
227 function fetch_txtinfo(otable)
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)
238 if #rawdata ~= 0 then
239 local tables = luci.util.split(luci.util.trim(rawdata), "\r?\n\r?\n", nil, true)
241 if otable == "links" then
243 luci.sys.net.routes(function(r) if r.dest:prefix() == 0 then defaultgw = r.gateway:string() end end)
246 for i, tbl in ipairs(tables) do
247 local lines = luci.util.split(tbl, "\r?\n", nil, true)
248 name = table.remove(lines, 1):sub(8)
249 local keys = luci.util.split(table.remove(lines, 1), "\t")
250 local split = #keys - 1
251 if not data[name] then
255 for j, line in ipairs(lines) do
256 dataindex = ( dataindex + 1 )
258 local fields = luci.util.split(line, "\t", split)
260 for k, key in pairs(keys) do
261 if key == "Remote IP" or key == "Dest. IP" or key == "Gateway IP" or key == "Gateway" then
262 data[name][di][key] = fields[k]
263 hostname = nixio.getnameinfo(fields[k], "inet")
265 data[name][di]["Hostname"] = hostname
267 if key == "Remote IP" and defaultgw then
268 if defaultgw == fields[k] then
269 data[name][di]["defaultgw"] = 1
272 elseif key == "Local IP" then
273 data[name][di][key] = fields[k]
274 data[name][di]['Local Device'] = fields[k]
275 uci:foreach("network", "interface",
277 localip = string.gsub(fields[k], ' ', '')
278 if s.ipaddr == localip then
279 data[name][di]['Local Device'] = s['.name'] or interface
282 elseif key == "Interface" then
283 data[name][di][key] = fields[k]
284 uci:foreach("network", "interface",
286 interface = string.gsub(fields[k], ' ', '')
287 if s.ifname == interface then
288 data[name][di][key] = s['.name'] or interface
292 data[name][di][key] = fields[k]
295 if data[name][di].Linkcost then
296 data[name][di].LinkQuality,
299 data[name][di].Linkcost:match("([%w.]+)/([%w.]+)[%s]+([%w.]+)")
305 if #rawdatav6 ~= 0 then
306 local tables = luci.util.split(luci.util.trim(rawdatav6), "\r?\n\r?\n", nil, true)
307 for i, tbl in ipairs(tables) do
308 local lines = luci.util.split(tbl, "\r?\n", nil, true)
309 name = table.remove(lines, 1):sub(8)
310 local keys = luci.util.split(table.remove(lines, 1), "\t")
311 local split = #keys - 1
312 if not data[name] then
315 for j, line in ipairs(lines) do
316 dataindex = ( dataindex + 1 )
318 local fields = luci.util.split(line, "\t", split)
320 for k, key in pairs(keys) do
321 if key == "Remote IP" then
322 data[name][di][key] = "[" .. fields[k] .. "]"
323 hostname = nixio.getnameinfo(fields[k], "inet6")
325 data[name][di]["Hostname"] = hostname
327 elseif key == "Local IP" then
328 data[name][di][key] = fields[k]
329 data[name][di]['Local Device'] = fields[k]
330 uci:foreach("network", "interface",
332 local localip = string.gsub(fields[k], ' ', '')
334 local ip6addr = string.gsub(s.ip6addr, '\/.*', '')
335 if ip6addr == localip then
336 data[name][di]['Local Device'] = s['.name'] or s.interface
340 elseif key == "Dest. IP" then
341 data[name][di][key] = "[" .. fields[k] .. "]"
342 elseif key == "Last hop IP" then
343 data[name][di][key] = "[" .. fields[k] .. "]"
344 elseif key == "IP address" then
345 data[name][di][key] = "[" .. fields[k] .. "]"
346 elseif key == "Gateway" then
347 data[name][di][key] = "[" .. fields[k] .. "]"
349 data[name][di][key] = fields[k]
353 if data[name][di].Linkcost then
354 data[name][di].LinkQuality,
357 data[name][di].Linkcost:match("([%w.]+)/([%w.]+)[%s]+([%w.]+)")