2008-06-03 23:15:16 +00:00
|
|
|
--[[
|
|
|
|
|
|
|
|
Luci statistics - statistics controller module
|
|
|
|
(c) 2008 Freifunk Leipzig / Jo-Philipp Wich <xm@leipzig.freifunk.net>
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
$Id$
|
|
|
|
|
|
|
|
]]--
|
|
|
|
|
2008-05-25 17:53:04 +00:00
|
|
|
module("luci.controller.luci_statistics.luci_statistics", package.seeall)
|
2008-05-23 19:23:06 +00:00
|
|
|
|
2008-05-27 21:23:11 +00:00
|
|
|
function index()
|
|
|
|
|
2008-06-03 19:05:34 +00:00
|
|
|
require("luci.fs")
|
2008-06-08 21:15:06 +00:00
|
|
|
require("luci.util")
|
2008-05-30 00:26:18 +00:00
|
|
|
require("luci.i18n")
|
|
|
|
require("luci.statistics.datatree")
|
|
|
|
|
|
|
|
-- load language file
|
2008-06-08 14:19:26 +00:00
|
|
|
luci.i18n.loadc("statistics")
|
2008-05-29 00:59:58 +00:00
|
|
|
|
2008-05-30 00:26:18 +00:00
|
|
|
-- get rrd data tree
|
|
|
|
local tree = luci.statistics.datatree.Instance()
|
|
|
|
|
|
|
|
-- override entry(): check for existance <plugin>.so where <plugin> is derived from the called path
|
2008-05-27 21:23:11 +00:00
|
|
|
function _entry( path, ... )
|
2008-06-08 21:15:06 +00:00
|
|
|
local file = path[5] or path[4]
|
2008-05-30 00:26:18 +00:00
|
|
|
if luci.fs.isfile( "/usr/lib/collectd/" .. file .. ".so" ) then
|
2008-05-27 21:23:11 +00:00
|
|
|
entry( path, ... )
|
|
|
|
end
|
2008-05-23 19:23:06 +00:00
|
|
|
end
|
|
|
|
|
2008-05-30 00:26:18 +00:00
|
|
|
-- override i18n(): try to translate stat_<str> or fall back to <str>
|
2008-05-29 00:59:58 +00:00
|
|
|
function _i18n( str )
|
2008-05-30 00:26:18 +00:00
|
|
|
return luci.i18n.translate( "stat_" .. str, str )
|
2008-05-29 00:59:58 +00:00
|
|
|
end
|
2008-05-23 19:23:06 +00:00
|
|
|
|
2008-06-08 21:15:06 +00:00
|
|
|
-- our collectd menu
|
|
|
|
local collectd_menu = {
|
|
|
|
output = { "rrdtool", "network", "unixsock", "csv" },
|
2008-06-08 21:51:55 +00:00
|
|
|
system = { "exec", "email", "cpu", "df", "disk", "irq", "processes", "load" },
|
2008-06-08 21:15:06 +00:00
|
|
|
network = { "interface", "netlink", "iptables", "tcpconns", "ping", "dns", "wireless" }
|
|
|
|
}
|
2008-05-30 00:26:18 +00:00
|
|
|
|
2008-06-08 21:15:06 +00:00
|
|
|
-- create toplevel menu nodes
|
|
|
|
entry({"admin", "statistics"}, call("statistics_index"), _i18n("statistics"), 80).i18n = "statistics"
|
|
|
|
entry({"admin", "statistics", "collectd"}, cbi("luci_statistics/collectd"), _i18n("collectd"), 10)
|
2008-06-02 00:48:25 +00:00
|
|
|
|
2008-06-08 21:15:06 +00:00
|
|
|
-- populate collectd plugin menu
|
|
|
|
local index = 1
|
|
|
|
for section, plugins in luci.util.kspairs( collectd_menu ) do
|
|
|
|
entry(
|
|
|
|
{ "admin", "statistics", "collectd", section },
|
|
|
|
call( "statistics_" .. section .. "plugins" ),
|
|
|
|
_i18n( section .. "plugins" ),
|
|
|
|
index * 10
|
|
|
|
)
|
|
|
|
|
|
|
|
for j, plugin in luci.util.vspairs( plugins ) do
|
|
|
|
_entry(
|
|
|
|
{ "admin", "statistics", "collectd", section, plugin },
|
|
|
|
cbi("luci_statistics/" .. plugin ),
|
|
|
|
_i18n( plugin ),
|
|
|
|
j * 10
|
|
|
|
)
|
|
|
|
end
|
2008-05-25 02:37:39 +00:00
|
|
|
|
2008-06-08 21:15:06 +00:00
|
|
|
index = index + 1
|
|
|
|
end
|
2008-06-05 09:21:44 +00:00
|
|
|
|
2008-06-03 20:04:20 +00:00
|
|
|
-- output views
|
2008-06-04 22:41:58 +00:00
|
|
|
local page = entry( { "admin", "statistics", "graph" }, call("statistics_index"), _i18n("graphs"), 80)
|
|
|
|
page.i18n = "statistics"
|
|
|
|
page.setuser = "nobody"
|
|
|
|
page.setgroup = "nogroup"
|
2008-05-30 00:26:18 +00:00
|
|
|
|
2008-06-15 17:45:10 +00:00
|
|
|
local vars = luci.http.formvalue()
|
2008-06-03 19:05:34 +00:00
|
|
|
local span = vars.timespan or nil
|
|
|
|
|
2008-06-08 21:15:06 +00:00
|
|
|
for i, plugin in luci.util.vspairs( tree:plugins() ) do
|
2008-05-29 00:59:58 +00:00
|
|
|
|
|
|
|
-- get plugin instances
|
2008-05-30 00:26:18 +00:00
|
|
|
local instances = tree:plugin_instances( plugin )
|
2008-05-29 00:59:58 +00:00
|
|
|
|
|
|
|
-- plugin menu entry
|
2008-06-03 19:05:34 +00:00
|
|
|
entry(
|
2008-06-03 20:04:20 +00:00
|
|
|
{ "admin", "statistics", "graph", plugin },
|
2008-06-03 19:05:34 +00:00
|
|
|
call("statistics_render"), _i18n( plugin ), i
|
|
|
|
).query = { timespan = span }
|
2008-05-29 00:59:58 +00:00
|
|
|
|
|
|
|
-- if more then one instance is found then generate submenu
|
|
|
|
if #instances > 1 then
|
2008-06-08 21:15:06 +00:00
|
|
|
for j, inst in luci.util.vspairs(instances) do
|
2008-05-29 00:59:58 +00:00
|
|
|
-- instance menu entry
|
2008-05-30 00:26:18 +00:00
|
|
|
entry(
|
2008-06-03 20:04:20 +00:00
|
|
|
{ "admin", "statistics", "graph", plugin, inst },
|
2008-05-30 00:26:18 +00:00
|
|
|
call("statistics_render"), inst, j
|
2008-06-03 19:05:34 +00:00
|
|
|
).query = { timespan = span }
|
2008-05-29 00:59:58 +00:00
|
|
|
end
|
2008-06-03 19:05:34 +00:00
|
|
|
end
|
2008-05-25 02:37:39 +00:00
|
|
|
end
|
2008-05-23 19:23:06 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function statistics_index()
|
2008-05-30 00:26:18 +00:00
|
|
|
luci.template.render("admin_statistics/index")
|
2008-05-23 19:23:06 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function statistics_outputplugins()
|
2008-06-02 00:48:25 +00:00
|
|
|
local plugins = { }
|
|
|
|
|
|
|
|
for i, p in ipairs({ "rrdtool", "network", "unixsock", "csv" }) do
|
|
|
|
plugins[p] = luci.i18n.translate( "stat_" .. p, p )
|
|
|
|
end
|
2008-05-23 19:23:06 +00:00
|
|
|
|
2008-05-30 00:26:18 +00:00
|
|
|
luci.template.render("admin_statistics/outputplugins", {plugins=plugins})
|
2008-05-23 19:23:06 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function statistics_systemplugins()
|
2008-06-02 00:48:25 +00:00
|
|
|
local plugins = { }
|
|
|
|
|
|
|
|
for i, p in ipairs({ "exec", "email", "df", "disk", "irq", "processes", "cpu" }) do
|
|
|
|
plugins[p] = luci.i18n.translate( "stat_" .. p, p )
|
|
|
|
end
|
2008-05-23 19:23:06 +00:00
|
|
|
|
2008-05-30 00:26:18 +00:00
|
|
|
luci.template.render("admin_statistics/systemplugins", {plugins=plugins})
|
2008-05-23 19:23:06 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function statistics_networkplugins()
|
2008-06-02 00:48:25 +00:00
|
|
|
local plugins = { }
|
|
|
|
|
|
|
|
for i, p in ipairs({ "interface", "netlink", "iptables", "tcpconns", "ping", "dns", "wireless" }) do
|
|
|
|
plugins[p] = luci.i18n.translate( "stat_" .. p, p )
|
|
|
|
end
|
2008-05-23 19:23:06 +00:00
|
|
|
|
2008-05-30 00:26:18 +00:00
|
|
|
luci.template.render("admin_statistics/networkplugins", {plugins=plugins})
|
2008-05-23 19:23:06 +00:00
|
|
|
end
|
2008-05-25 02:37:39 +00:00
|
|
|
|
|
|
|
|
2008-06-03 23:15:16 +00:00
|
|
|
function statistics_render()
|
2008-05-30 00:26:18 +00:00
|
|
|
|
|
|
|
require("luci.statistics.rrdtool")
|
|
|
|
require("luci.template")
|
2008-06-03 19:05:34 +00:00
|
|
|
require("luci.model.uci")
|
2008-05-30 00:26:18 +00:00
|
|
|
|
2008-06-15 17:45:10 +00:00
|
|
|
local vars = luci.http.formvalue()
|
2008-06-14 14:12:12 +00:00
|
|
|
local req = luci.dispatcher.context.request
|
|
|
|
local path = luci.dispatcher.context.dispatched.path
|
2008-06-05 19:16:38 +00:00
|
|
|
local uci = luci.model.uci
|
|
|
|
local spans = luci.util.split( uci.get( "luci_statistics", "collectd_rrdtool", "RRATimespans" ), "%s+", nil, true )
|
|
|
|
local span = vars.timespan or uci.get( "luci_statistics", "rrdtool", "default_timespan" ) or spans[1]
|
2008-06-03 19:05:34 +00:00
|
|
|
local graph = luci.statistics.rrdtool.Graph( luci.util.parse_units( span ) )
|
2008-05-30 00:26:18 +00:00
|
|
|
|
2008-06-05 09:19:46 +00:00
|
|
|
local plugin, instances
|
|
|
|
local images = { }
|
|
|
|
|
|
|
|
-- find requested plugin and instance
|
2008-06-14 14:12:12 +00:00
|
|
|
for i, p in ipairs( luci.dispatcher.context.dispatched.path ) do
|
|
|
|
if luci.dispatcher.context.dispatched.path[i] == "graph" then
|
|
|
|
plugin = luci.dispatcher.context.dispatched.path[i+1]
|
|
|
|
instances = { luci.dispatcher.context.dispatched.path[i+2] }
|
2008-06-05 09:21:44 +00:00
|
|
|
end
|
2008-06-05 09:19:46 +00:00
|
|
|
end
|
2008-05-29 00:59:58 +00:00
|
|
|
|
|
|
|
-- no instance requested, find all instances
|
|
|
|
if #instances == 0 then
|
2008-06-05 09:19:46 +00:00
|
|
|
instances = { graph.tree:plugin_instances( plugin )[1] }
|
2008-05-30 00:26:18 +00:00
|
|
|
|
|
|
|
-- index instance requested
|
|
|
|
elseif instances[1] == "-" then
|
|
|
|
instances[1] = ""
|
2008-05-29 00:59:58 +00:00
|
|
|
end
|
2008-05-25 02:37:39 +00:00
|
|
|
|
2008-05-30 00:26:18 +00:00
|
|
|
|
2008-05-29 00:59:58 +00:00
|
|
|
-- render graphs
|
|
|
|
for i, inst in ipairs( instances ) do
|
2008-05-28 22:34:26 +00:00
|
|
|
for i, img in ipairs( graph:render( plugin, inst ) ) do
|
2008-06-01 03:12:48 +00:00
|
|
|
table.insert( images, graph:strippngpath( img ) )
|
2008-05-25 02:37:39 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-06-03 19:05:34 +00:00
|
|
|
luci.template.render( "public_statistics/graph", {
|
|
|
|
images = images,
|
|
|
|
plugin = plugin,
|
|
|
|
timespans = spans,
|
|
|
|
current_timespan = span
|
|
|
|
} )
|
2008-05-25 02:37:39 +00:00
|
|
|
end
|