luci_statistics.lua 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. -- override entry(): check for existance <plugin>.so where <plugin> is derived from the called path
  10. function _entry( path, ... )
  11. local file = path[5] or path[4]
  12. if nixio.fs.access( "/usr/lib/collectd/" .. file .. ".so" ) then
  13. entry( path, ... )
  14. end
  15. end
  16. local labels = {
  17. s_output = _("Output plugins"),
  18. s_general = _("General plugins"),
  19. s_network = _("Network plugins"),
  20. apcups = _("APC UPS"),
  21. conntrack = _("Conntrack"),
  22. contextswitch = _("Context Switches"),
  23. cpu = _("Processor"),
  24. cpufreq = _("CPU Frequency"),
  25. csv = _("CSV Output"),
  26. df = _("Disk Space Usage"),
  27. disk = _("Disk Usage"),
  28. dns = _("DNS"),
  29. email = _("Email"),
  30. entropy = _("Entropy"),
  31. exec = _("Exec"),
  32. interface = _("Interfaces"),
  33. iptables = _("Firewall"),
  34. irq = _("Interrupts"),
  35. iwinfo = _("Wireless"),
  36. load = _("System Load"),
  37. memory = _("Memory"),
  38. netlink = _("Netlink"),
  39. network = _("Network"),
  40. nut = _("UPS"),
  41. olsrd = _("OLSRd"),
  42. openvpn = _("OpenVPN"),
  43. ping = _("Ping"),
  44. processes = _("Processes"),
  45. rrdtool = _("RRDTool"),
  46. sensors = _("Sensors"),
  47. splash_leases = _("Splash Leases"),
  48. tcpconns = _("TCP Connections"),
  49. thermal = _("Thermal"),
  50. unixsock = _("UnixSock"),
  51. uptime = _("Uptime")
  52. }
  53. -- our collectd menu
  54. local collectd_menu = {
  55. output = { "csv", "network", "rrdtool", "unixsock" },
  56. general = { "apcups", "contextswitch", "cpu", "cpufreq", "df",
  57. "disk", "email", "entropy", "exec", "irq", "load", "memory",
  58. "nut", "processes", "sensors", "thermal", "uptime" },
  59. network = { "conntrack", "dns", "interface", "iptables",
  60. "netlink", "olsrd", "openvpn", "ping",
  61. "splash_leases", "tcpconns", "iwinfo" }
  62. }
  63. -- create toplevel menu nodes
  64. local st = entry({"admin", "statistics"}, template("admin_statistics/index"), _("Statistics"), 80)
  65. st.index = true
  66. entry({"admin", "statistics", "collectd"}, cbi("luci_statistics/collectd"), _("Setup"), 20).subindex = true
  67. -- populate collectd plugin menu
  68. local index = 1
  69. for section, plugins in luci.util.kspairs( collectd_menu ) do
  70. local e = entry(
  71. { "admin", "statistics", "collectd", section },
  72. firstchild(), labels["s_"..section], index * 10
  73. )
  74. e.index = true
  75. for j, plugin in luci.util.vspairs( plugins ) do
  76. _entry(
  77. { "admin", "statistics", "collectd", section, plugin },
  78. cbi("luci_statistics/" .. plugin ),
  79. labels[plugin] or plugin, j * 10
  80. )
  81. end
  82. index = index + 1
  83. end
  84. -- output views
  85. local page = entry( { "admin", "statistics", "graph" }, template("admin_statistics/index"), _("Graphs"), 10)
  86. page.setuser = "nobody"
  87. page.setgroup = "nogroup"
  88. local vars = luci.http.formvalue(nil, true)
  89. local span = vars.timespan or nil
  90. local host = vars.host or nil
  91. -- get rrd data tree
  92. local tree = luci.statistics.datatree.Instance(host)
  93. local _, plugin, idx
  94. for _, plugin, idx in luci.util.vspairs( tree:plugins() ) do
  95. -- get plugin instances
  96. local instances = tree:plugin_instances( plugin )
  97. -- plugin menu entry
  98. entry(
  99. { "admin", "statistics", "graph", plugin },
  100. call("statistics_render"), labels[plugin], idx
  101. ).query = { timespan = span , host = host }
  102. -- if more then one instance is found then generate submenu
  103. if #instances > 1 then
  104. local _, inst, idx2
  105. for _, inst, idx2 in luci.util.vspairs(instances) do
  106. -- instance menu entry
  107. entry(
  108. { "admin", "statistics", "graph", plugin, inst },
  109. call("statistics_render"), inst, idx2
  110. ).query = { timespan = span , host = host }
  111. end
  112. end
  113. end
  114. end
  115. function statistics_render()
  116. require("luci.statistics.rrdtool")
  117. require("luci.template")
  118. require("luci.model.uci")
  119. local vars = luci.http.formvalue()
  120. local req = luci.dispatcher.context.request
  121. local path = luci.dispatcher.context.path
  122. local uci = luci.model.uci.cursor()
  123. local spans = luci.util.split( uci:get( "luci_statistics", "collectd_rrdtool", "RRATimespans" ), "%s+", nil, true )
  124. local span = vars.timespan or uci:get( "luci_statistics", "rrdtool", "default_timespan" ) or spans[1]
  125. local host = vars.host or uci:get( "luci_statistics", "collectd", "Hostname" ) or luci.sys.hostname()
  126. local opts = { host = vars.host }
  127. local graph = luci.statistics.rrdtool.Graph( luci.util.parse_units( span ), opts )
  128. local hosts = graph.tree:host_instances()
  129. local is_index = false
  130. local i, p, inst, idx
  131. -- deliver image
  132. if vars.img then
  133. local l12 = require "luci.ltn12"
  134. local png = io.open(graph.opts.imgpath .. "/" .. vars.img:gsub("%.+", "."), "r")
  135. if png then
  136. luci.http.prepare_content("image/png")
  137. l12.pump.all(l12.source.file(png), luci.http.write)
  138. end
  139. return
  140. end
  141. local plugin, instances
  142. local images = { }
  143. -- find requested plugin and instance
  144. for i, p in ipairs( luci.dispatcher.context.path ) do
  145. if luci.dispatcher.context.path[i] == "graph" then
  146. plugin = luci.dispatcher.context.path[i+1]
  147. instances = { luci.dispatcher.context.path[i+2] }
  148. end
  149. end
  150. -- no instance requested, find all instances
  151. if #instances == 0 then
  152. --instances = { graph.tree:plugin_instances( plugin )[1] }
  153. instances = graph.tree:plugin_instances( plugin )
  154. is_index = (#instances > 1)
  155. -- index instance requested
  156. elseif instances[1] == "-" then
  157. instances[1] = ""
  158. is_index = true
  159. end
  160. -- render graphs
  161. for i, inst in luci.util.vspairs( instances ) do
  162. for i, img in luci.util.vspairs( graph:render( plugin, inst, is_index ) ) do
  163. table.insert( images, graph:strippngpath( img ) )
  164. images[images[#images]] = inst
  165. end
  166. end
  167. luci.template.render( "public_statistics/graph", {
  168. images = images,
  169. plugin = plugin,
  170. timespans = spans,
  171. current_timespan = span,
  172. hosts = hosts,
  173. current_host = host,
  174. is_index = is_index
  175. } )
  176. end