1 -- Copyright 2010-2011 Jo-Philipp Wich <jow@openwrt.org>
2 -- Licensed to the public under the Apache License 2.0.
4 local utl = require "luci.util"
5 local sys = require "luci.sys"
6 local fs = require "nixio.fs"
7 local nw = require "luci.model.network"
11 for line in io.lines("/etc/vnstat.conf") do
12 dbdir = line:match("^%s*DatabaseDir%s+[\"'](%S-)[\"']")
13 if dbdir then break end
16 dbdir = dbdir or "/var/lib/vnstat"
19 m = Map("vnstat", translate("VnStat"),
20 translate("VnStat is a network traffic monitor for Linux that keeps a log of network traffic for the selected interface(s)."))
22 m.submit = translate("Restart VnStat")
25 nw.init(luci.model.uci.cursor_state())
31 if fs.access(dbdir) then
32 for iface in fs.dir(dbdir) do
33 if iface:sub(1,1) ~= '.' then
35 enabled[iface] = iface
40 for _, iface in ipairs(sys.net.devices()) do
45 local s = m:section(TypedSection, "vnstat")
49 mon_ifaces = s:option(Value, "interface", translate("Monitor selected interfaces"))
50 mon_ifaces.template = "cbi/network_ifacelist"
51 mon_ifaces.widget = "checkbox"
52 mon_ifaces.cast = "table"
53 mon_ifaces.noinactive = true
54 mon_ifaces.nocreate = true
56 function mon_ifaces.write(self, section, val)
61 for _, i in ipairs(type(val) == "table" and val or { val }) do
66 for i, _ in pairs(ifaces) do
68 fs.unlink(dbdir .. "/" .. i)
69 fs.unlink(dbdir .. "/." .. i)
74 m.uci:set_list("vnstat", section, "interface", utl.keys(s))
76 m.uci:delete("vnstat", section, "interface")
80 mon_ifaces.remove = mon_ifaces.write