root/luci/branches/luci-0.10/modules/freifunk/luasrc/controller/freifunk/freifunk.lua @ 6980

Revision 6980, 7.8 KB (checked in by soma, 2 years ago)

luci-0.10: Merge r6979

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