1
0

config.lua 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. -- Copyright 2017 Jo-Philipp Wich <jo@mein.io>
  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 ip = require "luci.ip"
  7. local nw = require "luci.model.network"
  8. local s, m, period, warning, date, days, interval, ifaces, subnets, limit, prealloc, compress, generations, commit, refresh, directory, protocols
  9. m = Map("nlbwmon", translate("Netlink Bandwidth Monitor - Configuration"),
  10. translate("The Netlink Bandwidth Monitor (nlbwmon) is a lightweight, efficient traffic accounting program keeping track of bandwidth usage per host and protocol."))
  11. nw.init(luci.model.uci.cursor_state())
  12. s = m:section(TypedSection, "nlbwmon")
  13. s.anonymous = true
  14. s.addremove = false
  15. s:tab("general", translate("General Settings"))
  16. s:tab("advanced", translate("Advanced Settings"))
  17. s:tab("protocol", translate("Protocol Mapping"),
  18. translate("Protocol mappings to distinguish traffic types per host, one mapping per line. The first value specifies the IP protocol, the second value the port number and the third column is the name of the mapped protocol."))
  19. period = s:taboption("general", ListValue, "_period", translate("Accounting period"),
  20. translate("Choose \"Day of month\" to restart the accounting period monthly on a specific date, e.g. every 3rd. Choose \"Fixed interval\" to restart the accounting period exactly every N days, beginning at a given date."))
  21. period:value("relative", translate("Day of month"))
  22. period:value("absolute", translate("Fixed interval"))
  23. period.write = function(self, cfg, val)
  24. if period:formvalue(cfg) == "relative" then
  25. m:set(cfg, "database_interval", interval:formvalue(cfg))
  26. else
  27. m:set(cfg, "database_interval", "%s/%s" %{
  28. date:formvalue(cfg),
  29. days:formvalue(cfg)
  30. })
  31. end
  32. end
  33. period.cfgvalue = function(self, cfg)
  34. local val = m:get(cfg, "database_interval") or ""
  35. if val:match("^%d%d%d%d%-%d%d%-%d%d/%d+$") then
  36. return "absolute"
  37. end
  38. return "relative"
  39. end
  40. warning = s:taboption("general", DummyValue, "_warning", translate("Warning"))
  41. warning.default = translatef("Changing the accounting interval type will invalidate existing databases!<br /><strong><a href=\"%s\">Download backup</a></strong>.", luci.dispatcher.build_url("admin/nlbw/backup"))
  42. warning.rawhtml = true
  43. if (m.uci:get_first("nlbwmon", "nlbwmon", "database_interval") or ""):match("^%d%d%d%d-%d%d-%d%d/%d+$") then
  44. warning:depends("_period", "relative")
  45. else
  46. warning:depends("_period", "absolute")
  47. end
  48. interval = s:taboption("general", Value, "_interval", translate("Due date"),
  49. translate("Day of month to restart the accounting period. Use negative values to count towards the end of month, e.g. \"-5\" to specify the 27th of July or the 24th of Februrary."))
  50. interval.datatype = "or(range(1,31),range(-31,-1))"
  51. interval.placeholder = "1"
  52. interval:value("1", translate("1 - Restart every 1st of month"))
  53. interval:value("-1", translate("-1 - Restart every last day of month"))
  54. interval:value("-7", translate("-7 - Restart a week before end of month"))
  55. interval.rmempty = false
  56. interval:depends("_period", "relative")
  57. interval.write = period.write
  58. interval.cfgvalue = function(self, cfg)
  59. local val = m:get(cfg, "database_interval")
  60. return val and tonumber(val)
  61. end
  62. date = s:taboption("general", Value, "_date", translate("Start date"),
  63. translate("Start date of the first accounting period, e.g. begin of ISP contract."))
  64. date.datatype = "dateyyyymmdd"
  65. date.placeholder = "2016-03-15"
  66. date.rmempty = false
  67. date:depends("_period", "absolute")
  68. date.write = period.write
  69. date.cfgvalue = function(self, cfg)
  70. local val = m:get(cfg, "database_interval") or ""
  71. return (val:match("^(%d%d%d%d%-%d%d%-%d%d)/%d+$"))
  72. end
  73. days = s:taboption("general", Value, "_days", translate("Interval"),
  74. translate("Length of accounting interval in days."))
  75. days.datatype = "min(1)"
  76. days.placeholder = "30"
  77. days.rmempty = false
  78. days:depends("_period", "absolute")
  79. days.write = period.write
  80. days.cfgvalue = function(self, cfg)
  81. local val = m:get(cfg, "database_interval") or ""
  82. return (val:match("^%d%d%d%d%-%d%d%-%d%d/(%d+)$"))
  83. end
  84. ifaces = s:taboption("general", Value, "_ifaces", translate("Local interfaces"),
  85. translate("Only conntrack streams from or to any of these networks are counted."))
  86. ifaces.template = "cbi/network_netlist"
  87. ifaces.widget = "checkbox"
  88. ifaces.nocreate = true
  89. ifaces.cfgvalue = function(self, cfg)
  90. return m:get(cfg, "local_network")
  91. end
  92. ifaces.write = function(self, cfg)
  93. local item
  94. local items = {}
  95. for item in utl.imatch(subnets:formvalue(cfg)) do
  96. items[#items+1] = item
  97. end
  98. for item in utl.imatch(ifaces:formvalue(cfg)) do
  99. items[#items+1] = item
  100. end
  101. m:set(cfg, "local_network", items)
  102. end
  103. subnets = s:taboption("general", DynamicList, "_subnets", translate("Local subnets"),
  104. translate("Only conntrack streams from or to any of these subnets are counted."))
  105. subnets.datatype = "ipaddr"
  106. subnets.cfgvalue = function(self, cfg)
  107. local subnet
  108. local subnets = {}
  109. for subnet in utl.imatch(m:get(cfg, "local_network")) do
  110. subnet = ip.new(subnet)
  111. subnets[#subnets+1] = subnet and subnet:string()
  112. end
  113. return subnets
  114. end
  115. subnets.write = ifaces.write
  116. limit = s:taboption("advanced", Value, "database_limit", translate("Maximum entries"),
  117. translate("The maximum amount of entries that should be put into the database, setting the limit to 0 will allow databases to grow indefinitely."))
  118. limit.datatype = "uinteger"
  119. limit.placeholder = "10000"
  120. prealloc = s:taboption("advanced", Flag, "database_prealloc", translate("Preallocate database"),
  121. translate("Whether to preallocate the maximum possible database size in memory. This is mainly useful for memory constrained systems which might not be able to satisfy memory allocation after longer uptime periods."))
  122. prealloc:depends({["database_limit"] = "0", ["!reverse"] = true })
  123. compress = s:taboption("advanced", Flag, "database_compress", translate("Compress database"),
  124. translate("Whether to gzip compress archive databases. Compressing the database files makes accessing old data slightly slower but helps to reduce storage requirements."))
  125. compress.default = compress.enabled
  126. generations = s:taboption("advanced", Value, "database_generations", translate("Stored periods"),
  127. translate("Maximum number of accounting periods to keep, use zero to keep databases forever."))
  128. generations.datatype = "uinteger"
  129. generations.placeholder = "10"
  130. commit = s:taboption("advanced", Value, "commit_interval", translate("Commit interval"),
  131. translate("Interval at which the temporary in-memory database is committed to the persistent database directory."))
  132. commit.placeholder = "24h"
  133. commit:value("24h", translate("24h - least flash wear at the expense of data loss risk"))
  134. commit:value("12h", translate("12h - compromise between risk of data loss and flash wear"))
  135. commit:value("10m", translate("10m - frequent commits at the expense of flash wear"))
  136. commit:value("60s", translate("60s - commit minutely, useful for non-flash storage"))
  137. refresh = s:taboption("advanced", Value, "refresh_interval", translate("Refresh interval"),
  138. translate("Interval at which traffic counters of still established connections are refreshed from netlink information."))
  139. refresh.placeholder = "30s"
  140. refresh:value("30s", translate("30s - refresh twice per minute for reasonably current stats"))
  141. refresh:value("5m", translate("5m - rarely refresh to avoid frequently clearing conntrack counters"))
  142. directory = s:taboption("advanced", Value, "database_directory", translate("Database directory"),
  143. translate("Database storage directory. One file per accounting period will be placed into this directory."))
  144. directory.placeholder = "/var/lib/nlbwmon"
  145. protocols = s:taboption("protocol", TextValue, "_protocols")
  146. protocols.rows = 50
  147. protocols.cfgvalue = function(self, cfg)
  148. return fs.readfile("/usr/share/nlbwmon/protocols")
  149. end
  150. protocols.write = function(self, cfg, value)
  151. fs.writefile("/usr/share/nlbwmon/protocols", (value or ""):gsub("\r\n", "\n"))
  152. end
  153. protocols.remove = protocols.write
  154. return m