luci-0.11: merge outstanding trunk changes
[project/luci.git] / modules / freifunk / luasrc / controller / freifunk / freifunk.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2008 Steven Barth <steven@midlink.org>
5
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 $Id$
13 ]]--
14
15 module("luci.controller.freifunk.freifunk", package.seeall)
16
17 function index()
18 local uci = require "luci.model.uci".cursor()
19 local page
20
21 -- Frontend
22 page = node()
23 page.lock = true
24 page.target = alias("freifunk")
25 page.subindex = true
26 page.index = false
27
28 page = node("freifunk")
29 page.title = _("Freifunk")
30 page.target = alias("freifunk", "index")
31 page.order = 5
32 page.setuser = "nobody"
33 page.setgroup = "nogroup"
34 page.i18n = "freifunk"
35 page.index = true
36
37 page = node("freifunk", "index")
38 page.target = template("freifunk/index")
39 page.title = _("Overview")
40 page.order = 10
41 page.indexignore = true
42
43 page = node("freifunk", "contact")
44 page.target = template("freifunk/contact")
45 page.title = _("Contact")
46 page.order = 15
47
48 page = node("freifunk", "status")
49 page.target = template("freifunk/public_status")
50 page.title = _("Status")
51 page.order = 20
52 page.i18n = "base"
53 page.setuser = false
54 page.setgroup = false
55
56 entry({"freifunk", "status.json"}, call("jsonstatus"))
57 entry({"freifunk", "status", "zeroes"}, call("zeroes"), "Testdownload")
58 entry({"freifunk", "status", "public_status_json"}, call("public_status_json")).leaf = true
59
60 if nixio.fs.access("/usr/sbin/luci-splash") then
61 assign({"freifunk", "status", "splash"}, {"splash", "publicstatus"}, _("Splash"), 40)
62 end
63
64 assign({"freifunk", "olsr"}, {"admin", "status", "olsr"}, _("OLSR"), 30)
65
66 if nixio.fs.access("/etc/config/luci_statistics") then
67 assign({"freifunk", "graph"}, {"admin", "statistics", "graph"}, _("Statistics"), 40)
68 end
69
70 -- backend
71 assign({"mini", "freifunk"}, {"admin", "freifunk"}, _("Freifunk"), 5)
72 entry({"admin", "freifunk"}, alias("admin", "freifunk", "index"), _("Freifunk"), 5)
73
74 page = node("admin", "freifunk")
75 page.target = template("freifunk/adminindex")
76 page.title = _("Freifunk")
77 page.order = 5
78
79 page = node("admin", "freifunk", "basics")
80 page.target = cbi("freifunk/basics")
81 page.title = _("Basic Settings")
82 page.order = 5
83
84 page = node("admin", "freifunk", "basics", "profile")
85 page.target = cbi("freifunk/profile")
86 page.title = _("Profile")
87 page.order = 10
88
89 page = node("admin", "freifunk", "basics", "profile_expert")
90 page.target = cbi("freifunk/profile_expert")
91 page.title = _("Profile (Expert)")
92 page.order = 20
93
94 page = node("admin", "freifunk", "Index-Page")
95 page.target = cbi("freifunk/user_index")
96 page.title = _("Index Page")
97 page.order = 50
98
99 page = node("admin", "freifunk", "contact")
100 page.target = cbi("freifunk/contact")
101 page.title = _("Contact")
102 page.order = 15
103
104 entry({"freifunk", "map"}, template("freifunk-map/frame"), _("Map"), 50)
105 entry({"freifunk", "map", "content"}, template("freifunk-map/map"), nil, 51)
106 entry({"admin", "freifunk", "profile_error"}, template("freifunk/profile_error"))
107 end
108
109 local function fetch_olsrd()
110 local sys = require "luci.sys"
111 local util = require "luci.util"
112 local table = require "table"
113 local rawdata = sys.httpget("http://127.0.0.1:2006/")
114
115 if #rawdata == 0 then
116 if nixio.fs.access("/proc/net/ipv6_route", "r") then
117 rawdata = sys.httpget("http://[::1]:2006/")
118 if #rawdata == 0 then
119 return nil
120 end
121 else
122 return nil
123 end
124 end
125
126 local data = {}
127
128 local tables = util.split(util.trim(rawdata), "\r?\n\r?\n", nil, true)
129
130
131 for i, tbl in ipairs(tables) do
132 local lines = util.split(tbl, "\r?\n", nil, true)
133 local name = table.remove(lines, 1):sub(8)
134 local keys = util.split(table.remove(lines, 1), "\t")
135 local split = #keys - 1
136
137 data[name] = {}
138
139 for j, line in ipairs(lines) do
140 local fields = util.split(line, "\t", split)
141 data[name][j] = {}
142 for k, key in pairs(keys) do
143 data[name][j][key] = fields[k]
144 end
145
146 if data[name][j].Linkcost then
147 data[name][j].LinkQuality,
148 data[name][j].NLQ,
149 data[name][j].ETX =
150 data[name][j].Linkcost:match("([%w.]+)/([%w.]+)[%s]+([%w.]+)")
151 end
152 end
153 end
154
155 return data
156 end
157
158 function zeroes()
159 local string = require "string"
160 local http = require "luci.http"
161 local zeroes = string.rep(string.char(0), 8192)
162 local cnt = 0
163 local lim = 1024 * 1024 * 1024
164
165 http.prepare_content("application/x-many-zeroes")
166
167 while cnt < lim do
168 http.write(zeroes)
169 cnt = cnt + #zeroes
170 end
171 end
172
173 function jsonstatus()
174 local root = {}
175 local sys = require "luci.sys"
176 local uci = require "luci.model.uci"
177 local util = require "luci.util"
178 local http = require "luci.http"
179 local json = require "luci.json"
180 local ltn12 = require "luci.ltn12"
181 local version = require "luci.version"
182 local webadmin = require "luci.tools.webadmin"
183
184 local cursor = uci.cursor_state()
185
186 local ffzone = webadmin.firewall_find_zone("freifunk")
187 local ffznet = ffzone and cursor:get("firewall", ffzone, "network")
188 local ffwifs = ffznet and util.split(ffznet, " ") or {}
189
190
191 root.protocol = 1
192
193 root.system = {
194 uptime = {sys.uptime()},
195 loadavg = {sys.loadavg()},
196 sysinfo = {sys.sysinfo()},
197 hostname = sys.hostname()
198 }
199
200 root.firmware = {
201 luciname=version.luciname,
202 luciversion=version.luciversion,
203 distname=version.distname,
204 distversion=version.distversion
205 }
206
207 root.freifunk = {}
208 cursor:foreach("freifunk", "public", function(s)
209 root.freifunk[s[".name"]] = s
210 end)
211
212 cursor:foreach("system", "system", function(s)
213 root.geo = {
214 latitude = s.latitude,
215 longitude = s.longitude
216 }
217 end)
218
219 root.network = {}
220 root.wireless = {devices = {}, interfaces = {}, status = {}}
221 local wifs = root.wireless.interfaces
222 local netdata = luci.sys.net.deviceinfo() or {}
223
224 for _, vif in ipairs(ffwifs) do
225 root.network[vif] = cursor:get_all("network", vif)
226 root.wireless.devices[vif] = cursor:get_all("wireless", vif)
227 cursor:foreach("wireless", "wifi-iface", function(s)
228 if s.device == vif and s.network == vif then
229 wifs[#wifs+1] = s
230 if s.ifname then
231 local iwinfo = luci.sys.wifi.getiwinfo(s.ifname)
232 if iwinfo then
233 root.wireless.status[s.ifname] = { }
234
235 local _, f
236 for _, f in ipairs({
237 "channel", "txpower", "bitrate", "signal", "noise",
238 "quality", "quality_max", "mode", "ssid", "bssid", "encryption", "ifname"
239 }) do
240 root.wireless.status[s.ifname][f] = iwinfo[f]
241 end
242 end
243 end
244 end
245 end)
246 end
247
248 root.olsrd = fetch_olsrd()
249
250 http.prepare_content("application/json")
251 ltn12.pump.all(json.Encoder(root):source(), http.write)
252 end
253
254 function public_status_json(devs)
255 local twa = require "luci.tools.webadmin"
256 local sys = require "luci.sys"
257 local i18n = require "luci.i18n"
258 local rv = { }
259
260 local dev
261 for dev in devs:gmatch("[%w%.%-]+") do
262 local j = { id = dev }
263 local iw = luci.sys.wifi.getiwinfo(dev)
264 if iw then
265 local f
266 for _, f in ipairs({
267 "channel", "txpower", "bitrate", "signal", "noise",
268 "quality", "quality_max", "mode", "ssid", "bssid", "encryption", "ifname"
269 }) do
270 j[f] = iw[f]
271 end
272 end
273 rv[#rv+1] = j
274 end
275
276 local load1, load5, load15 = sys.loadavg()
277
278 local _, _, memtotal, memcached, membuffers, memfree = sys.sysinfo()
279 local mem = string.format("%.2f MB (%.2f %s, %.2f %s, %.2f %s, %.2f %s)",
280 tonumber(memtotal) / 1024,
281 tonumber(memtotal - memfree) / 1024,
282 tostring(i18n.translate("used")),
283 memfree / 1024,
284 tostring(i18n.translate("free")),
285 memcached / 1024,
286 tostring(i18n.translate("cached")),
287 membuffers / 1024,
288 tostring(i18n.translate("buffered"))
289 )
290
291 local dr4 = sys.net.defaultroute()
292 local dr6 = sys.net.defaultroute6()
293
294 if dr6 then
295 def6 = {
296 gateway = dr6.nexthop:string(),
297 dest = dr6.dest:string(),
298 dev = dr6.device,
299 metr = dr6.metric }
300 end
301
302 if dr4 then
303 def4 = {
304 gateway = dr4.gateway:string(),
305 dest = dr4.dest:string(),
306 dev = dr4.device,
307 metr = dr4.metric }
308 else
309 local dr = sys.exec("ip r s t olsr-default")
310 if dr then
311 local dest, gateway, dev, metr = dr:match("^(%w+) via (%d+.%d+.%d+.%d+) dev (%w+) +metric (%d+)")
312 def4 = {
313 dest = dest,
314 gateway = gateway,
315 dev = dev,
316 metr = metr
317 }
318 end
319 end
320
321 rv[#rv+1] = {
322 time = os.date("%a, %d %b %Y, %H:%M:%S"),
323 uptime = twa.date_format(tonumber(sys.uptime())),
324 load = string.format("%.2f, %.2f, %.2f", load1, load5, load15),
325 mem = mem,
326 defroutev4 = def4,
327 defroutev6 = def6
328 }
329
330 luci.http.prepare_content("application/json")
331 luci.http.write_json(rv)
332 return
333 end