* ffluci/statistics: added initial config generator for collectd
[project/luci.git] / applications / luci-statistics / files / 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 <xm@leipzig.freifunk.net>
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("ffluci.model.uci")
20 require("ffluci.sys.iptparser")
21 require("ffluci.util")
22
23 local ipt = ffluci.sys.iptparser.IptParser()
24 local uci = ffluci.model.uci.Session()
25 local sections, names = uci:sections( "luci_statistics" )
26
27
28 function section( plugin )
29
30         local config = sections[ "collectd_" .. plugin ]
31
32         if type(config) == "table" and config.enable == "1" then
33
34                 print( "<Plugin " .. plugin .. ">" )
35
36                 if type( plugins[plugin] ) == "function" then
37                         plugins[plugin]( config )
38                 else
39                         config_generic( config, plugins[plugin][1], plugins[plugin][2], plugins[plugin][3] )
40                 end
41
42                 print( "</Plugin>\n" )
43
44         end
45 end
46
47 function config_generic( c, singles, bools, lists )
48
49         if type(c) == "table" then
50
51                 if type(singles) == "table" then
52                         for i, key in ipairs( singles ) do
53                                 if c[key] then
54                                         print( "\t" .. key .. ' "' .. c[key] .. '"' )
55                                 end
56                         end
57                 end
58
59                 if type(bools) == "table" then
60                         for i, key in ipairs( bools ) do
61                                 if c[key] == 1 then
62                                         print( "\t" .. key .. " true" )
63                                 else
64                                         print( "\t" .. key .. " false" )
65                                 end
66                         end
67                 end
68
69                 if type(lists) == "table" then
70                         _list_expand( c, lists )
71                 end
72
73         end
74
75 end
76
77 function config_exec( c )
78
79         for s in pairs(sections) do
80                 for key, type in pairs({ Exec="collectd_exec_input", NotificationExec="collectd_exec_notify" }) do
81                         if sections[s][".type"] == type then
82
83                                 cmd   = sections[s].cmdline
84                                 user  = sections[s].cmduser  or "root"
85                                 group = sections[s].cmdgroup or "root"
86
87                                 print( "\t" .. key .. " " .. user .. ":" .. group .. ' "' .. cmd .. '"' )
88                         end
89                 end
90         end
91 end
92
93 function config_iptables( c )
94
95         for s in pairs(sections) do
96                 if sections[s][".type"] == "collectd_iptables_match" then
97
98                         search = { }
99
100                         for i, k in ipairs( {
101                                 "table", "chain", "target", "protocol", "source", "destination",
102                                 "inputif", "outputif", "options"
103                         } ) do
104                                 v = sections[s][k]
105
106                                 if type(v) == "string" then
107                                         if k == "options" then v = ffluci.util.split( v, "%s+", nil, true ) end
108                                         search[k] = v
109                                 end
110                         end
111
112                         for i, rule in ipairs( ipt:find( search ) ) do
113
114                                 name = sections[s].name
115                                 if i > 1 then name = name .. " (" .. i .. ")" end
116
117                                 print( "\tChain " .. rule.table .. " " .. rule.chain .. " " .. rule.index .. ' "' .. name .. '"' )
118
119                         end
120                 end
121         end
122
123 end
124
125 function _list_expand( c, l )
126         for i, n in ipairs(l) do
127                 if c[n] then
128                         _expand( c[n], n:gsub( "(%w+)s", "%1" ) )
129                 end
130         end
131 end
132
133 function _expand( s, n )
134
135         if type(s) == "string" then
136                 for i, v in ipairs( ffluci.util.split( s, "%s+", nil, true ) ) do
137                         print( "\t" .. n .. ' "' .. v .. '"' )
138                 end
139         end
140 end
141
142
143
144 plugins = {
145         csv     = { 
146                 { "DataDir" },
147                 { "StoreRates" },
148                 { }
149         },
150
151         df      = {
152                 { },
153                 { "IgnoreSelected" },
154                 { "Devices", "MountPoints", "FSTypes" }
155         },
156
157         disk    = {
158                 { },
159                 { "IgnoreSelected" },
160                 { "Disks" }
161         },
162
163         dns     = {
164                 { },
165                 { },
166                 { "Interfaces", "IgnoreSources" }
167         },
168
169         email   = {
170                 { "SocketFile", "SocketUser", "SocketPerms", "MaxConns" },
171                 { },
172                 { }
173         },
174
175         exec    = config_exec,
176
177         interface = {
178                 { },
179                 { "IgnoreSelected" },
180                 { "Interfaces" }
181         },
182
183         iptables = config_iptables,
184
185 }
186
187
188 for plugin in pairs(plugins) do
189
190         section( plugin )        
191 end