luci/applications/luci-olsr/luasrc/controller/olsr.lua

231 lines
4.7 KiB
Lua
Raw Normal View History

module("luci.controller.olsr", package.seeall)
2008-05-09 00:14:49 +08:00
2008-05-22 22:04:03 +08:00
function index()
2009-07-19 08:24:58 +08:00
if not nixio.fs.access("/etc/config/olsrd") then
return
end
2008-06-09 02:02:47 +08:00
require("luci.i18n").loadc("olsr")
local i18n = luci.i18n.translate
local page = node("admin", "status", "olsr")
2008-05-28 04:39:48 +08:00
page.target = call("action_index")
2008-05-22 22:04:03 +08:00
page.title = "OLSR"
2008-06-09 02:02:47 +08:00
page.i18n = "olsr"
2008-10-10 04:28:07 +08:00
page.subindex = true
local page = node("admin", "status", "olsr", "routes")
2008-05-28 04:39:48 +08:00
page.target = call("action_routes")
page.title = i18n("Routen")
2008-05-22 22:04:03 +08:00
page.order = 10
local page = node("admin", "status", "olsr", "topology")
2008-05-28 04:39:48 +08:00
page.target = call("action_topology")
page.title = i18n("Topologie")
2008-05-22 22:04:03 +08:00
page.order = 20
local page = node("admin", "status", "olsr", "hna")
2008-05-28 04:39:48 +08:00
page.target = call("action_hna")
2008-05-22 22:04:03 +08:00
page.title = "HNA"
page.order = 30
local page = node("admin", "status", "olsr", "mid")
2008-05-28 04:39:48 +08:00
page.target = call("action_mid")
2008-05-22 22:04:03 +08:00
page.title = "MID"
page.order = 50
2008-10-10 04:28:07 +08:00
local ol = entry(
{"admin", "services", "olsrd"},
cbi("olsr/olsrd"), "OLSR"
2008-10-10 04:28:07 +08:00
)
ol.i18n = "olsr"
ol.subindex = true
2010-12-05 09:55:02 +08:00
entry(
{"admin", "services", "olsrd", "iface"},
cbi("olsr/olsrdiface")
).leaf = true
entry(
{"admin", "services", "olsrd", "hna"},
cbi("olsr/olsrdhna"), "HNA Announcements"
2008-10-10 04:28:07 +08:00
)
oplg = entry(
{"admin", "services", "olsrd", "plugins"},
cbi("olsr/olsrdplugins"), "Plugins"
)
oplg.i18n = "olsr"
oplg.leaf = true
2008-10-10 04:28:07 +08:00
oplg.subindex = true
local uci = require("luci.model.uci").cursor()
uci:foreach("olsrd", "LoadPlugin",
function (section)
local lib = section.library
entry(
{"admin", "services", "olsrd", "plugins", lib },
cbi("olsr/olsrdplugins"),
nil --'Plugin "%s"' % lib:gsub("^olsrd_",""):gsub("%.so.+$","")
)
end
)
2008-05-22 22:04:03 +08:00
end
2008-05-09 00:14:49 +08:00
function action_index()
local data = fetch_txtinfo("links")
2008-05-09 00:14:49 +08:00
if not data or not data.Links then
luci.template.render("status-olsr/error_olsr")
2008-05-09 00:14:49 +08:00
return nil
end
2008-05-09 00:14:49 +08:00
local function compare(a, b)
2008-09-14 01:51:03 +08:00
local c = tonumber(a.Cost)
local d = tonumber(b.Cost)
2008-08-18 04:29:00 +08:00
if not c or c == 0 then
2008-05-09 00:14:49 +08:00
return false
end
2008-08-18 04:29:00 +08:00
if not d or d == 0 then
2008-05-09 00:14:49 +08:00
return true
end
2008-08-18 04:29:00 +08:00
return c < d
2008-05-09 00:14:49 +08:00
end
2008-05-09 00:14:49 +08:00
table.sort(data.Links, compare)
luci.template.render("status-olsr/index", {links=data.Links})
2008-05-09 00:14:49 +08:00
end
function action_routes()
local data = fetch_txtinfo("routes")
2008-05-09 00:14:49 +08:00
if not data or not data.Routes then
luci.template.render("status-olsr/error_olsr")
2008-05-09 00:14:49 +08:00
return nil
end
2008-05-09 00:14:49 +08:00
local function compare(a, b)
2008-08-18 04:29:00 +08:00
local c = tonumber(a.ETX)
local d = tonumber(b.ETX)
2008-08-18 04:29:00 +08:00
if not c or c == 0 then
2008-05-09 00:14:49 +08:00
return false
end
2008-08-18 04:29:00 +08:00
if not d or d == 0 then
2008-05-09 00:14:49 +08:00
return true
end
2008-08-18 04:29:00 +08:00
return c < d
2008-05-09 00:14:49 +08:00
end
2008-05-09 00:14:49 +08:00
table.sort(data.Routes, compare)
luci.template.render("status-olsr/routes", {routes=data.Routes})
2008-05-09 00:14:49 +08:00
end
function action_topology()
local data = fetch_txtinfo("topology")
2008-05-09 00:14:49 +08:00
if not data or not data.Topology then
luci.template.render("status-olsr/error_olsr")
2008-05-09 00:14:49 +08:00
return nil
end
2008-05-09 00:14:49 +08:00
local function compare(a, b)
2008-09-14 01:51:03 +08:00
return a["Dest. IP"] < b["Dest. IP"]
2008-05-09 00:14:49 +08:00
end
2008-05-09 00:14:49 +08:00
table.sort(data.Topology, compare)
luci.template.render("status-olsr/topology", {routes=data.Topology})
2008-05-09 00:14:49 +08:00
end
function action_hna()
local data = fetch_txtinfo("hna")
2008-05-09 00:14:49 +08:00
if not data or not data.HNA then
luci.template.render("status-olsr/error_olsr")
2008-05-09 00:14:49 +08:00
return nil
end
2008-05-09 00:14:49 +08:00
local function compare(a, b)
2008-09-14 01:51:03 +08:00
return a.Destination < b.Destination
2008-05-09 00:14:49 +08:00
end
2008-05-09 00:14:49 +08:00
table.sort(data.HNA, compare)
luci.template.render("status-olsr/hna", {routes=data.HNA})
2008-05-09 00:14:49 +08:00
end
function action_mid()
local data = fetch_txtinfo("mid")
2008-05-09 00:14:49 +08:00
if not data or not data.MID then
luci.template.render("status-olsr/error_olsr")
2008-05-09 00:14:49 +08:00
return nil
end
2008-05-09 00:14:49 +08:00
local function compare(a, b)
2008-09-14 01:51:03 +08:00
return a["IP address"] < b["IP address"]
2008-05-09 00:14:49 +08:00
end
2008-05-09 00:14:49 +08:00
table.sort(data.MID, compare)
luci.template.render("status-olsr/mid", {mids=data.MID})
2008-05-09 00:14:49 +08:00
end
-- Internal
function fetch_txtinfo(otable)
require("luci.sys")
2008-05-09 00:14:49 +08:00
otable = otable or ""
local rawdata = luci.sys.httpget("http://127.0.0.1:2006/"..otable)
2008-05-09 00:14:49 +08:00
if #rawdata == 0 then
2009-07-19 08:24:58 +08:00
if nixio.fs.access("/proc/net/ipv6_route", "r") then
rawdata = luci.sys.httpget("http://[::1]:2006/"..otable)
if #rawdata == 0 then
return nil
end
else
return nil
end
2008-05-09 00:14:49 +08:00
end
2008-05-09 00:14:49 +08:00
local data = {}
2008-08-18 04:54:26 +08:00
local tables = luci.util.split(luci.util.trim(rawdata), "\r?\n\r?\n", nil, true)
2008-05-09 00:14:49 +08:00
for i, tbl in ipairs(tables) do
2008-08-18 04:54:26 +08:00
local lines = luci.util.split(tbl, "\r?\n", nil, true)
2008-05-09 00:14:49 +08:00
local name = table.remove(lines, 1):sub(8)
local keys = luci.util.split(table.remove(lines, 1), "\t")
local split = #keys - 1
2008-05-09 00:14:49 +08:00
data[name] = {}
2008-05-09 00:14:49 +08:00
for j, line in ipairs(lines) do
local fields = luci.util.split(line, "\t", split)
2008-05-09 00:14:49 +08:00
data[name][j] = {}
for k, key in pairs(keys) do
data[name][j][key] = fields[k]
2008-05-09 00:14:49 +08:00
end
2008-08-18 04:19:50 +08:00
if data[name][j].Linkcost then
data[name][j].LinkQuality,
data[name][j].NLQ,
data[name][j].ETX =
data[name][j].Linkcost:match("([%w.]+)/([%w.]+)[%s]+([%w.]+)")
2008-08-18 04:19:50 +08:00
end
2008-05-09 00:14:49 +08:00
end
end
2008-05-09 00:14:49 +08:00
return data
end