1 -- Copyright 2017 Jo-Philipp Wich <jo@mein.io>
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 ip = require "luci.ip"
8 local nw = require "luci.model.network"
10 local s, m, period, warning, date, days, interval, ifaces, subnets, limit, prealloc, compress, generations, commit, refresh, directory, protocols
12 m = Map("nlbwmon", translate("Netlink Bandwidth Monitor - Configuration"),
13 translate("The Netlink Bandwidth Monitor (nlbwmon) is a lightweight, efficient traffic accounting program keeping track of bandwidth usage per host and protocol."))
15 nw.init(luci.model.uci.cursor_state())
17 s = m:section(TypedSection, "nlbwmon")
20 s:tab("general", translate("General Settings"))
21 s:tab("advanced", translate("Advanced Settings"))
22 s:tab("protocol", translate("Protocol Mapping"),
23 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."))
25 period = s:taboption("general", ListValue, "_period", translate("Accounting period"),
26 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."))
28 period:value("relative", translate("Day of month"))
29 period:value("absolute", translate("Fixed interval"))
31 period.write = function(self, cfg, val)
32 if period:formvalue(cfg) == "relative" then
33 m:set(cfg, "database_interval", interval:formvalue(cfg))
35 m:set(cfg, "database_interval", "%s/%s" %{
42 period.cfgvalue = function(self, cfg)
43 local val = m:get(cfg, "database_interval") or ""
44 if val:match("^%d%d%d%d%-%d%d%-%d%d/%d+$") then
51 warning = s:taboption("general", DummyValue, "_warning", translate("Warning"))
52 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"))
53 warning.rawhtml = true
55 if (m.uci:get_first("nlbwmon", "nlbwmon", "database_interval") or ""):match("^%d%d%d%d-%d%d-%d%d/%d+$") then
56 warning:depends("_period", "relative")
58 warning:depends("_period", "absolute")
62 interval = s:taboption("general", Value, "_interval", translate("Due date"),
63 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."))
65 interval.datatype = "or(range(1,31),range(-31,-1))"
66 interval.placeholder = "1"
67 interval:value("1", translate("1 - Restart every 1st of month"))
68 interval:value("-1", translate("-1 - Restart every last day of month"))
69 interval:value("-7", translate("-7 - Restart a week before end of month"))
70 interval.rmempty = false
71 interval:depends("_period", "relative")
72 interval.write = period.write
74 interval.cfgvalue = function(self, cfg)
75 local val = m:get(cfg, "database_interval")
76 return val and tonumber(val)
80 date = s:taboption("general", Value, "_date", translate("Start date"),
81 translate("Start date of the first accounting period, e.g. begin of ISP contract."))
83 date.datatype = "dateyyyymmdd"
84 date.placeholder = "2016-03-15"
86 date:depends("_period", "absolute")
87 date.write = period.write
89 date.cfgvalue = function(self, cfg)
90 local val = m:get(cfg, "database_interval") or ""
91 return (val:match("^(%d%d%d%d%-%d%d%-%d%d)/%d+$"))
95 days = s:taboption("general", Value, "_days", translate("Interval"),
96 translate("Length of accounting interval in days."))
98 days.datatype = "min(1)"
99 days.placeholder = "30"
101 days:depends("_period", "absolute")
102 days.write = period.write
104 days.cfgvalue = function(self, cfg)
105 local val = m:get(cfg, "database_interval") or ""
106 return (val:match("^%d%d%d%d%-%d%d%-%d%d/(%d+)$"))
110 ifaces = s:taboption("general", Value, "_ifaces", translate("Local interfaces"),
111 translate("Only conntrack streams from or to any of these networks are counted."))
113 ifaces.template = "cbi/network_netlist"
114 ifaces.widget = "checkbox"
115 ifaces.nocreate = true
117 ifaces.cfgvalue = function(self, cfg)
118 return m:get(cfg, "local_network")
121 ifaces.write = function(self, cfg)
124 for item in utl.imatch(subnets:formvalue(cfg)) do
125 items[#items+1] = item
127 for item in utl.imatch(ifaces:formvalue(cfg)) do
128 items[#items+1] = item
130 m:set(cfg, "local_network", items)
134 subnets = s:taboption("general", DynamicList, "_subnets", translate("Local subnets"),
135 translate("Only conntrack streams from or to any of these subnets are counted."))
137 subnets.datatype = "ipaddr"
139 subnets.cfgvalue = function(self, cfg)
142 for subnet in utl.imatch(m:get(cfg, "local_network")) do
143 subnet = ip.new(subnet)
144 subnets[#subnets+1] = subnet and subnet:string()
149 subnets.write = ifaces.write
152 limit = s:taboption("advanced", Value, "database_limit", translate("Maximum entries"),
153 translate("The maximum amount of entries that should be put into the database, setting the limit to 0 will allow databases to grow indefinitely."))
155 limit.datatype = "uinteger"
156 limit.placeholder = "10000"
158 prealloc = s:taboption("advanced", Flag, "database_prealloc", translate("Preallocate database"),
159 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."))
161 prealloc:depends({["database_limit"] = "0", ["!reverse"] = true })
164 compress = s:taboption("advanced", Flag, "database_compress", translate("Compress database"),
165 translate("Whether to gzip compress archive databases. Compressing the database files makes accessing old data slightly slower but helps to reduce storage requirements."))
167 compress.default = compress.enabled
170 generations = s:taboption("advanced", Value, "database_generations", translate("Stored periods"),
171 translate("Maximum number of accounting periods to keep, use zero to keep databases forever."))
173 generations.datatype = "uinteger"
174 generations.placeholder = "10"
177 commit = s:taboption("advanced", Value, "commit_interval", translate("Commit interval"),
178 translate("Interval at which the temporary in-memory database is committed to the persistent database directory."))
180 commit.placeholder = "24h"
181 commit:value("24h", translate("24h - least flash wear at the expense of data loss risk"))
182 commit:value("12h", translate("12h - compromise between risk of data loss and flash wear"))
183 commit:value("10m", translate("10m - frequent commits at the expense of flash wear"))
184 commit:value("60s", translate("60s - commit minutely, useful for non-flash storage"))
187 refresh = s:taboption("advanced", Value, "refresh_interval", translate("Refresh interval"),
188 translate("Interval at which traffic counters of still established connections are refreshed from netlink information."))
190 refresh.placeholder = "30s"
191 refresh:value("30s", translate("30s - refresh twice per minute for reasonably current stats"))
192 refresh:value("5m", translate("5m - rarely refresh to avoid frequently clearing conntrack counters"))
195 directory = s:taboption("advanced", Value, "database_directory", translate("Database directory"),
196 translate("Database storage directory. One file per accounting period will be placed into this directory."))
198 directory.placeholder = "/var/lib/nlbwmon"
201 protocols = s:taboption("protocol", TextValue, "_protocols")
204 protocols.cfgvalue = function(self, cfg)
205 return fs.readfile("/usr/share/nlbwmon/protocols")
208 protocols.write = function(self, cfg, value)
209 fs.writefile("/usr/share/nlbwmon/protocols", (value or ""):gsub("\r\n", "\n"))
212 protocols.remove = protocols.write