2 LuCI - Lua Configuration Interface
4 Copyright 2008 Steven Barth <steven@midlink.org>
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
10 http://www.apache.org/licenses/LICENSE-2.0
14 module("luci.controller.freifunk.freifunk", package.seeall)
17 local i18n = luci.i18n.translate
18 local uci = require "luci.model.uci".cursor()
22 page.target = alias("freifunk")
26 local page = node("freifunk")
27 page.title = "Freifunk"
28 page.target = alias("freifunk", "index")
30 page.setuser = "nobody"
31 page.setgroup = "nogroup"
32 page.i18n = "freifunk"
35 local page = node("freifunk", "index")
36 page.target = template("freifunk/index")
37 page.title = "Übersicht"
39 page.indexignore = true
41 local page = node("freifunk", "index", "contact")
42 page.target = template("freifunk/contact")
43 page.title = "Kontakt"
45 local page = node("freifunk", "status")
46 page.target = template("freifunk/public_status")
47 page.title = i18n("Status")
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
57 assign({"freifunk", "olsr"}, {"admin", "status", "olsr"}, "OLSR", 30)
59 if nixio.fs.access("/etc/config/luci_statistics") then
60 assign({"freifunk", "graph"}, {"admin", "statistics", "graph"}, i18n("Statistics"), 40)
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"
70 local page = node("admin", "freifunk", "Index-Page")
71 page.target = cbi("freifunk/user_index")
72 page.title = "Index-Page"
75 local page = node("admin", "freifunk", "contact")
76 page.target = cbi("freifunk/contact")
77 page.title = "Kontakt"
80 entry({"freifunk", "map"}, template("freifunk-map/frame"), i18n("Karte"), 50)
81 entry({"freifunk", "map", "content"}, template("freifunk-map/map"), nil, 51)
83 uci:foreach("olsrd", "LoadPlugin", function(s)
84 if s.library == "olsrd_nameservice.so.0.3" then
90 entry({"freifunk", "services"}, template("freifunk-services/services"), i18n("Services"), 60)
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/")
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
113 local tables = util.split(util.trim(rawdata), "\r?\n\r?\n", nil, true)
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
124 for j, line in ipairs(lines) do
125 local fields = util.split(line, "\t", split)
127 for k, key in pairs(keys) do
128 data[name][j][key] = fields[k]
131 if data[name][j].Linkcost then
132 data[name][j].LinkQuality,
135 data[name][j].Linkcost:match("([%w.]+)/([%w.]+)[%s]+([%w.]+)")
144 local string = require "string"
145 local http = require "luci.http"
146 local zeroes = string.rep(string.char(0), 8192)
148 local lim = 1024 * 1024 * 1024
150 http.prepare_content("application/x-many-zeroes")
158 function jsonstatus()
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"
169 local cursor = uci.cursor_state()
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 {}
179 uptime = {sys.uptime()},
180 loadavg = {sys.loadavg()},
181 sysinfo = {sys.sysinfo()},
182 hostname = sys.hostname()
186 luciname=version.luciname,
187 luciversion=version.luciversion,
188 distname=version.distname,
189 distversion=version.distversion
193 cursor:foreach("freifunk", "public", function(s)
194 root.freifunk[s[".name"]] = s
197 cursor:foreach("system", "system", function(s)
199 latitude = s.latitude,
200 longitude = s.longitude
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 {}
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
217 root.wireless.status[s.ifname] = wifidata[s.ifname]
223 root.olsrd = fetch_olsrd()
225 http.prepare_content("application/json")
226 ltn12.pump.all(json.Encoder(root):source(), http.write)
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
237 for dev in path[#path]:gmatch("[%w%.%-]+") do
238 local j = { id = dev }
239 local iw = luci.sys.wifi.getiwinfo(dev)
243 "channel", "txpower", "bitrate", "signal", "noise",
244 "quality", "quality_max", "mode", "ssid", "bssid", "encryption", "ifname"
252 local load1, load5, load15 = sys.loadavg()
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")),
260 tostring(i18n.translate("free")),
262 tostring(i18n.translate("cached")),
264 tostring(i18n.translate("buffered"))
267 local dr4 = sys.net.defaultroute()
268 local dr6 = sys.net.defaultroute6()
272 gateway = dr6.nexthop:string(),
273 dest = dr6.dest:string(),
280 gateway = dr4.gateway:string(),
281 dest = dr4.dest:string(),
287 time = os.date("%c"),
288 uptime = twa.date_format(tonumber(sys.uptime())),
289 load = string.format("%.2f, %.2f, %.2f", load1, load5, load15),
295 luci.http.prepare_content("application/json")
296 luci.http.write_json(rv)