stat-genconfig 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. require("luci.i18n")
  15. require("nixio.fs")
  16. local ipt = luci.sys.iptparser.IptParser()
  17. local uci = luci.model.uci.cursor()
  18. local sections = uci:get_all( "luci_statistics" )
  19. function print(...)
  20. nixio.stdout:write(...)
  21. nixio.stdout:write("\n")
  22. end
  23. function section( plugin )
  24. local config = sections[ "collectd_" .. plugin ] or sections["collectd"]
  25. if type(config) == "table" and ( plugin == "collectd" or config.enable == "1" ) then
  26. local params = ""
  27. if type( plugins[plugin] ) == "function" then
  28. params = plugins[plugin]( config )
  29. else
  30. params = config_generic( config, plugins[plugin][1], plugins[plugin][2], plugins[plugin][3], plugin == "collectd" )
  31. end
  32. if plugin ~= "collectd" then
  33. print( "LoadPlugin " .. plugin )
  34. if params:len() > 0 then
  35. print( "<Plugin " .. plugin .. ">\n" .. params .. "</Plugin>\n" )
  36. else
  37. print( "" )
  38. end
  39. else
  40. print( params .. "\n" )
  41. end
  42. end
  43. end
  44. function config_generic( c, singles, bools, lists, nopad )
  45. local str = ""
  46. if type(c) == "table" then
  47. if type(singles) == "table" then
  48. for i, key in ipairs( singles ) do
  49. if preprocess[key] then
  50. c[key] = preprocess[key](c[key])
  51. end
  52. str = str .. _string( c[key], key, nopad )
  53. end
  54. end
  55. if type(bools) == "table" then
  56. for i, key in ipairs( bools ) do
  57. if preprocess[key] then
  58. c[key] = preprocess[key](c[key])
  59. end
  60. str = str .. _bool( c[key], key, nopad )
  61. end
  62. end
  63. if type(lists) == "table" then
  64. str = str .. _list_expand( c, lists, nopad )
  65. end
  66. end
  67. return str
  68. end
  69. function config_exec( c )
  70. local str = ""
  71. for s in pairs(sections) do
  72. for key, type in pairs({ Exec="collectd_exec_input", NotificationExec="collectd_exec_notify" }) do
  73. if sections[s][".type"] == type then
  74. cmd = sections[s].cmdline
  75. if cmd then
  76. cmd = cmd:gsub("^%s+", ""):gsub("%s+$", "")
  77. user = sections[s].cmduser or "nobody"
  78. group = sections[s].cmdgroup
  79. str = str .. "\t" .. key .. ' "' ..
  80. user .. ( group and ":" .. group or "" ) .. '" "' ..
  81. cmd:gsub('%s+', '" "') .. '"\n'
  82. end
  83. end
  84. end
  85. end
  86. return str
  87. end
  88. function config_curl( c )
  89. local str = ""
  90. for s in pairs(sections) do
  91. if sections[s][".type"] == "collectd_curl_page" then
  92. str = str .. "\t<Page \"" .. sections[s].name .. "\">\n" ..
  93. "\t\tURL \"" .. sections[s].url .. "\"\n" ..
  94. "\t\tMeasureResponseTime true\n" ..
  95. "\t</Page>\n"
  96. end
  97. end
  98. return str
  99. end
  100. function config_iptables( c )
  101. local str = ""
  102. for s in pairs(sections) do
  103. if sections[s][".type"] == "collectd_iptables_match" then
  104. search = { }
  105. for i, k in ipairs( {
  106. "table", "chain", "target", "protocol", "source", "destination",
  107. "inputif", "outputif", "options"
  108. } ) do
  109. v = sections[s][k]
  110. if type(v) == "string" then
  111. if k == "options" then v = luci.util.split( v, "%s+", nil, true ) end
  112. search[k] = v
  113. end
  114. end
  115. for i, rule in ipairs( ipt:find( search ) ) do
  116. name = sections[s].name:gsub( "%s+", "_" )
  117. if i > 1 then name = name .. "_(" .. i .. ")" end
  118. str = str .. "\tChain " .. rule.table .. " " .. rule.chain .. " " .. rule.index .. ' "' .. name .. "\"\n"
  119. end
  120. end
  121. end
  122. return str
  123. end
  124. function config_network( c )
  125. local str = ""
  126. for s in pairs(sections) do
  127. for key, type in pairs({ Listen="collectd_network_listen", Server="collectd_network_server" }) do
  128. if sections[s][".type"] == type then
  129. host = sections[s].host
  130. port = sections[s].port
  131. if host then
  132. if port then
  133. str = str .. "\t" .. key .. " \"" .. host .. "\" \"" .. port .. "\"\n"
  134. else
  135. str = str .. "\t" .. key .. " \"" .. host .. "\"\n"
  136. end
  137. end
  138. end
  139. end
  140. end
  141. return str .. _string( c["TimeToLive"], "TimeToLive" )
  142. .. _string( c["CacheFlush"], "CacheFlush" )
  143. .. _bool( c["Forward"], "Forward" )
  144. end
  145. function _list_expand( c, l, nopad )
  146. local str = ""
  147. for i, n in ipairs(l) do
  148. if c[n] then
  149. if preprocess[n] then
  150. c[n] = preprocess[n](c[n])
  151. end
  152. if n:find("(%w+)ses") then
  153. k = n:gsub("(%w+)ses$", "%1s")
  154. else
  155. k = n:gsub("(%w+)s$", "%1")
  156. end
  157. str = str .. _expand( c[n], k, nopad )
  158. end
  159. end
  160. return str
  161. end
  162. function _expand( s, n, nopad )
  163. local str = ""
  164. if type(s) == "string" then
  165. for i, v in ipairs( luci.util.split( s, "%s+", nil, true ) ) do
  166. str = str .. _string( v, n, nopad )
  167. end
  168. elseif type(s) == "table" then
  169. for i, v in ipairs(s) do
  170. str = str .. _string( v, n, nopad )
  171. end
  172. end
  173. return str
  174. end
  175. function _bool( s, n, nopad )
  176. local str = ""
  177. local pad = ""
  178. if not nopad then pad = "\t" end
  179. if s and s == "1" then
  180. str = pad .. n .. " true"
  181. else
  182. str = pad .. n .. " false"
  183. end
  184. return str .. "\n"
  185. end
  186. function _string( s, n, nopad )
  187. local str = ""
  188. local pad = ""
  189. if not nopad then pad = "\t" end
  190. if s then
  191. if s:find("[^%d]") or n == "Port" then
  192. if not s:find("[^%w]") and n ~= "Port" then
  193. str = pad .. n .. " " .. luci.util.trim(s)
  194. else
  195. str = pad .. n .. ' "' .. luci.util.trim(s) .. '"'
  196. end
  197. else
  198. str = pad .. n .. " " .. luci.util.trim(s)
  199. end
  200. str = str .. "\n"
  201. end
  202. return str
  203. end
  204. plugins = {
  205. collectd = {
  206. { "BaseDir", "Include", "PIDFile", "PluginDir", "TypesDB", "Interval", "ReadThreads", "Hostname" },
  207. { },
  208. { }
  209. },
  210. logfile = {
  211. { "LogLevel", "File" },
  212. { "Timestamp" },
  213. { }
  214. },
  215. }
  216. local plugin_dir = "/usr/lib/lua/luci/statistics/plugins/"
  217. for filename in nixio.fs.dir(plugin_dir) do
  218. local plugin_fun = loadfile(plugin_dir .. filename)
  219. setfenv(plugin_fun, { _ = luci.i18n.translate })
  220. local plugin = plugin_fun()
  221. local name = filename:gsub("%.lua", "")
  222. if (name == "exec") then
  223. plugins[name] = config_exec
  224. elseif (name == "iptables") then
  225. plugins[name] = config_iptables
  226. elseif (name == "curl") then
  227. plugins[name] = config_curl
  228. elseif (name == "network") then
  229. plugins[name] = config_network
  230. else
  231. plugins[name] = plugin.legend
  232. end
  233. end
  234. preprocess = {
  235. RRATimespans = function(val)
  236. local rv = { }
  237. for time in val:gmatch("[^%s]+") do
  238. table.insert( rv, luci.util.parse_units(time) )
  239. end
  240. return table.concat(rv, " ")
  241. end
  242. }
  243. section("collectd")
  244. section("logfile")
  245. for plugin in pairs(plugins) do
  246. if (plugin ~= "collectd") and (plugin ~= "logfile") then
  247. section( plugin )
  248. end
  249. end