e2dc4fe445ad0af06a22eecd990c453b29d4b35e
[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 = call("action_index")
13         page.title  = "OLSR"
14         page.i18n   = "olsr"
15         page.subindex = true
16
17         local page  = node("admin", "status", "olsr", "routes")
18         page.target = call("action_routes")
19         page.title  = i18n("Routen")
20         page.order  = 10
21
22         local page  = node("admin", "status", "olsr", "topology")
23         page.target = call("action_topology")
24         page.title  = i18n("Topologie")
25         page.order  = 20
26
27         local page  = node("admin", "status", "olsr", "hna")
28         page.target = call("action_hna")
29         page.title  = "HNA"
30         page.order  = 30
31
32         local page  = node("admin", "status", "olsr", "mid")
33         page.target = call("action_mid")
34         page.title  = "MID"
35         page.order  = 50
36
37         local ol = entry(
38                 {"admin", "services", "olsrd"},
39                 cbi("olsr/olsrd"), "OLSR"
40         )
41         ol.i18n = "olsr"
42         ol.subindex = true
43
44         entry(
45                 {"admin", "services", "olsrd", "hna"},
46                 cbi("olsr/olsrdhna"), "HNA Announcements"
47         )
48
49         oplg = entry(
50                 {"admin", "services", "olsrd", "plugins"},
51                 cbi("olsr/olsrdplugins"), "Plugins"
52         )
53         oplg.i18n = "olsr"
54         oplg.leaf = true
55         oplg.subindex = true
56
57         local uci = require("luci.model.uci").cursor()
58         uci:foreach("olsrd", "LoadPlugin",
59                 function (section)
60                         local lib = section.library
61                         entry(
62                                 {"admin", "services", "olsrd", "plugins", lib },
63                                 cbi("olsr/olsrdplugins"),
64                                 nil --'Plugin "%s"' % lib:gsub("^olsrd_",""):gsub("%.so.+$","")
65                         )
66                 end
67         )
68 end
69
70 function action_index()
71         local data = fetch_txtinfo("links")
72
73         if not data or not data.Links then
74                 luci.template.render("status-olsr/error_olsr")
75                 return nil
76         end
77
78         local function compare(a, b)
79                 local c = tonumber(a.Cost)
80                 local d = tonumber(b.Cost)
81
82                 if not c or c == 0 then
83                         return false
84                 end
85
86                 if not d or d == 0 then
87                         return true
88                 end
89
90                 return c < d
91         end
92
93         table.sort(data.Links, compare)
94
95         luci.template.render("status-olsr/index", {links=data.Links})
96 end
97
98 function action_routes()
99         local data = fetch_txtinfo("routes")
100
101         if not data or not data.Routes then
102                 luci.template.render("status-olsr/error_olsr")
103                 return nil
104         end
105
106         local function compare(a, b)
107                 local c = tonumber(a.ETX)
108                 local d = tonumber(b.ETX)
109
110                 if not c or c == 0 then
111                         return false
112                 end
113
114                 if not d or d == 0 then
115                         return true
116                 end
117
118                 return c < d
119         end
120
121         table.sort(data.Routes, compare)
122
123         luci.template.render("status-olsr/routes", {routes=data.Routes})
124 end
125
126 function action_topology()
127         local data = fetch_txtinfo("topology")
128
129         if not data or not data.Topology then
130                 luci.template.render("status-olsr/error_olsr")
131                 return nil
132         end
133
134         local function compare(a, b)
135                 return a["Dest. IP"] < b["Dest. IP"]
136         end
137
138         table.sort(data.Topology, compare)
139
140         luci.template.render("status-olsr/topology", {routes=data.Topology})
141 end
142
143 function action_hna()
144         local data = fetch_txtinfo("hna")
145
146         if not data or not data.HNA then
147                 luci.template.render("status-olsr/error_olsr")
148                 return nil
149         end
150
151         local function compare(a, b)
152                 return a.Destination < b.Destination
153         end
154
155         table.sort(data.HNA, compare)
156
157         luci.template.render("status-olsr/hna", {routes=data.HNA})
158 end
159
160 function action_mid()
161         local data = fetch_txtinfo("mid")
162
163         if not data or not data.MID then
164                 luci.template.render("status-olsr/error_olsr")
165                 return nil
166         end
167
168         local function compare(a, b)
169                 return a["IP address"] < b["IP address"]
170         end
171
172         table.sort(data.MID, compare)
173
174         luci.template.render("status-olsr/mid", {mids=data.MID})
175 end
176
177
178 -- Internal
179 function fetch_txtinfo(otable)
180         require("luci.sys")
181         otable = otable or ""
182         local rawdata = luci.sys.httpget("http://127.0.0.1:2006/"..otable)
183
184         if #rawdata == 0 then
185                 if nixio.fs.access("/proc/net/ipv6_route", "r") then
186                         rawdata = luci.sys.httpget("http://[::1]:2006/"..otable)
187                         if #rawdata == 0 then
188                                 return nil
189                         end
190                 else
191                         return nil
192                 end
193         end
194
195         local data = {}
196
197         local tables = luci.util.split(luci.util.trim(rawdata), "\r?\n\r?\n", nil, true)
198
199
200         for i, tbl in ipairs(tables) do
201                 local lines = luci.util.split(tbl, "\r?\n", nil, true)
202                 local name  = table.remove(lines, 1):sub(8)
203                 local keys  = luci.util.split(table.remove(lines, 1), "\t")
204                 local split = #keys - 1
205
206                 data[name] = {}
207
208                 for j, line in ipairs(lines) do
209                         local fields = luci.util.split(line, "\t", split)
210                         data[name][j] = {}
211                         for k, key in pairs(keys) do
212                                 data[name][j][key] = fields[k]
213                         end
214
215                         if data[name][j].Linkcost then
216                                 data[name][j].LinkQuality,
217                                 data[name][j].NLQ,
218                                 data[name][j].ETX =
219                                 data[name][j].Linkcost:match("([%w.]+)/([%w.]+)[%s]+([%w.]+)")
220                         end
221                 end
222         end
223
224         return data
225 end