* ffluci/statistics: add cbi models for irq, netlink and network plugins, updated...
[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                                 _string( c[key], key )
54                         end
55                 end
56
57                 if type(bools) == "table" then
58                         for i, key in ipairs( bools ) do
59                                 _bool( c[key], key )
60                         end
61                 end
62
63                 if type(lists) == "table" then
64                         _list_expand( c, lists )
65                 end
66
67         end
68
69 end
70
71 function config_exec( c )
72
73         for s in pairs(sections) do
74                 for key, type in pairs({ Exec="collectd_exec_input", NotificationExec="collectd_exec_notify" }) do
75                         if sections[s][".type"] == type then
76
77                                 cmd   = sections[s].cmdline
78                                 user  = sections[s].cmduser  or "root"
79                                 group = sections[s].cmdgroup or "root"
80
81                                 print( "\t" .. key .. " " .. user .. ":" .. group .. ' "' .. cmd .. '"' )
82                         end
83                 end
84         end
85 end
86
87 function config_iptables( c )
88
89         for s in pairs(sections) do
90                 if sections[s][".type"] == "collectd_iptables_match" then
91
92                         search = { }
93
94                         for i, k in ipairs( {
95                                 "table", "chain", "target", "protocol", "source", "destination",
96                                 "inputif", "outputif", "options"
97                         } ) do
98                                 v = sections[s][k]
99
100                                 if type(v) == "string" then
101                                         if k == "options" then v = ffluci.util.split( v, "%s+", nil, true ) end
102                                         search[k] = v
103                                 end
104                         end
105
106                         for i, rule in ipairs( ipt:find( search ) ) do
107
108                                 name = sections[s].name
109                                 if i > 1 then name = name .. " (" .. i .. ")" end
110
111                                 print( "\tChain " .. rule.table .. " " .. rule.chain .. " " .. rule.index .. ' "' .. name .. '"' )
112
113                         end
114                 end
115         end
116 end
117
118 function config_network( c )
119
120         for s in pairs(sections) do
121                 for key, type in pairs({ Listen="collectd_network_listen", Server="collectd_network_server" }) do
122                         if sections[s][".type"] == type then
123
124                                 host = sections[s].host
125                                 port = sections[s].port
126
127                                 if host then
128                                         if port then
129                                                 print( "\t" .. key .. " " .. host .. " " .. port )
130                                         else
131                                                 print( "\t" .. key .. " " .. host )
132                                         end
133                                 end
134                         end
135                 end
136         end
137
138         _string( c["TimeToLive"], "TimeToLive" )
139         _string( c["CacheFlush"], "CacheFlush" )
140         _bool(   c["Forward"],    "Forward"    )
141 end
142
143
144 function _list_expand( c, l )
145         for i, n in ipairs(l) do
146                 if c[n] then
147                         _expand( c[n], n:gsub( "(%w+)s", "%1" ) )
148                 end
149         end
150 end
151
152 function _expand( s, n )
153         if type(s) == "string" then
154                 for i, v in ipairs( ffluci.util.split( s, "%s+", nil, true ) ) do
155                         _string( v, n )
156                 end
157         end
158 end
159
160 function _bool( s, n )
161         if s and s == "1" then
162                 print( "\t" .. n .. " true" )
163         else
164                 print( "\t" .. n .. " false" )
165         end
166 end
167
168 function _string( s, n )
169         if s then
170                 if not s:find("%d") then
171                         if not s:find("%s") then
172                                 print( "\t" .. n .. " " .. s )
173                         else
174                                 print( "\t" .. n .. ' "' .. s '"' )
175                         end
176                 else
177                         print( "\t" .. n .. " " .. s )
178                 end
179         end
180 end             
181
182
183 plugins = {
184         csv     = { 
185                 { "DataDir" },
186                 { "StoreRates" },
187                 { }
188         },
189
190         df      = {
191                 { },
192                 { "IgnoreSelected" },
193                 { "Devices", "MountPoints", "FSTypes" }
194         },
195
196         disk    = {
197                 { },
198                 { "IgnoreSelected" },
199                 { "Disks" }
200         },
201
202         dns     = {
203                 { },
204                 { },
205                 { "Interfaces", "IgnoreSources" }
206         },
207
208         email   = {
209                 { "SocketFile", "SocketUser", "SocketPerms", "MaxConns" },
210                 { },
211                 { }
212         },
213
214         exec    = config_exec,
215
216         interface = {
217                 { },
218                 { "IgnoreSelected" },
219                 { "Interfaces" }
220         },
221
222         iptables = config_iptables,
223
224         irq     = {
225                 { },
226                 { "IgnoreSelected" },
227                 { "Irqs" }
228         },
229
230         logfile = {
231                 { "LogLevel", "File" },
232                 { "Timestamp" },
233                 { }
234         },
235
236         netlink = {
237                 { },
238                 { "IgnoreSelected" },
239                 { "Interfaces", "VerboseInterfaces", "QDiscs", "Classs", "Filters" }
240         },
241
242         network = config_network,
243
244 }
245
246
247 for plugin in pairs(plugins) do
248
249         section( plugin )        
250 end