X-Git-Url: https://git.archive.openwrt.org/?p=project%2Fluci.git;a=blobdiff_plain;f=applications%2Fluci-statistics%2Fluasrc%2Fcontroller%2Fluci_statistics%2Fluci_statistics.lua;h=7ca8a5aa374f734bcf4f1c3c5e00136fbe710bf9;hp=3b1bb99d711c6292b16e22973842704724e54ffd;hb=cbb0bead6fe87a99f77e0597733dcac453d08097;hpb=db3a4c5c9799c8921fc8a6b4dd17acc19fb510b3 diff --git a/applications/luci-statistics/luasrc/controller/luci_statistics/luci_statistics.lua b/applications/luci-statistics/luasrc/controller/luci_statistics/luci_statistics.lua index 3b1bb99d7..7ca8a5aa3 100644 --- a/applications/luci-statistics/luasrc/controller/luci_statistics/luci_statistics.lua +++ b/applications/luci-statistics/luasrc/controller/luci_statistics/luci_statistics.lua @@ -1,71 +1,90 @@ +--[[ + +Luci statistics - statistics controller module +(c) 2008 Freifunk Leipzig / Jo-Philipp Wich + +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$ + +]]-- + module("luci.controller.luci_statistics.luci_statistics", package.seeall) function index() require("luci.fs") + require("luci.util") require("luci.i18n") require("luci.statistics.datatree") - -- load language file - luci.i18n.load("statistics.en") -- XXX: temporary / replace with loadc() + -- load language files + luci.i18n.loadc("rrdtool") + luci.i18n.loadc("statistics") -- get rrd data tree local tree = luci.statistics.datatree.Instance() -- override entry(): check for existance .so where is derived from the called path function _entry( path, ... ) - local file = path[4] or path[3] + local file = path[5] or path[4] if luci.fs.isfile( "/usr/lib/collectd/" .. file .. ".so" ) then entry( path, ... ) end end - -- override call(): call requested action function with supplied parameters - function _call( func, tree, plugin ) - return function() getfenv()[func]( tree, plugin ) end - end - -- override i18n(): try to translate stat_ or fall back to function _i18n( str ) return luci.i18n.translate( "stat_" .. str, str ) end + -- our collectd menu + local collectd_menu = { + output = { "rrdtool", "network", "unixsock", "csv" }, + system = { "exec", "email", "cpu", "df", "disk", "irq", "processes", "load" }, + network = { "interface", "netlink", "iptables", "tcpconns", "ping", "dns", "wireless" } + } + + -- 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) + + -- 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 + + index = index + 1 + end - entry({"admin", "statistics"}, call("statistics_index"), _i18n("statistics"), 80).i18n = "statistics" - entry({"admin", "statistics", "collectd"}, cbi("luci_statistics/collectd"), _i18n("collectd"), 10) - - entry({"admin", "statistics", "output"}, call("statistics_outputplugins"), _i18n("outputplugins"), 20) - _entry({"admin", "statistics", "output", "rrdtool"}, cbi("luci_statistics/rrdtool"), _i18n("rrdtool"), 10) - _entry({"admin", "statistics", "output", "network"}, cbi("luci_statistics/network"), _i18n("network"), 20) - _entry({"admin", "statistics", "output", "unixsock"}, cbi("luci_statistics/unixsock"), _i18n("unixsock"), 30) - _entry({"admin", "statistics", "output", "csv"}, cbi("luci_statistics/csv"), _i18n("csv"), 40) - - entry({"admin", "statistics", "system"}, call("statistics_systemplugins"), _i18n("systemplugins"), 30) - _entry({"admin", "statistics", "system", "exec"}, cbi("luci_statistics/exec"), _i18n("exec"), 10) - _entry({"admin", "statistics", "system", "email"}, cbi("luci_statistics/email"), _i18n("email"), 20) - _entry({"admin", "statistics", "system", "cpu"}, cbi("luci_statistics/cpu"), _i18n("cpu"), 30) - _entry({"admin", "statistics", "system", "df"}, cbi("luci_statistics/df"), _i18n("df"), 40) - _entry({"admin", "statistics", "system", "disk"}, cbi("luci_statistics/disk"), _i18n("disk"), 50) - _entry({"admin", "statistics", "system", "irq"}, cbi("luci_statistics/irq"), _i18n("irq"), 60) - _entry({"admin", "statistics", "system", "processes"}, cbi("luci_statistics/processes"), _i18n("processes"), 70) - - entry({"admin", "statistics", "network"}, call("statistics_networkplugins"), _i18n("networkplugins"),40) - _entry({"admin", "statistics", "network", "interface"}, cbi("luci_statistics/interface"), _i18n("interface"), 10) - _entry({"admin", "statistics", "network", "netlink"}, cbi("luci_statistics/netlink"), _i18n("netlink"), 20) - _entry({"admin", "statistics", "network", "iptables"}, cbi("luci_statistics/iptables"), _i18n("iptables"), 30) - _entry({"admin", "statistics", "network", "tcpconns"}, cbi("luci_statistics/tcpconns"), _i18n("tcpconns"), 40) - _entry({"admin", "statistics", "network", "ping"}, cbi("luci_statistics/ping"), _i18n("ping"), 50) - _entry({"admin", "statistics", "network", "dns"}, cbi("luci_statistics/dns"), _i18n("dns"), 60) - _entry({"admin", "statistics", "network", "wireless"}, cbi("luci_statistics/wireless"), _i18n("wireless"), 70) - - -- output views - entry( { "admin", "statistics", "graph" }, call("statistics_index"), _i18n("graphs"), 80).i18n = "statistics" + local page = entry( { "admin", "statistics", "graph" }, call("statistics_index"), _i18n("graphs"), 80) + page.i18n = "statistics" + page.setuser = "nobody" + page.setgroup = "nogroup" - local vars = luci.http.formvalues() + local vars = luci.http.formvalue(nil, true) local span = vars.timespan or nil - for i, plugin in ipairs( tree:plugins() ) do + for i, plugin in luci.util.vspairs( tree:plugins() ) do -- get plugin instances local instances = tree:plugin_instances( plugin ) @@ -78,7 +97,7 @@ function index() -- if more then one instance is found then generate submenu if #instances > 1 then - for j, inst in ipairs(instances) do + for j, inst in luci.util.vspairs(instances) do -- instance menu entry entry( { "admin", "statistics", "graph", plugin, inst }, @@ -124,42 +143,34 @@ function statistics_networkplugins() end -function statistics_render( tree ) +function statistics_render() require("luci.statistics.rrdtool") require("luci.template") require("luci.model.uci") - local vars = luci.http.formvalues() - local req = luci.dispatcher.request - local uci = luci.model.uci.Session() + local vars = luci.http.formvalue() + local req = luci.dispatcher.context.request + local path = luci.dispatcher.context.path + local uci = luci.model.uci.cursor() 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] local graph = luci.statistics.rrdtool.Graph( luci.util.parse_units( span ) ) - local plugin = req[4] - local instances = { req[5] } - local images = { } + local plugin, instances + local images = { } + + -- find requested plugin and instance + for i, p in ipairs( luci.dispatcher.context.path ) do + if luci.dispatcher.context.path[i] == "graph" then + plugin = luci.dispatcher.context.path[i+1] + instances = { luci.dispatcher.context.path[i+2] } + end + end -- no instance requested, find all instances if #instances == 0 then - - instances = graph.tree:plugin_instances( plugin ) - - -- more than one available instance - if #instances > 1 then - - -- redirect to first instance and return - local r = luci.dispatcher.request - local i = instances[1] - if i:len() == 0 then i = "-" end - - luci.http.redirect( luci.dispatcher.build_url( - req[1], req[2], req[3], req[4], i - ) ) - - return - end + instances = { graph.tree:plugin_instances( plugin )[1] } -- index instance requested elseif instances[1] == "-" then