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