3 Luci statistics - statistics controller module
4 (c) 2008 Freifunk Leipzig / Jo-Philipp Wich <xm@leipzig.freifunk.net>
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
10 http://www.apache.org/licenses/LICENSE-2.0
16 module("luci.controller.luci_statistics.luci_statistics", package.seeall)
23 require("luci.statistics.datatree")
25 -- load language files
26 luci.i18n.loadc("rrdtool")
27 luci.i18n.loadc("statistics")
30 local tree = luci.statistics.datatree.Instance()
32 -- override entry(): check for existance <plugin>.so where <plugin> is derived from the called path
33 function _entry( path, ... )
34 local file = path[5] or path[4]
35 if luci.fs.isfile( "/usr/lib/collectd/" .. file .. ".so" ) then
40 -- override i18n(): try to translate stat_<str> or fall back to <str>
42 return luci.i18n.translate( "stat_" .. str, str )
46 local collectd_menu = {
47 output = { "rrdtool", "network", "unixsock", "csv" },
48 system = { "exec", "email", "cpu", "df", "disk", "irq", "processes", "load" },
49 network = { "interface", "netlink", "iptables", "tcpconns", "ping", "dns", "wireless" }
52 -- create toplevel menu nodes
53 entry({"admin", "statistics"}, call("statistics_index"), _i18n("statistics"), 80).i18n = "statistics"
54 entry({"admin", "statistics", "collectd"}, cbi("luci_statistics/collectd"), _i18n("collectd"), 10)
56 -- populate collectd plugin menu
58 for section, plugins in luci.util.kspairs( collectd_menu ) do
60 { "admin", "statistics", "collectd", section },
61 call( "statistics_" .. section .. "plugins" ),
62 _i18n( section .. "plugins" ),
66 for j, plugin in luci.util.vspairs( plugins ) do
68 { "admin", "statistics", "collectd", section, plugin },
69 cbi("luci_statistics/" .. plugin ),
79 local page = entry( { "admin", "statistics", "graph" }, call("statistics_index"), _i18n("graphs"), 80)
80 page.i18n = "statistics"
81 page.setuser = "nobody"
82 page.setgroup = "nogroup"
84 local vars = luci.http.formvalue(nil, true)
85 local span = vars.timespan or nil
87 for i, plugin in luci.util.vspairs( tree:plugins() ) do
89 -- get plugin instances
90 local instances = tree:plugin_instances( plugin )
94 { "admin", "statistics", "graph", plugin },
95 call("statistics_render"), _i18n( plugin ), i
96 ).query = { timespan = span }
98 -- if more then one instance is found then generate submenu
99 if #instances > 1 then
100 for j, inst in luci.util.vspairs(instances) do
101 -- instance menu entry
103 { "admin", "statistics", "graph", plugin, inst },
104 call("statistics_render"), inst, j
105 ).query = { timespan = span }
111 function statistics_index()
112 luci.template.render("admin_statistics/index")
115 function statistics_outputplugins()
118 for i, p in ipairs({ "rrdtool", "network", "unixsock", "csv" }) do
119 plugins[p] = luci.i18n.translate( "stat_" .. p, p )
122 luci.template.render("admin_statistics/outputplugins", {plugins=plugins})
125 function statistics_systemplugins()
128 for i, p in ipairs({ "exec", "email", "df", "disk", "irq", "processes", "cpu" }) do
129 plugins[p] = luci.i18n.translate( "stat_" .. p, p )
132 luci.template.render("admin_statistics/systemplugins", {plugins=plugins})
135 function statistics_networkplugins()
138 for i, p in ipairs({ "interface", "netlink", "iptables", "tcpconns", "ping", "dns", "wireless" }) do
139 plugins[p] = luci.i18n.translate( "stat_" .. p, p )
142 luci.template.render("admin_statistics/networkplugins", {plugins=plugins})
146 function statistics_render()
148 require("luci.statistics.rrdtool")
149 require("luci.template")
150 require("luci.model.uci")
152 local vars = luci.http.formvalue()
153 local req = luci.dispatcher.context.request
154 local path = luci.dispatcher.context.path
155 local uci = luci.model.uci.cursor()
156 local spans = luci.util.split( uci:get( "luci_statistics", "collectd_rrdtool", "RRATimespans" ), "%s+", nil, true )
157 local span = vars.timespan or uci:get( "luci_statistics", "rrdtool", "default_timespan" ) or spans[1]
158 local graph = luci.statistics.rrdtool.Graph( luci.util.parse_units( span ) )
160 local plugin, instances
163 -- find requested plugin and instance
164 for i, p in ipairs( luci.dispatcher.context.path ) do
165 if luci.dispatcher.context.path[i] == "graph" then
166 plugin = luci.dispatcher.context.path[i+1]
167 instances = { luci.dispatcher.context.path[i+2] }
171 -- no instance requested, find all instances
172 if #instances == 0 then
173 instances = { graph.tree:plugin_instances( plugin )[1] }
175 -- index instance requested
176 elseif instances[1] == "-" then
182 for i, inst in ipairs( instances ) do
183 for i, img in ipairs( graph:render( plugin, inst ) ) do
184 table.insert( images, graph:strippngpath( img ) )
188 luci.template.render( "public_statistics/graph", {
192 current_timespan = span