stat-genconfig 6.6 KB

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