root/luci/branches/luci-0.10/applications/luci-vnstat/luasrc/model/cbi/vnstat.lua @ 7038

Revision 7038, 1.9 KB (checked in by jow, 2 years ago)

luci-0.10: merge trunk

Line 
1--[[
2LuCI - Lua Configuration Interface
3
4Copyright 2010-2011 Jo-Philipp Wich <xm@subsignal.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
13local utl = require "luci.util"
14local sys = require "luci.sys"
15local fs  = require "nixio.fs"
16local nw  = require "luci.model.network"
17
18local dbdir, line
19
20for line in io.lines("/etc/vnstat.conf") do
21    dbdir = line:match("^%s*DatabaseDir%s+[\"'](%S-)[\"']")
22    if dbdir then break end
23end
24
25dbdir = dbdir or "/var/lib/vnstat"
26
27
28m = Map("vnstat", translate("VnStat"),
29    translate("VnStat is a network traffic monitor for Linux that keeps a log of network traffic for the selected interface(s)."))
30
31m.submit = translate("Restart VnStat")
32m.reset  = false
33
34nw.init(luci.model.uci.cursor_state())
35
36local ifaces = { }
37local enabled = { }
38local iface
39
40if fs.access(dbdir) then
41    for iface in fs.dir(dbdir) do
42        if iface:sub(1,1) ~= '.' then
43            ifaces[iface] = iface
44            enabled[iface] = iface
45        end
46    end
47end
48
49for _, iface in ipairs(sys.net.devices()) do
50    ifaces[iface] = iface
51end
52
53
54local s = m:section(TypedSection, "vnstat")
55s.anonymous = true
56s.addremove = false
57
58mon_ifaces = s:option(Value, "interface", translate("Monitor selected interfaces"))
59mon_ifaces.template = "cbi/network_ifacelist"
60mon_ifaces.widget   = "checkbox"
61mon_ifaces.cast     = "table"
62
63function mon_ifaces.write(self, section, val)
64    local i
65    local s = { }
66
67    if val then
68        for _, i in ipairs(type(val) == "table" and val or { val }) do
69            s[i] = true
70        end
71    end
72
73    for i, _ in pairs(ifaces) do
74        if not s[i] then
75            fs.unlink(dbdir .. "/" .. i)
76            fs.unlink(dbdir .. "/." .. i)
77        end
78    end
79
80    if next(s) then
81        m.uci:set_list("vnstat", section, "interface", utl.keys(s))
82    else
83        m.uci:delete("vnstat", section, "interface")
84    end
85end
86
87mon_ifaces.remove = mon_ifaces.write
88
89return m
Note: See TracBrowser for help on using the browser.