vnstat.lua 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. -- Copyright 2010-2011 Jo-Philipp Wich <jow@openwrt.org>
  2. -- Licensed to the public under the Apache License 2.0.
  3. local utl = require "luci.util"
  4. local sys = require "luci.sys"
  5. local fs = require "nixio.fs"
  6. local nw = require "luci.model.network"
  7. local dbdir, line
  8. for line in io.lines("/etc/vnstat.conf") do
  9. dbdir = line:match("^%s*DatabaseDir%s+[\"'](%S-)[\"']")
  10. if dbdir then break end
  11. end
  12. dbdir = dbdir or "/var/lib/vnstat"
  13. m = Map("vnstat", translate("VnStat"),
  14. translate("VnStat is a network traffic monitor for Linux that keeps a log of network traffic for the selected interface(s)."))
  15. m.submit = translate("Restart VnStat")
  16. m.reset = false
  17. nw.init(luci.model.uci.cursor_state())
  18. local ifaces = { }
  19. local enabled = { }
  20. local iface
  21. if fs.access(dbdir) then
  22. for iface in fs.dir(dbdir) do
  23. if iface:sub(1,1) ~= '.' then
  24. ifaces[iface] = iface
  25. enabled[iface] = iface
  26. end
  27. end
  28. end
  29. for _, iface in ipairs(sys.net.devices()) do
  30. ifaces[iface] = iface
  31. end
  32. local s = m:section(TypedSection, "vnstat")
  33. s.anonymous = true
  34. s.addremove = false
  35. mon_ifaces = s:option(Value, "interface", translate("Monitor selected interfaces"))
  36. mon_ifaces.template = "cbi/network_ifacelist"
  37. mon_ifaces.widget = "checkbox"
  38. mon_ifaces.cast = "table"
  39. mon_ifaces.noinactive = true
  40. mon_ifaces.nocreate = true
  41. function mon_ifaces.write(self, section, val)
  42. local i
  43. local s = { }
  44. if val then
  45. for _, i in ipairs(type(val) == "table" and val or { val }) do
  46. s[i] = true
  47. end
  48. end
  49. for i, _ in pairs(ifaces) do
  50. if not s[i] then
  51. fs.unlink(dbdir .. "/" .. i)
  52. fs.unlink(dbdir .. "/." .. i)
  53. end
  54. end
  55. if next(s) then
  56. m.uci:set_list("vnstat", section, "interface", utl.keys(s))
  57. else
  58. m.uci:delete("vnstat", section, "interface")
  59. end
  60. end
  61. mon_ifaces.remove = mon_ifaces.write
  62. return m