luci_statistics.lua 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. -- Copyright 2008 Freifunk Leipzig / Jo-Philipp Wich <jow@openwrt.org>
  2. -- Copyright 2012 Jo-Philipp Wich <jow@openwrt.org>
  3. -- Licensed to the public under the Apache License 2.0.
  4. module("luci.controller.luci_statistics.luci_statistics", package.seeall)
  5. function index()
  6. require("nixio.fs")
  7. require("luci.util")
  8. require("luci.statistics.datatree")
  9. require("luci.jsonc")
  10. -- override entry(): check for existence <plugin>.so where <plugin> is derived from the called path
  11. function _entry( path, ... )
  12. local file = path[5] or path[4]
  13. if nixio.fs.access( "/usr/lib/collectd/" .. file .. ".so" ) then
  14. entry( path, ... )
  15. end
  16. end
  17. local labels = {
  18. s_output = _("Output plugins"),
  19. s_general = _("General plugins"),
  20. s_network = _("Network plugins"),
  21. }
  22. -- our collectd menu
  23. local collectd_menu = {
  24. output = { },
  25. general = { },
  26. network = { }
  27. }
  28. local plugin_dir = "/usr/share/luci/statistics/plugins/"
  29. for filename in nixio.fs.dir(plugin_dir) do
  30. local plugin_def = luci.jsonc.parse(nixio.fs.readfile(plugin_dir .. filename))
  31. if type(plugin_def) == "table" then
  32. local name = filename:gsub("%.json", "")
  33. table.insert(collectd_menu[plugin_def.category], name)
  34. labels[name] = plugin_def.title
  35. end
  36. end
  37. -- create toplevel menu nodes
  38. local st = entry({"admin", "statistics"}, template("admin_statistics/index"), _("Statistics"), 80)
  39. st.index = true
  40. entry({"admin", "statistics", "collectd"}, cbi("luci_statistics/collectd"), _("Setup"), 20).subindex = true
  41. -- populate collectd plugin menu
  42. local index = 1
  43. for section, plugins in luci.util.kspairs( collectd_menu ) do
  44. local e = entry(
  45. { "admin", "statistics", "collectd", section },
  46. firstchild(), labels["s_"..section], index * 10
  47. )
  48. e.index = true
  49. for j, plugin in luci.util.vspairs( plugins ) do
  50. _entry(
  51. { "admin", "statistics", "collectd", section, plugin },
  52. cbi("luci_statistics/" .. plugin ),
  53. labels[plugin] or plugin, j * 10
  54. )
  55. end
  56. index = index + 1
  57. end
  58. -- output views
  59. local page = entry( { "admin", "statistics", "graph" }, template("admin_statistics/index"), _("Graphs"), 10)
  60. page.setuser = "nobody"
  61. page.setgroup = "nogroup"
  62. local vars = luci.http.formvalue(nil, true)
  63. local span = vars.timespan or nil
  64. local host = vars.host or nil
  65. -- get rrd data tree
  66. local tree = luci.statistics.datatree.Instance(host)
  67. local _, plugin, idx
  68. for _, plugin, idx in luci.util.vspairs( tree:plugins() ) do
  69. -- get plugin instances
  70. local instances = tree:plugin_instances( plugin )
  71. -- load plugin menu entry from the description
  72. local plugin_name = "luci.statistics.rrdtool.definitions." .. plugin
  73. local stat, def = pcall( require, plugin_name )
  74. if stat and def and type(def.item) == "function" then
  75. entry(
  76. { "admin", "statistics", "graph", plugin },
  77. call("statistics_render"), def.item(), idx
  78. ).query = { timespan = span , host = host }
  79. end
  80. -- if more then one instance is found then generate submenu
  81. if #instances > 1 then
  82. local _, inst, idx2
  83. for _, inst, idx2 in luci.util.vspairs(instances) do
  84. -- instance menu entry
  85. entry(
  86. { "admin", "statistics", "graph", plugin, inst },
  87. call("statistics_render"), inst, idx2
  88. ).query = { timespan = span , host = host }
  89. end
  90. end
  91. end
  92. end
  93. function statistics_render()
  94. require("luci.statistics.rrdtool")
  95. require("luci.template")
  96. require("luci.model.uci")
  97. local vars = luci.http.formvalue()
  98. local req = luci.dispatcher.context.request
  99. local path = luci.dispatcher.context.path
  100. local uci = luci.model.uci.cursor()
  101. local spans = luci.util.split( uci:get( "luci_statistics", "collectd_rrdtool", "RRATimespans" ), "%s+", nil, true )
  102. local span = vars.timespan or uci:get( "luci_statistics", "rrdtool", "default_timespan" ) or spans[1]
  103. local host = vars.host or uci:get( "luci_statistics", "collectd", "Hostname" ) or luci.sys.hostname()
  104. local opts = { host = vars.host }
  105. local graph = luci.statistics.rrdtool.Graph( luci.util.parse_units( span ), opts )
  106. local hosts = graph.tree:host_instances()
  107. local is_index = false
  108. local i, p, inst, idx
  109. -- deliver image
  110. if vars.img then
  111. local l12 = require "luci.ltn12"
  112. local png = io.open(graph.opts.imgpath .. "/" .. vars.img:gsub("%.+", "."), "r")
  113. if png then
  114. luci.http.prepare_content("image/png")
  115. l12.pump.all(l12.source.file(png), luci.http.write)
  116. end
  117. return
  118. end
  119. local plugin, instances
  120. local images = { }
  121. -- find requested plugin and instance
  122. for i, p in ipairs( luci.dispatcher.context.path ) do
  123. if luci.dispatcher.context.path[i] == "graph" then
  124. plugin = luci.dispatcher.context.path[i+1]
  125. instances = { luci.dispatcher.context.path[i+2] }
  126. end
  127. end
  128. -- no instance requested, find all instances
  129. if #instances == 0 then
  130. --instances = { graph.tree:plugin_instances( plugin )[1] }
  131. instances = graph.tree:plugin_instances( plugin )
  132. is_index = (#instances > 1)
  133. -- index instance requested
  134. elseif instances[1] == "-" then
  135. instances[1] = ""
  136. is_index = true
  137. end
  138. -- render graphs
  139. for i, inst in luci.util.vspairs( instances ) do
  140. for i, img in luci.util.vspairs( graph:render( plugin, inst, is_index ) ) do
  141. table.insert( images, graph:strippngpath( img ) )
  142. images[images[#images]] = inst
  143. end
  144. end
  145. luci.template.render( "public_statistics/graph", {
  146. images = images,
  147. plugin = plugin,
  148. timespans = spans,
  149. current_timespan = span,
  150. hosts = hosts,
  151. current_host = host,
  152. is_index = is_index
  153. } )
  154. end