summaryrefslogtreecommitdiffhomepage
path: root/applications/luci-app-nlbwmon/luasrc/model/cbi/nlbw/config.lua
blob: 08df5b332e7dde8e9f558491c02f03fcfe9c9548 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
-- Copyright 2017 Jo-Philipp Wich <jo@mein.io>
-- Licensed to the public under the Apache License 2.0.

local utl = require "luci.util"
local sys = require "luci.sys"
local fs  = require "nixio.fs"
local ip  = require "luci.ip"
local nw  = require "luci.model.network"

local s, m, period, warning, date, days, interval, ifaces, subnets, limit, prealloc, compress, generations, commit, refresh, directory, protocols

m = Map("nlbwmon", translate("Netlink Bandwidth Monitor - Configuration"),
	translate("The Netlink Bandwidth Monitor (nlbwmon) is a lightweight, efficient traffic accounting program keeping track of bandwidth usage per host and protocol."))

nw.init(luci.model.uci.cursor_state())

s = m:section(TypedSection, "nlbwmon")
s.anonymous = true
s.addremove = false
s:tab("general", translate("General Settings"))
s:tab("advanced", translate("Advanced Settings"))
s:tab("protocol", translate("Protocol Mapping"),
	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."))

period = s:taboption("general", ListValue, "_period", translate("Accounting period"),
	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."))

period:value("relative", translate("Day of month"))
period:value("absolute", translate("Fixed interval"))

period.write = function(self, cfg, val)
	if period:formvalue(cfg) == "relative" then
		m:set(cfg, "database_interval", interval:formvalue(cfg))
	else
		m:set(cfg, "database_interval", "%s/%s" %{
			date:formvalue(cfg),
			days:formvalue(cfg)
		})
	end
end

period.cfgvalue = function(self, cfg)
	local val = m:get(cfg, "database_interval") or ""
	if val:match("^%d%d%d%d%-%d%d%-%d%d/%d+$") then
		return "absolute"
	end
	return "relative"
end


warning = s:taboption("general", DummyValue, "_warning", translate("Warning"))
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"))
warning.rawhtml = true

if (m.uci:get_first("nlbwmon", "nlbwmon", "database_interval") or ""):match("^%d%d%d%d-%d%d-%d%d/%d+$") then
	warning:depends("_period", "relative")
else
	warning:depends("_period", "absolute")
end


interval = s:taboption("general", Value, "_interval", translate("Due date"),
	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."))

interval.datatype = "or(range(1,31),range(-31,-1))"
interval.placeholder = "1"
interval:value("1", translate("1 - Restart every 1st of month"))
interval:value("-1", translate("-1 - Restart every last day of month"))
interval:value("-7", translate("-7 - Restart a week before end of month"))
interval.rmempty = false
interval:depends("_period", "relative")
interval.write = period.write

interval.cfgvalue = function(self, cfg)
	local val = tonumber(m:get(cfg, "database_interval"))
	return val and tostring(val)
end


date = s:taboption("general", Value, "_date", translate("Start date"),
	translate("Start date of the first accounting period, e.g. begin of ISP contract."))

date.datatype = "dateyyyymmdd"
date.placeholder = "2016-03-15"
date.rmempty = false
date:depends("_period", "absolute")
date.write = period.write

date.cfgvalue = function(self, cfg)
	local val = m:get(cfg, "database_interval") or ""
	return (val:match("^(%d%d%d%d%-%d%d%-%d%d)/%d+$"))
end


days = s:taboption("general", Value, "_days", translate("Interval"),
	translate("Length of accounting interval in days."))

days.datatype = "min(1)"
days.placeholder = "30"
days.rmempty = false
days:depends("_period", "absolute")
days.write = period.write

days.cfgvalue = function(self, cfg)
	local val = m:get(cfg, "database_interval") or ""
	return (val:match("^%d%d%d%d%-%d%d%-%d%d/(%d+)$"))
end


ifaces = s:taboption("general", Value, "_ifaces", translate("Local interfaces"),
	translate("Only conntrack streams from or to any of these networks are counted."))

ifaces.template = "cbi/network_netlist"
ifaces.widget = "checkbox"
ifaces.nocreate = true

ifaces.cfgvalue = function(self, cfg)
	return m:get(cfg, "local_network")
end

ifaces.write = function(self, cfg)
	local item
	local items = {}
	for item in utl.imatch(subnets:formvalue(cfg)) do
		items[#items+1] = item
	end
	for item in utl.imatch(ifaces:formvalue(cfg)) do
		items[#items+1] = item
	end
	m:set(cfg, "local_network", items)
end


subnets = s:taboption("general", DynamicList, "_subnets", translate("Local subnets"),
	translate("Only conntrack streams from or to any of these subnets are counted."))

subnets.datatype = "ipaddr"

subnets.cfgvalue = function(self, cfg)
	local subnet
	local subnets = {}
	for subnet in utl.imatch(m:get(cfg, "local_network")) do
		subnet = ip.new(subnet)
		subnets[#subnets+1] = subnet and subnet:string()
	end
	return subnets
end

subnets.write = ifaces.write


limit = s:taboption("advanced", Value, "database_limit", translate("Maximum entries"),
	translate("The maximum amount of entries that should be put into the database, setting the limit to 0 will allow databases to grow indefinitely."))

limit.datatype = "uinteger"
limit.placeholder = "10000"

prealloc = s:taboption("advanced", Flag, "database_prealloc", translate("Preallocate database"),
	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."))

prealloc:depends({["database_limit"] = "0", ["!reverse"] = true })


compress = s:taboption("advanced", Flag, "database_compress", translate("Compress database"),
	translate("Whether to gzip compress archive databases. Compressing the database files makes accessing old data slightly slower but helps to reduce storage requirements."))

compress.default = compress.enabled


generations = s:taboption("advanced", Value, "database_generations", translate("Stored periods"),
	translate("Maximum number of accounting periods to keep, use zero to keep databases forever."))

generations.datatype = "uinteger"
generations.placeholder = "10"


commit = s:taboption("advanced", Value, "commit_interval", translate("Commit interval"),
	translate("Interval at which the temporary in-memory database is committed to the persistent database directory."))

commit.placeholder = "24h"
commit:value("24h", translate("24h - least flash wear at the expense of data loss risk"))
commit:value("12h", translate("12h - compromise between risk of data loss and flash wear"))
commit:value("10m", translate("10m - frequent commits at the expense of flash wear"))
commit:value("60s", translate("60s - commit minutely, useful for non-flash storage"))


refresh = s:taboption("advanced", Value, "refresh_interval", translate("Refresh interval"),
	translate("Interval at which traffic counters of still established connections are refreshed from netlink information."))

refresh.placeholder = "30s"
refresh:value("30s", translate("30s - refresh twice per minute for reasonably current stats"))
refresh:value("5m", translate("5m - rarely refresh to avoid frequently clearing conntrack counters"))


directory = s:taboption("advanced", Value, "database_directory", translate("Database directory"),
	translate("Database storage directory. One file per accounting period will be placed into this directory."))

directory.placeholder = "/var/lib/nlbwmon"


protocols = s:taboption("protocol", TextValue, "_protocols")
protocols.rows = 50

protocols.cfgvalue = function(self, cfg)
	return fs.readfile("/usr/share/nlbwmon/protocols")
end

protocols.write = function(self, cfg, value)
	fs.writefile("/usr/share/nlbwmon/protocols", (value or ""):gsub("\r\n", "\n"))
end

protocols.remove = protocols.write


return m