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.
5 module("luci.controller.luci_statistics.luci_statistics", package.seeall)
11 require("luci.statistics.datatree")
13 -- override entry(): check for existence <plugin>.so where <plugin> is derived from the called path
14 function _entry( path, ... )
15 local file = path[5] or path[4]
16 if nixio.fs.access( "/usr/lib/collectd/" .. file .. ".so" ) then
22 s_output = _("Output plugins"),
23 s_general = _("General plugins"),
24 s_network = _("Network plugins"),
28 local collectd_menu = {
34 local plugin_dir = "/usr/lib/lua/luci/statistics/plugins/"
35 for filename in nixio.fs.dir(plugin_dir) do
36 local plugin_fun = loadfile(plugin_dir .. filename)
37 setfenv(plugin_fun, { _ = luci.i18n.translate })
38 local plugin = plugin_fun()
39 local name = filename:gsub("%.lua", "")
40 table.insert(collectd_menu[plugin.category], name)
41 labels[name] = plugin.label
44 -- create toplevel menu nodes
45 local st = entry({"admin", "statistics"}, template("admin_statistics/index"), _("Statistics"), 80)
48 entry({"admin", "statistics", "collectd"}, cbi("luci_statistics/collectd"), _("Setup"), 20).subindex = true
51 -- populate collectd plugin menu
53 for section, plugins in luci.util.kspairs( collectd_menu ) do
55 { "admin", "statistics", "collectd", section },
56 firstchild(), labels["s_"..section], index * 10
61 for j, plugin in luci.util.vspairs( plugins ) do
63 { "admin", "statistics", "collectd", section, plugin },
64 cbi("luci_statistics/" .. plugin ),
65 labels[plugin] or plugin, j * 10
73 local page = entry( { "admin", "statistics", "graph" }, template("admin_statistics/index"), _("Graphs"), 10)
74 page.setuser = "nobody"
75 page.setgroup = "nogroup"
77 local vars = luci.http.formvalue(nil, true)
78 local span = vars.timespan or nil
79 local host = vars.host or nil
82 local tree = luci.statistics.datatree.Instance(host)
85 for _, plugin, idx in luci.util.vspairs( tree:plugins() ) do
87 -- get plugin instances
88 local instances = tree:plugin_instances( plugin )
90 -- load plugin menu entry from the description
91 local plugin_name = "luci.statistics.rrdtool.definitions." .. plugin
92 local stat, def = pcall( require, plugin_name )
93 if stat and def and type(def.item) == "function" then
95 { "admin", "statistics", "graph", plugin },
96 call("statistics_render"), def.item(), idx
97 ).query = { timespan = span , host = host }
100 -- if more then one instance is found then generate submenu
101 if #instances > 1 then
103 for _, inst, idx2 in luci.util.vspairs(instances) do
104 -- instance menu entry
106 { "admin", "statistics", "graph", plugin, inst },
107 call("statistics_render"), inst, idx2
108 ).query = { timespan = span , host = host }
114 function statistics_render()
116 require("luci.statistics.rrdtool")
117 require("luci.template")
118 require("luci.model.uci")
120 local vars = luci.http.formvalue()
121 local req = luci.dispatcher.context.request
122 local path = luci.dispatcher.context.path
123 local uci = luci.model.uci.cursor()
124 local spans = luci.util.split( uci:get( "luci_statistics", "collectd_rrdtool", "RRATimespans" ), "%s+", nil, true )
125 local span = vars.timespan or uci:get( "luci_statistics", "rrdtool", "default_timespan" ) or spans[1]
126 local host = vars.host or uci:get( "luci_statistics", "collectd", "Hostname" ) or luci.sys.hostname()
127 local opts = { host = vars.host }
128 local graph = luci.statistics.rrdtool.Graph( luci.util.parse_units( span ), opts )
129 local hosts = graph.tree:host_instances()
131 local is_index = false
132 local i, p, inst, idx
136 local l12 = require "luci.ltn12"
137 local png = io.open(graph.opts.imgpath .. "/" .. vars.img:gsub("%.+", "."), "r")
139 luci.http.prepare_content("image/png")
140 l12.pump.all(l12.source.file(png), luci.http.write)
145 local plugin, instances
148 -- find requested plugin and instance
149 for i, p in ipairs( luci.dispatcher.context.path ) do
150 if luci.dispatcher.context.path[i] == "graph" then
151 plugin = luci.dispatcher.context.path[i+1]
152 instances = { luci.dispatcher.context.path[i+2] }
156 -- no instance requested, find all instances
157 if #instances == 0 then
158 --instances = { graph.tree:plugin_instances( plugin )[1] }
159 instances = graph.tree:plugin_instances( plugin )
160 is_index = (#instances > 1)
162 -- index instance requested
163 elseif instances[1] == "-" then
169 for i, inst in luci.util.vspairs( instances ) do
170 for i, img in luci.util.vspairs( graph:render( plugin, inst, is_index ) ) do
171 table.insert( images, graph:strippngpath( img ) )
172 images[images[#images]] = inst
176 luci.template.render( "public_statistics/graph", {
180 current_timespan = span,