stat-genconfig 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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. apcups = {
  192. { "Host", "Port" },
  193. { },
  194. { }
  195. },
  196. collectd = {
  197. { "BaseDir", "Include", "PIDFile", "PluginDir", "TypesDB", "Interval", "ReadThreads", "Hostname" },
  198. { },
  199. { }
  200. },
  201. conntrack = {
  202. { },
  203. { },
  204. { }
  205. },
  206. cpu = {
  207. { },
  208. { },
  209. { }
  210. },
  211. cpufreq = {
  212. { },
  213. { },
  214. { }
  215. },
  216. contextswitch = {
  217. { },
  218. { },
  219. { }
  220. },
  221. csv = {
  222. { "DataDir" },
  223. { "StoreRates" },
  224. { }
  225. },
  226. df = {
  227. { },
  228. { "IgnoreSelected" },
  229. { "Devices", "MountPoints", "FSTypes" }
  230. },
  231. disk = {
  232. { },
  233. { "IgnoreSelected" },
  234. { "Disks" }
  235. },
  236. dns = {
  237. { },
  238. { },
  239. { "Interfaces", "IgnoreSources" }
  240. },
  241. email = {
  242. { "SocketFile", "SocketGroup", "SocketPerms", "MaxConns" },
  243. { },
  244. { }
  245. },
  246. entropy = {
  247. { },
  248. { },
  249. { }
  250. },
  251. exec = config_exec,
  252. interface = {
  253. { },
  254. { "IgnoreSelected" },
  255. { "Interfaces" }
  256. },
  257. iptables = config_iptables,
  258. irq = {
  259. { },
  260. { "IgnoreSelected" },
  261. { "Irqs" }
  262. },
  263. iwinfo = {
  264. { },
  265. { "IgnoreSelected" },
  266. { "Interfaces" }
  267. },
  268. load = {
  269. { },
  270. { },
  271. { }
  272. },
  273. logfile = {
  274. { "LogLevel", "File" },
  275. { "Timestamp" },
  276. { }
  277. },
  278. memory = {
  279. { },
  280. { },
  281. { }
  282. },
  283. netlink = {
  284. { },
  285. { "IgnoreSelected" },
  286. { "Interfaces", "VerboseInterfaces", "QDiscs", "Classes", "Filters" }
  287. },
  288. network = config_network,
  289. nut = {
  290. { "UPS" },
  291. { },
  292. { }
  293. },
  294. olsrd = {
  295. { "Host", "Port", "CollectLinks","CollectRoutes","CollectTopology"},
  296. { },
  297. { }
  298. },
  299. openvpn = {
  300. { },
  301. { "CollectIndividualUsers", "CollectUserCount", "CollectCompression", "ImprovedNamingSchema" },
  302. { "StatusFile" }
  303. },
  304. ping = {
  305. { "TTL", "Interval" },
  306. { },
  307. { "Hosts" }
  308. },
  309. processes = {
  310. { },
  311. { },
  312. { "Processes" }
  313. },
  314. rrdtool = {
  315. { "DataDir", "StepSize", "HeartBeat", "RRARows", "XFF", "CacheFlush", "CacheTimeout" },
  316. { "RRASingle" },
  317. { "RRATimespans" }
  318. },
  319. sensors = {
  320. { },
  321. { "IgnoreSelected" },
  322. { "Sensor" }
  323. },
  324. splash_leases = {
  325. { },
  326. { },
  327. { }
  328. },
  329. tcpconns = {
  330. { },
  331. { "ListeningPorts" },
  332. { "LocalPorts", "RemotePorts" }
  333. },
  334. thermal = {
  335. { },
  336. { "IgnoreSelected" },
  337. { "Device" }
  338. },
  339. unixsock = {
  340. { "SocketFile", "SocketGroup", "SocketPerms" },
  341. { },
  342. { }
  343. },
  344. uptime = {
  345. { },
  346. { },
  347. { }
  348. },
  349. wireless = {
  350. { },
  351. { },
  352. { }
  353. },
  354. }
  355. preprocess = {
  356. RRATimespans = function(val)
  357. local rv = { }
  358. for time in val:gmatch("[^%s]+") do
  359. table.insert( rv, luci.util.parse_units(time) )
  360. end
  361. return table.concat(rv, " ")
  362. end
  363. }
  364. section("collectd")
  365. section("logfile")
  366. for plugin in pairs(plugins) do
  367. if (plugin ~= "collectd") and (plugin ~= "logfile") then
  368. section( plugin )
  369. end
  370. end