luci_statistics.lua 5.8 KB

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