stat-genconfig 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. #!/usr/bin/lua
  2. --[[
  3. Luci statistics - collectd configuration generator
  4. (c) 2008 Freifunk Leipzig / Jo-Philipp Wich <jow@openwrt.org>
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. $Id$
  10. ]]--
  11. require("luci.model.uci")
  12. require("luci.sys.iptparser")
  13. require("luci.util")
  14. local ipt = luci.sys.iptparser.IptParser()
  15. local uci = luci.model.uci.cursor()
  16. local sections = uci:get_all( "luci_statistics" )
  17. function print(...)
  18. nixio.stdout:write(...)
  19. nixio.stdout:write("\n")
  20. end
  21. function section( plugin )
  22. local config = sections[ "collectd_" .. plugin ] or sections["collectd"]
  23. if type(config) == "table" and ( plugin == "collectd" or config.enable == "1" ) then
  24. local params = ""
  25. if type( plugins[plugin] ) == "function" then
  26. params = plugins[plugin]( config )
  27. else
  28. params = config_generic( config, plugins[plugin][1], plugins[plugin][2], plugins[plugin][3], plugin == "collectd" )
  29. end
  30. if plugin ~= "collectd" then
  31. print( "LoadPlugin " .. plugin )
  32. if params:len() > 0 then
  33. print( "<Plugin " .. plugin .. ">\n" .. params .. "</Plugin>\n" )
  34. else
  35. print( "" )
  36. end
  37. else
  38. print( params .. "\n" )
  39. end
  40. end
  41. end
  42. function config_generic( c, singles, bools, lists, nopad )
  43. local str = ""
  44. if type(c) == "table" then
  45. if type(singles) == "table" then
  46. for i, key in ipairs( singles ) do
  47. if preprocess[key] then
  48. c[key] = preprocess[key](c[key])
  49. end
  50. str = str .. _string( c[key], key, nopad )
  51. end
  52. end
  53. if type(bools) == "table" then
  54. for i, key in ipairs( bools ) do
  55. if preprocess[key] then
  56. c[key] = preprocess[key](c[key])
  57. end
  58. str = str .. _bool( c[key], key, nopad )
  59. end
  60. end
  61. if type(lists) == "table" then
  62. str = str .. _list_expand( c, lists, nopad )
  63. end
  64. end
  65. return str
  66. end
  67. function config_exec( c )
  68. local str = ""
  69. for s in pairs(sections) do
  70. for key, type in pairs({ Exec="collectd_exec_input", NotificationExec="collectd_exec_notify" }) do
  71. if sections[s][".type"] == type then
  72. cmd = sections[s].cmdline
  73. if cmd then
  74. cmd = cmd:gsub("^%s+", ""):gsub("%s+$", "")
  75. user = sections[s].cmduser or "nobody"
  76. group = sections[s].cmdgroup
  77. str = str .. "\t" .. key .. ' "' ..
  78. user .. ( group and ":" .. group or "" ) .. '" "' ..
  79. cmd:gsub('%s+', '" "') .. '"\n'
  80. end
  81. end
  82. end
  83. end
  84. return str
  85. end
  86. function config_iptables( c )
  87. local str = ""
  88. for s in pairs(sections) do
  89. if sections[s][".type"] == "collectd_iptables_match" then
  90. search = { }
  91. for i, k in ipairs( {
  92. "table", "chain", "target", "protocol", "source", "destination",
  93. "inputif", "outputif", "options"
  94. } ) do
  95. v = sections[s][k]
  96. if type(v) == "string" then
  97. if k == "options" then v = luci.util.split( v, "%s+", nil, true ) end
  98. search[k] = v
  99. end
  100. end
  101. for i, rule in ipairs( ipt:find( search ) ) do
  102. name = sections[s].name:gsub( "%s+", "_" )
  103. if i > 1 then name = name .. "_(" .. i .. ")" end
  104. str = str .. "\tChain " .. rule.table .. " " .. rule.chain .. " " .. rule.index .. ' "' .. name .. "\"\n"
  105. end
  106. end
  107. end
  108. return str
  109. end
  110. function config_network( c )
  111. local str = ""
  112. for s in pairs(sections) do
  113. for key, type in pairs({ Listen="collectd_network_listen", Server="collectd_network_server" }) do
  114. if sections[s][".type"] == type then
  115. host = sections[s].host
  116. port = sections[s].port
  117. if host then
  118. if port then
  119. str = str .. "\t" .. key .. " \"" .. host .. "\" \"" .. port .. "\"\n"
  120. else
  121. str = str .. "\t" .. key .. " \"" .. host .. "\"\n"
  122. end
  123. end
  124. end
  125. end
  126. end
  127. return str .. _string( c["TimeToLive"], "TimeToLive" )
  128. .. _string( c["CacheFlush"], "CacheFlush" )
  129. .. _bool( c["Forward"], "Forward" )
  130. end
  131. function _list_expand( c, l, nopad )
  132. local str = ""
  133. for i, n in ipairs(l) do
  134. if c[n] then
  135. if preprocess[n] then
  136. c[n] = preprocess[n](c[n])
  137. end
  138. if n:find("(%w+)ses") then
  139. k = n:gsub("(%w+)ses", "%1s")
  140. else
  141. k = n:gsub("(%w+)s", "%1")
  142. end
  143. str = str .. _expand( c[n], k, nopad )
  144. end
  145. end
  146. return str
  147. end
  148. function _expand( s, n, nopad )
  149. local str = ""
  150. if type(s) == "string" then
  151. for i, v in ipairs( luci.util.split( s, "%s+", nil, true ) ) do
  152. str = str .. _string( v, n, nopad )
  153. end
  154. elseif type(s) == "table" then
  155. for i, v in ipairs(s) do
  156. str = str .. _string( v, n, nopad )
  157. end
  158. end
  159. return str
  160. end
  161. function _bool( s, n, nopad )
  162. local str = ""
  163. local pad = ""
  164. if not nopad then pad = "\t" end
  165. if s and s == "1" then
  166. str = pad .. n .. " true"
  167. else
  168. str = pad .. n .. " false"
  169. end
  170. return str .. "\n"
  171. end
  172. function _string( s, n, nopad )
  173. local str = ""
  174. local pad = ""
  175. if not nopad then pad = "\t" end
  176. if s then
  177. if s:find("[^%d]") or n == "Port" then
  178. if not s:find("[^%w]") and n ~= "Port" then
  179. str = pad .. n .. " " .. luci.util.trim(s)
  180. else
  181. str = pad .. n .. ' "' .. luci.util.trim(s) .. '"'
  182. end
  183. else
  184. str = pad .. n .. " " .. luci.util.trim(s)
  185. end
  186. str = str .. "\n"
  187. end
  188. return str
  189. end
  190. plugins = {
  191. collectd = {
  192. { "BaseDir", "Include", "PIDFile", "PluginDir", "TypesDB", "Interval", "ReadThreads", "Hostname" },
  193. { },
  194. { }
  195. },
  196. conntrack = {
  197. { },
  198. { },
  199. { }
  200. },
  201. cpu = {
  202. { },
  203. { },
  204. { }
  205. },
  206. csv = {
  207. { "DataDir" },
  208. { "StoreRates" },
  209. { }
  210. },
  211. df = {
  212. { },
  213. { "IgnoreSelected" },
  214. { "Devices", "MountPoints", "FSTypes" }
  215. },
  216. disk = {
  217. { },
  218. { "IgnoreSelected" },
  219. { "Disks" }
  220. },
  221. dns = {
  222. { },
  223. { },
  224. { "Interfaces", "IgnoreSources" }
  225. },
  226. email = {
  227. { "SocketFile", "SocketGroup", "SocketPerms", "MaxConns" },
  228. { },
  229. { }
  230. },
  231. entropy = {
  232. { },
  233. { },
  234. { }
  235. },
  236. exec = config_exec,
  237. interface = {
  238. { },
  239. { "IgnoreSelected" },
  240. { "Interfaces" }
  241. },
  242. iptables = config_iptables,
  243. irq = {
  244. { },
  245. { "IgnoreSelected" },
  246. { "Irqs" }
  247. },
  248. iwinfo = {
  249. { },
  250. { "IgnoreSelected" },
  251. { "Interfaces" }
  252. },
  253. load = {
  254. { },
  255. { },
  256. { }
  257. },
  258. logfile = {
  259. { "LogLevel", "File" },
  260. { "Timestamp" },
  261. { }
  262. },
  263. madwifi = {
  264. { "WatchSet" },
  265. { },
  266. { "Interfaces", "WatchAdds" }
  267. },
  268. memory = {
  269. { },
  270. { },
  271. { }
  272. },
  273. netlink = {
  274. { },
  275. { "IgnoreSelected" },
  276. { "Interfaces", "VerboseInterfaces", "QDiscs", "Classes", "Filters" }
  277. },
  278. network = config_network,
  279. nut = {
  280. { "UPS" },
  281. { },
  282. { }
  283. },
  284. olsrd = {
  285. { "Host", "Port", "CollectLinks","CollectRoutes","CollectTopology"},
  286. { },
  287. { }
  288. },
  289. ping = {
  290. { "TTL", "Interval" },
  291. { },
  292. { "Hosts" }
  293. },
  294. processes = {
  295. { },
  296. { },
  297. { "Processes" }
  298. },
  299. rrdtool = {
  300. { "DataDir", "StepSize", "HeartBeat", "RRARows", "XFF", "CacheFlush", "CacheTimeout" },
  301. { "RRASingle" },
  302. { "RRATimespans" }
  303. },
  304. splash_leases = {
  305. { },
  306. { },
  307. { }
  308. },
  309. tcpconns = {
  310. { },
  311. { "ListeningPorts" },
  312. { "LocalPorts", "RemotePorts" }
  313. },
  314. unixsock = {
  315. { "SocketFile", "SocketGroup", "SocketPerms" },
  316. { },
  317. { }
  318. },
  319. uptime = {
  320. { },
  321. { },
  322. { }
  323. },
  324. wireless = {
  325. { },
  326. { },
  327. { }
  328. },
  329. }
  330. preprocess = {
  331. RRATimespans = function(val)
  332. local rv = { }
  333. for time in val:gmatch("[^%s]+") do
  334. table.insert( rv, luci.util.parse_units(time) )
  335. end
  336. return table.concat(rv, " ")
  337. end
  338. }
  339. section("collectd")
  340. for plugin in pairs(plugins) do
  341. if plugin ~= "collectd" then
  342. section( plugin )
  343. end
  344. end