Translated using Weblate (Greek)
[project/luci.git] / applications / luci-app-statistics / root / usr / bin / stat-genconfig
1 #!/usr/bin/lua
2
3 --[[
4
5 Luci statistics - collectd configuration generator
6 (c) 2008 Freifunk Leipzig / Jo-Philipp Wich <jow@openwrt.org>
7
8 Licensed under the Apache License, Version 2.0 (the "License");
9 you may not use this file except in compliance with the License.
10 You may obtain a copy of the License at
11
12 http://www.apache.org/licenses/LICENSE-2.0
13
14 $Id$
15
16 ]]--
17
18
19 require("luci.model.uci")
20 require("luci.util")
21 require("luci.i18n")
22 require("luci.jsonc")
23 require("nixio.fs")
24
25 local uci = luci.model.uci.cursor()
26 local sections = uci:get_all( "luci_statistics" )
27
28
29 function print(...)
30 nixio.stdout:write(...)
31 nixio.stdout:write("\n")
32 end
33
34 function section( plugin )
35
36 local config = sections[ "collectd_" .. plugin ] or sections["collectd"]
37
38 if type(config) == "table" and ( plugin == "collectd" or config.enable == "1" ) then
39
40 local params = ""
41
42 if type( plugins[plugin] ) == "function" then
43 params = plugins[plugin]( config )
44 else
45 params = config_generic( config, plugins[plugin][1], plugins[plugin][2], plugins[plugin][3], plugin == "collectd" )
46 end
47
48
49 if plugin ~= "collectd" then
50 print( "LoadPlugin " .. plugin )
51
52 if params:len() > 0 then
53 print( "<Plugin " .. plugin .. ">\n" .. params .. "</Plugin>\n" )
54 else
55 print( "" )
56 end
57 else
58 print( params .. "\n" )
59 end
60 end
61 end
62
63 function config_generic( c, singles, bools, lists, nopad )
64 local str = ""
65
66 if type(c) == "table" then
67
68 if type(singles) == "table" then
69 for i, key in ipairs( singles ) do
70 if preprocess[key] then
71 c[key] = preprocess[key](c[key])
72 end
73
74 str = str .. _string( c[key], key, nopad )
75 end
76 end
77
78 if type(bools) == "table" then
79 for i, key in ipairs( bools ) do
80 if preprocess[key] then
81 c[key] = preprocess[key](c[key])
82 end
83
84 str = str .. _bool( c[key], key, nopad )
85 end
86 end
87
88 if type(lists) == "table" then
89 str = str .. _list_expand( c, lists, nopad )
90 end
91 end
92
93 return str
94 end
95
96 function config_exec( c )
97 local str = ""
98
99 for s in pairs(sections) do
100 for key, type in pairs({ Exec="collectd_exec_input", NotificationExec="collectd_exec_notify" }) do
101 if sections[s][".type"] == type then
102
103 cmd = sections[s].cmdline
104
105 if cmd then
106 cmd = cmd:gsub("^%s+", ""):gsub("%s+$", "")
107 user = sections[s].cmduser or "nobody"
108 group = sections[s].cmdgroup
109
110 str = str .. "\t" .. key .. ' "' ..
111 user .. ( group and ":" .. group or "" ) .. '" "' ..
112 cmd:gsub('%s+', '" "') .. '"\n'
113 end
114 end
115 end
116 end
117
118 return str
119 end
120
121 function config_curl( c )
122 local str = ""
123
124 for s in pairs(sections) do
125 if sections[s][".type"] == "collectd_curl_page" then
126 str = str .. "\t<Page \"" .. sections[s].name .. "\">\n" ..
127 "\t\tURL \"" .. sections[s].url .. "\"\n" ..
128 "\t\tMeasureResponseTime true\n" ..
129 "\t</Page>\n"
130 end
131 end
132
133 return str
134 end
135
136 function config_iptables( c )
137 local str = ""
138
139 for id, s in pairs(sections) do
140 if s[".type"] == "collectd_iptables_match" or s[".type"] == "collectd_iptables_match6" then
141 local tname = s.table and tostring(s.table)
142 local chain = s.chain and tostring(s.chain)
143
144 if tname and tname:match("^%S+$") and chain and chain:match("^%S+$") then
145 local line = { #s[".type"] > 23 and "\tChain6" or "\tChain", tname, chain }
146 local rule = s.rule and tostring(s.rule)
147
148 if rule and rule:match("^%S+$") then
149 line[#line+1] = rule
150
151 local name = s.name and tostring(s.name)
152 if name and name:match("^%S+$") then
153 line[#line+1] = name
154 end
155 end
156
157 str = str .. table.concat(line, " ") .. "\n"
158 end
159 end
160 end
161
162 return str
163 end
164
165 function config_network( c )
166 local str = ""
167
168 for s in pairs(sections) do
169 for key, type in pairs({ Listen="collectd_network_listen", Server="collectd_network_server" }) do
170 if sections[s][".type"] == type then
171
172 host = sections[s].host
173 port = sections[s].port
174
175 if host then
176 if port then
177 str = str .. "\t" .. key .. " \"" .. host .. "\" \"" .. port .. "\"\n"
178 else
179 str = str .. "\t" .. key .. " \"" .. host .. "\"\n"
180 end
181 end
182 end
183 end
184 end
185
186 return str ..
187 _string(c["MaxPacketSize"], "MaxPacketSize") ..
188 _string(c["TimeToLive"], "TimeToLive") ..
189 _bool(c["Forward"], "Forward") ..
190 _bool(c["ReportStats"], "ReportStats")
191 end
192
193
194 function _list_expand( c, l, nopad )
195 local str = ""
196
197 for i, n in ipairs(l) do
198 if c[n] then
199 if preprocess[n] then
200 c[n] = preprocess[n](c[n])
201 end
202
203 if n:find("(%w+)ses") then
204 k = n:gsub("(%w+)ses$", "%1s")
205 else
206 k = n:gsub("(%w+)s$", "%1")
207 end
208
209 str = str .. _expand( c[n], k, nopad )
210 end
211 end
212
213 return str
214 end
215
216 function _expand( s, n, nopad )
217 local str = ""
218
219 if type(s) == "string" then
220 for i, v in ipairs( luci.util.split( s, "%s+", nil, true ) ) do
221 str = str .. _string( v, n, nopad )
222 end
223 elseif type(s) == "table" then
224 for i, v in ipairs(s) do
225 str = str .. _string( v, n, nopad )
226 end
227 end
228
229 return str
230 end
231
232 function _bool( s, n, nopad )
233
234 local str = ""
235 local pad = ""
236 if not nopad then pad = "\t" end
237
238 if s and s == "1" then
239 str = pad .. n .. " true"
240 else
241 str = pad .. n .. " false"
242 end
243
244 return str .. "\n"
245 end
246
247 function _string( s, n, nopad )
248
249 local str = ""
250 local pad = ""
251 if not nopad then pad = "\t" end
252
253 if s then
254 if s:find("[^%d]") or n == "Port" then
255 if not s:find("[^%w]") and n ~= "Port" then
256 str = pad .. n .. " " .. luci.util.trim(s)
257 else
258 str = pad .. n .. ' "' .. luci.util.trim(s) .. '"'
259 end
260 else
261 str = pad .. n .. " " .. luci.util.trim(s)
262 end
263
264 str = str .. "\n"
265 end
266
267 return str
268 end
269
270
271 plugins = {
272 collectd = {
273 { "BaseDir", "Include", "PIDFile", "PluginDir", "TypesDB", "Interval", "ReadThreads", "Hostname" },
274 { },
275 { }
276 },
277 logfile = {
278 { "LogLevel", "File" },
279 { "Timestamp" },
280 { }
281 },
282 }
283
284 local plugin_dir = "/usr/share/luci/statistics/plugins/"
285 for filename in nixio.fs.dir(plugin_dir) do
286 local name = filename:gsub("%.json", "")
287 if (name == "exec") then
288 plugins[name] = config_exec
289 elseif (name == "iptables") then
290 plugins[name] = config_iptables
291 elseif (name == "curl") then
292 plugins[name] = config_curl
293 elseif (name == "network") then
294 plugins[name] = config_network
295 else
296 local plugin_def = luci.jsonc.parse(nixio.fs.readfile(plugin_dir .. filename))
297 if type(plugin_def) == "table" then
298 plugins[name] = plugin_def.legend
299 end
300 end
301 end
302
303
304 preprocess = {
305 RRATimespans = function(val)
306 local rv = { }
307 for time in luci.util.imatch(val) do
308 table.insert( rv, luci.util.parse_units(time) )
309 end
310 return table.concat(rv, " ")
311 end
312 }
313
314
315 section("collectd")
316
317 section("logfile")
318
319 for plugin in pairs(plugins) do
320 if (plugin ~= "collectd") and (plugin ~= "logfile") then
321 section( plugin )
322 end
323 end