2 LuCI - Lua Configuration Interface
4 Copyright 2010 Jo-Philipp Wich <xm@subsignal.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
13 local utl = require "luci.util"
14 local sys = require "luci.sys"
15 local fs = require "nixio.fs"
16 local nw = require "luci.model.network"
20 for line in io.lines("/etc/vnstat.conf") do
21 dbdir = line:match("^%s*DatabaseDir%s+[\"'](%S-)[\"']")
22 if dbdir then break end
25 dbdir = dbdir or "/var/lib/vnstat"
28 m = SimpleForm("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)."))
31 m.submit = translate("Restart VnStat")
34 nw.init(luci.model.uci.cursor_state())
40 for iface in fs.dir(dbdir) do
41 if iface:sub(1,1) ~= '.' then
43 enabled[iface] = iface
47 for _, iface in ipairs(sys.net.devices()) do
52 local s = m:section(SimpleSection)
54 mon_ifaces = s:option(Value, "ifaces", translate("Monitor selected interfaces"))
55 mon_ifaces.template = "cbi/network_ifacelist"
56 mon_ifaces.widget = "checkbox"
57 mon_ifaces.default = utl.keys(enabled)
59 function mon_ifaces.write(self, s, val)
64 for _, i in ipairs(type(val) == "table" and val or { val }) do
69 for i, _ in pairs(ifaces) do
71 sys.call("vnstat -u -i %q" % i)
73 fs.unlink(dbdir .. "/" .. i)
74 fs.unlink(dbdir .. "/." .. i)
79 sys.call("/etc/init.d/vnstat restart >/dev/null 2>/dev/null")
81 m.message = "<p><strong>%s</strong></p>"
82 % translate("The VnStat service has been restarted."), cmd
84 self.default = utl.keys(s)
87 mon_ifaces.remove = mon_ifaces.write