summaryrefslogtreecommitdiffhomepage
path: root/applications/luci-statistics/files/usr/bin/stat-genconfig
blob: 4f7aa6eb0f83de9582760fca287a4c1bc9978bbf (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
#!/usr/bin/lua

--[[

Luci statistics - collectd configuration generator
(c) 2008 Freifunk Leipzig / Jo-Philipp Wich <xm@leipzig.freifunk.net>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

$Id$

]]--


require("ffluci.model.uci")
require("ffluci.sys.iptparser")
require("ffluci.util")

local ipt = ffluci.sys.iptparser.IptParser()
local uci = ffluci.model.uci.Session()
local sections, names = uci:sections( "luci_statistics" )


function section( plugin )

	local config = sections[ "collectd_" .. plugin ]

	if type(config) == "table" and config.enable == "1" then

	        print( "<Plugin " .. plugin .. ">" )

		if type( plugins[plugin] ) == "function" then
			plugins[plugin]( config )
		else
			config_generic( config, plugins[plugin][1], plugins[plugin][2], plugins[plugin][3] )
		end

	        print( "</Plugin>\n" )

	end
end

function config_generic( c, singles, bools, lists )

	if type(c) == "table" then

		if type(singles) == "table" then
			for i, key in ipairs( singles ) do
				if c[key] then
					print( "\t" .. key .. ' "' .. c[key] .. '"' )
				end
			end
		end

		if type(bools) == "table" then
			for i, key in ipairs( bools ) do
				if c[key] == 1 then
					print( "\t" .. key .. " true" )
				else
					print( "\t" .. key .. " false" )
				end
			end
		end

		if type(lists) == "table" then
			_list_expand( c, lists )
		end

	end

end

function config_exec( c )

	for s in pairs(sections) do
		for key, type in pairs({ Exec="collectd_exec_input", NotificationExec="collectd_exec_notify" }) do
			if sections[s][".type"] == type then

				cmd   = sections[s].cmdline
				user  = sections[s].cmduser  or "root"
				group = sections[s].cmdgroup or "root"

				print( "\t" .. key .. " " .. user .. ":" .. group .. ' "' .. cmd .. '"' )
			end
		end
	end
end

function config_iptables( c )

	for s in pairs(sections) do
		if sections[s][".type"] == "collectd_iptables_match" then

			search = { }

			for i, k in ipairs( {
				"table", "chain", "target", "protocol", "source", "destination",
				"inputif", "outputif", "options"
			} ) do
				v = sections[s][k]

				if type(v) == "string" then
					if k == "options" then v = ffluci.util.split( v, "%s+", nil, true ) end
					search[k] = v
				end
			end

			for i, rule in ipairs( ipt:find( search ) ) do

				name = sections[s].name
				if i > 1 then name = name .. " (" .. i .. ")" end

				print( "\tChain " .. rule.table .. " " .. rule.chain .. " " .. rule.index .. ' "' .. name .. '"' )

			end
		end
	end

end

function _list_expand( c, l )
	for i, n in ipairs(l) do
		if c[n] then
			_expand( c[n], n:gsub( "(%w+)s", "%1" ) )
		end
	end
end

function _expand( s, n )

	if type(s) == "string" then
		for i, v in ipairs( ffluci.util.split( s, "%s+", nil, true ) ) do
			print( "\t" .. n .. ' "' .. v .. '"' )
		end
	end
end



plugins = {
	csv	= { 
		{ "DataDir" },
		{ "StoreRates" },
		{ }
	},

	df	= {
		{ },
		{ "IgnoreSelected" },
		{ "Devices", "MountPoints", "FSTypes" }
	},

	disk	= {
		{ },
		{ "IgnoreSelected" },
		{ "Disks" }
	},

	dns	= {
		{ },
		{ },
		{ "Interfaces", "IgnoreSources" }
	},

	email	= {
		{ "SocketFile", "SocketUser", "SocketPerms", "MaxConns" },
		{ },
		{ }
	},

	exec	= config_exec,

	interface = {
		{ },
		{ "IgnoreSelected" },
		{ "Interfaces" }
	},

	iptables = config_iptables,

}


for plugin in pairs(plugins) do

	section( plugin )        
end