f4f9b97c0dc193722480a17f51459b326460c643
[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 module("luci.controller.freifunk.freifunk", package.seeall)
15
16 function index()
17 local i18n = luci.i18n.translate
18
19 local page = node()
20 page.lock = true
21 page.target = alias("freifunk")
22 page.subindex = true
23 page.index = false
24
25 local page = node("freifunk")
26 page.title = "Freifunk"
27 page.target = alias("freifunk", "index")
28 page.order = 5
29 page.setuser = "nobody"
30 page.setgroup = "nogroup"
31 page.i18n = "freifunk"
32 page.index = true
33
34 local page = node("freifunk", "index")
35 page.target = template("freifunk/index")
36 page.title = "Übersicht"
37 page.order = 10
38 page.indexignore = true
39
40 local page = node("freifunk", "index", "contact")
41 page.target = template("freifunk/contact")
42 page.title = "Kontakt"
43
44 local page = node("freifunk", "status")
45 page.target = template("freifunk/public_status")
46 page.title = i18n("Status")
47 page.order = 20
48 page.i18n = "base"
49 page.setuser = false
50 page.setgroup = false
51
52 entry({"freifunk", "status.json"}, call("jsonstatus"))
53 entry({"freifunk", "status", "zeroes"}, call("zeroes"), "Testdownload")
54 entry({"freifunk", "status", "public_status_json"}, call("public_status_json")).leaf = true
55
56 assign({"freifunk", "olsr"}, {"admin", "status", "olsr"}, "OLSR", 30)
57
58 if nixio.fs.access("/etc/config/luci_statistics") then
59 assign({"freifunk", "graph"}, {"admin", "statistics", "graph"}, i18n("Statistics"), 40)
60 end
61
62 assign({"mini", "freifunk"}, {"admin", "freifunk"}, "Freifunk", 15)
63 entry({"admin", "freifunk"}, alias("admin", "freifunk", "index"), "Freifunk", 15)
64 local page = node("admin", "freifunk", "index")
65 page.target = cbi("freifunk/freifunk")
66 page.title = "Freifunk"
67 page.order = 30
68
69 local page = node("admin", "freifunk", "Index-Page")
70 page.target = cbi("freifunk/user_index")
71 page.title = "Index-Page"
72 page.order = 35
73
74 local page = node("admin", "freifunk", "contact")
75 page.target = cbi("freifunk/contact")
76 page.title = "Kontakt"
77 page.order = 40
78
79 entry({"freifunk", "map"}, template("freifunk-map/frame"), i18n("Karte"), 50)
80 entry({"freifunk", "map", "content"}, template("freifunk-map/map"), nil, 51)
81
82 entry({"freifunk", "services"}, template("freifunk-services/services"), i18n("Services"), 60)
83 entry({"freifunk", "services", "content"}, template("freifunk-services/services"), nil, 61)
84
85 end
86
87 local function fetch_olsrd()
88 local sys = require "luci.sys"
89 local util = require "luci.util"
90 local table = require "table"
91 local rawdata = sys.httpget("http://127.0.0.1:2006/")
92
93 if #rawdata == 0 then
94 if nixio.fs.access("/proc/net/ipv6_route", "r") then
95 rawdata = sys.httpget("http://[::1]:2006/")
96 if #rawdata == 0 then
97 return nil
98 end
99 else
100 return nil
101 end
102 end
103
104 local data = {}
105
106 local tables = util.split(util.trim(rawdata), "\r?\n\r?\n", nil, true)
107
108
109 for i, tbl in ipairs(tables) do
110 local lines = util.split(tbl, "\r?\n", nil, true)
111 local name = table.remove(lines, 1):sub(8)
112 local keys = util.split(table.remove(lines, 1), "\t")
113 local split = #keys - 1
114
115 data[name] = {}
116
117 for j, line in ipairs(lines) do
118 local fields = util.split(line, "\t", split)
119 data[name][j] = {}
120 for k, key in pairs(keys) do
121 data[name][j][key] = fields[k]
122 end
123
124 if data[name][j].Linkcost then
125 data[name][j].LinkQuality,
126 data[name][j].NLQ,
127 data[name][j].ETX =
128 data[name][j].Linkcost:match("([%w.]+)/([%w.]+)[%s]+([%w.]+)")
129 end
130 end
131 end
132
133 return data
134 end
135
136 function zeroes()
137 local string = require "string"
138 local http = require "luci.http"
139 local zeroes = string.rep(string.char(0), 8192)
140 local cnt = 0
141 local lim = 1024 * 1024 * 1024
142
143 http.prepare_content("application/x-many-zeroes")
144
145 while cnt < lim do
146 http.write(zeroes)
147 cnt = cnt + #zeroes
148 end
149 end
150
151 function jsonstatus()
152 local root = {}
153 local sys = require "luci.sys"
154 local uci = require "luci.model.uci"
155 local util = require "luci.util"
156 local http = require "luci.http"
157 local json = require "luci.json"
158 local ltn12 = require "luci.ltn12"
159 local version = require "luci.version"
160 local webadmin = require "luci.tools.webadmin"
161
162 local cursor = uci.cursor_state()
163
164 local ffzone = webadmin.firewall_find_zone("freifunk")
165 local ffznet = ffzone and cursor:get("firewall", ffzone, "network")
166 local ffwifs = ffznet and util.split(ffznet, " ") or {}
167
168
169 root.protocol = 1
170
171 root.system = {
172 uptime = {sys.uptime()},
173 loadavg = {sys.loadavg()},
174 sysinfo = {sys.sysinfo()},
175 hostname = sys.hostname()
176 }
177
178 root.firmware = {
179 luciname=version.luciname,
180 luciversion=version.luciversion,
181 distname=version.distname,
182 distversion=version.distversion
183 }
184
185 root.freifunk = {}
186 cursor:foreach("freifunk", "public", function(s)
187 root.freifunk[s[".name"]] = s
188 end)
189
190 cursor:foreach("system", "system", function(s)
191 root.geo = {
192 latitude = s.latitude,
193 longitude = s.longitude
194 }
195 end)
196
197 root.network = {}
198 root.wireless = {devices = {}, interfaces = {}, status = {}}
199 local wifs = root.wireless.interfaces
200 local wifidata = luci.sys.wifi.getiwconfig() or {}
201 local netdata = luci.sys.net.deviceinfo() or {}
202
203 for _, vif in ipairs(ffwifs) do
204 root.network[vif] = cursor:get_all("network", vif)
205 root.wireless.devices[vif] = cursor:get_all("wireless", vif)
206 cursor:foreach("wireless", "wifi-iface", function(s)
207 if s.device == vif and s.network == vif then
208 wifs[#wifs+1] = s
209 if s.ifname then
210 root.wireless.status[s.ifname] = wifidata[s.ifname]
211 end
212 end
213 end)
214 end
215
216 root.olsrd = fetch_olsrd()
217
218 http.prepare_content("application/json")
219 ltn12.pump.all(json.Encoder(root):source(), http.write)
220 end
221
222 function public_status_json()
223 local twa = require "luci.tools.webadmin"
224 local sys = require "luci.sys"
225 local i18n = require "luci.i18n"
226 local path = luci.dispatcher.context.requestpath
227 local rv = { }
228
229 local dev
230 for dev in path[#path]:gmatch("[%w%.%-]+") do
231 local j = { id = dev }
232 local iw = luci.sys.wifi.getiwinfo(dev)
233 if iw then
234 local f
235 for _, f in ipairs({
236 "channel", "txpower", "bitrate", "signal", "noise",
237 "quality", "quality_max", "mode", "ssid", "bssid", "encryption", "ifname"
238 }) do
239 j[f] = iw[f]
240 end
241 end
242 rv[#rv+1] = j
243 end
244
245 local load1, load5, load15 = sys.loadavg()
246
247 local _, _, memtotal, memcached, membuffers, memfree = sys.sysinfo()
248 local mem = string.format("%.2f MB (%.2f %s, %.2f %s, %.2f %s, %.2f %s)",
249 tonumber(memtotal) / 1024,
250 tonumber(memtotal - memfree) / 1024,
251 tostring(i18n.translate("used")),
252 memfree / 1024,
253 tostring(i18n.translate("free")),
254 memcached / 1024,
255 tostring(i18n.translate("cached")),
256 membuffers / 1024,
257 tostring(i18n.translate("buffered"))
258 )
259
260 local dr4 = sys.net.defaultroute()
261 local dr6 = sys.net.defaultroute6()
262
263 rv[#rv+1] = {
264 time = os.date("%c"),
265 uptime = twa.date_format(tonumber(sys.uptime())),
266 load = string.format("%.2f, %.2f, %.2f", load1, load5, load15),
267 mem = mem,
268 defroutev4 = { gateway = dr4.gateway:string(),
269 dest = dr4.dest:string(),
270 dev = dr4.device,
271 metr = dr4.metric },
272 defroutev6 = { gateway = dr6.nexthop:string(),
273 dest = dr6.dest:string(),
274 dev = dr6.device,
275 metr = dr6.metric }
276 }
277
278 luci.http.prepare_content("application/json")
279 luci.http.write_json(rv)
280 return
281 end
282