-module("luci.controller.luci_statistics.luci_statistics", package.seeall)
+--[[
+
+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
-local fs = require("luci.fs")
-local i18n = require("luci.i18n")
-local tpl = require("luci.template")
-local rrd = require("luci.statistics.rrdtool")
-local data = require("luci.statistics.datatree").Instance()
+$Id$
+
+]]--
+
+module("luci.controller.luci_statistics.luci_statistics", package.seeall)
function index()
- -- XXX: fixme
- i18n.load("statistics.en")
+ require("nixio.fs")
+ require("luci.util")
+ require("luci.i18n")
+ require("luci.statistics.datatree")
+
+ -- 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 <plugin>.so where <plugin> is derived from the called path
function _entry( path, ... )
- local file = path[4] or path[3]
- if fs.isfile( "/usr/lib/collectd/" .. file .. ".so" ) then
+ local file = path[5] or path[4]
+ if nixio.fs.access( "/usr/lib/collectd/" .. file .. ".so" ) then
entry( path, ... )
end
end
- function _i18n( str )
- return i18n.translate( "stat_" .. str, str )
- end
+ local translate = luci.i18n.translate
+
+ local labels = {
+ s_output = translate("Output plugins"),
+ s_system = translate("System plugins"),
+ s_network = translate("Network plugins"),
+
+ rrdtool = translate("RRDTool"),
+ network = translate("Network"),
+ unixsock = translate("UnixSock"),
+ csv = translate("CSV Output"),
+ exec = translate("Exec"),
+ email = translate("Email"),
+ cpu = translate("Processor"),
+ df = translate("Disk Space Usage"),
+ disk = translate("Disk Usage"),
+ irq = translate("Interrupts"),
+ processes = translate("Processes"),
+ load = translate("System Load"),
+ interface = translate("Interfaces"),
+ netlink = translate("Netlink"),
+ iptables = translate("Firewall"),
+ tcpconns = translate("TCP Connections"),
+ ping = translate("Ping"),
+ dns = translate("DNS"),
+ wireless = translate("Wireless")
+ }
- entry({"admin", "statistics"}, call("statistics_index"), "Statistiken", 80)
- entry({"admin", "statistics", "collectd"}, cbi("luci_statistics/collectd"), "Collectd", 10)
-
- entry({"admin", "statistics", "output"}, call("statistics_outputplugins"), "Ausgabeplugins", 20)
- _entry({"admin", "statistics", "output", "rrdtool"}, cbi("luci_statistics/rrdtool"), "RRDTool", 10)
- _entry({"admin", "statistics", "output", "network"}, cbi("luci_statistics/network"), "Netzwerk", 20)
- _entry({"admin", "statistics", "output", "unixsock"}, cbi("luci_statistics/unixsock"), "Unix Socket", 30)
- _entry({"admin", "statistics", "output", "csv"}, cbi("luci_statistics/csv"), "CSV", 40)
-
- entry({"admin", "statistics", "system"}, call("statistics_systemplugins"), "Systemplugins", 30)
- _entry({"admin", "statistics", "system", "exec"}, cbi("luci_statistics/exec"), "Exec", 10)
- _entry({"admin", "statistics", "system", "email"}, cbi("luci_statistics/email"), "E-Mail", 20)
- _entry({"admin", "statistics", "system", "cpu"}, cbi("luci_statistics/cpu"), "Prozessor", 30)
- _entry({"admin", "statistics", "system", "df"}, cbi("luci_statistics/df"), "Speicherplatz", 40)
- _entry({"admin", "statistics", "system", "disk"}, cbi("luci_statistics/disk"), "Datenträger", 50)
- _entry({"admin", "statistics", "system", "irq"}, cbi("luci_statistics/irq"), "Interrupts", 60)
- _entry({"admin", "statistics", "system", "processes"}, cbi("luci_statistics/processes"), "Prozesse", 70)
-
- entry({"admin", "statistics", "network"}, call("statistics_networkplugins"), "Netzwerkplugins", 40)
- _entry({"admin", "statistics", "network", "interface"}, cbi("luci_statistics/interface"), "Schnittstellen", 10)
- _entry({"admin", "statistics", "network", "netlink"}, cbi("luci_statistics/netlink"), "Netlink", 20)
- _entry({"admin", "statistics", "network", "iptables"}, cbi("luci_statistics/iptables"), "Firewall", 30)
- _entry({"admin", "statistics", "network", "tcpconns"}, cbi("luci_statistics/tcpconns"), "Verbindungen", 40)
- _entry({"admin", "statistics", "network", "ping"}, cbi("luci_statistics/ping"), "Ping", 50)
- _entry({"admin", "statistics", "network", "dns"}, cbi("luci_statistics/dns"), "DNS", 60)
+ -- 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
+ local st = entry({"admin", "statistics"}, call("statistics_index"), translate("Statistics"), 80)
+ st.i18n = "statistics"
+ st.index = true
- -- public views
- entry({"freifunk", "statistics"}, call("statistics_index"), "Statistiken", 80).i18n = "statistics"
+ entry({"admin", "statistics", "collectd"}, cbi("luci_statistics/collectd"), translate("Collectd"), 10).subindex = true
- for i, plugin in ipairs( data:plugins() ) do
+
+ -- 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" ),
+ labels["s_"..section], index * 10
+ ).index = true
+
+ for j, plugin in luci.util.vspairs( plugins ) do
+ _entry(
+ { "admin", "statistics", "collectd", section, plugin },
+ cbi("luci_statistics/" .. plugin ),
+ labels[plugin], j * 10
+ )
+ end
+
+ index = index + 1
+ end
+
+ -- output views
+ local page = entry( { "admin", "statistics", "graph" }, call("statistics_index"), translate("Graphs"), 80)
+ page.i18n = "statistics"
+ page.setuser = "nobody"
+ page.setgroup = "nogroup"
+
+ local vars = luci.http.formvalue(nil, true)
+ local span = vars.timespan or nil
+
+ for i, plugin in luci.util.vspairs( tree:plugins() ) do
-- get plugin instances
- local instances = data:plugin_instances( plugin )
+ local instances = tree:plugin_instances( plugin )
-- plugin menu entry
- _entry( { "freifunk", "statistics", plugin }, call("statistics_render"), _i18n( plugin ), i )
+ entry(
+ { "admin", "statistics", "graph", plugin },
+ call("statistics_render"), labels[plugin], i
+ ).query = { timespan = span }
-- 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( { "freifunk", "statistics", plugin, inst }, call("statistics_render"), inst, j )
+ entry(
+ { "admin", "statistics", "graph", plugin, inst },
+ call("statistics_render"), inst, j
+ ).query = { timespan = span }
end
- end
+ end
end
end
-
function statistics_index()
- tpl.render("admin_statistics/index")
+ luci.template.render("admin_statistics/index")
end
function statistics_outputplugins()
- plugins = {
- rrdtool="RRDTool",
- network="Netzwerk",
- unixsock="Unix Socket",
- csv="CSV"
+ local translate = luci.i18n.translate
+ local plugins = {
+ rrdtool = translate("RRDTool"),
+ network = translate("Network"),
+ unixsock = translate("UnixSock"),
+ csv = translate("CSV Output")
}
- tpl.render("admin_statistics/outputplugins", {plugins=plugins})
+ luci.template.render("admin_statistics/outputplugins", {plugins=plugins})
end
function statistics_systemplugins()
- plugins = {
- exec="Exec",
- email="E-Mail",
- disk="Datenträger",
- irq="Interrupts",
- processes="Prozesse"
+ local translate = luci.i18n.translate
+ local plugins = {
+ exec = translate("Exec"),
+ email = translate("Email"),
+ cpu = translate("Processor"),
+ df = translate("Disk Space Usage"),
+ disk = translate("Disk Usage"),
+ irq = translate("Interrupts"),
+ processes = translate("Processes"),
+ load = translate("System Load"),
}
- tpl.render("admin_statistics/systemplugins", {plugins=plugins})
+ luci.template.render("admin_statistics/systemplugins", {plugins=plugins})
end
function statistics_networkplugins()
- plugins = {
- interface="Schnittstellen",
- netlink="Netlink",
- iptables="Firewall",
- tcpconns="Verbindungen",
- ping="Ping",
- dns="DNS"
+ local translate = luci.i18n.translate
+ local plugins = {
+ interface = translate("Interfaces"),
+ netlink = translate("Netlink"),
+ iptables = translate("Firewall"),
+ tcpconns = translate("TCP Connections"),
+ ping = translate("Ping"),
+ dns = translate("DNS"),
+ wireless = translate("Wireless")
}
- tpl.render("admin_statistics/networkplugins", {plugins=plugins})
+ luci.template.render("admin_statistics/networkplugins", {plugins=plugins})
end
function statistics_render()
- local plugin = luci.dispatcher.request[3]
- local instances = { luci.dispatcher.request[4] }
- local images = { }
-
- -- no instance requested, find all instances
- if #instances == 0 then
- instances = data:plugin_instances( plugin )
+ require("luci.statistics.rrdtool")
+ require("luci.template")
+ require("luci.model.uci")
+
+ 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 ) )
+
+ -- deliver image
+ if vars.img then
+ local l12 = require "luci.ltn12"
+ local png = io.open(graph.opts.imgpath .. "/" .. vars.img:gsub("%.+", "."), "r")
+ if png then
+ luci.http.prepare_content("image/png")
+ l12.pump.all(l12.source.file(png), luci.http.write)
+ png:close()
+ end
+ return
+ end
- -- more than one available instance
- if #instances > 1 then
+ local plugin, instances
+ local images = { }
- -- redirect to first instance and return
- local r = luci.dispatcher.request
+ -- 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
- luci.http.redirect( luci.dispatcher.build_url(
- r[1], r[2], r[3], instances[1]
- ) )
+ -- no instance requested, find all instances
+ if #instances == 0 then
+ instances = { graph.tree:plugin_instances( plugin )[1] }
- return
- end
+ -- index instance requested
+ elseif instances[1] == "-" then
+ instances[1] = ""
end
+
-- render graphs
for i, inst in ipairs( instances ) do
- local graph = rrd.Graph()
for i, img in ipairs( graph:render( plugin, inst ) ) do
- table.insert( images, img )
+ table.insert( images, graph:strippngpath( img ) )
end
end
- tpl.render("public_statistics/graph", { images=images, plugin=plugin } )
+ luci.template.render( "public_statistics/graph", {
+ images = images,
+ plugin = plugin,
+ timespans = spans,
+ current_timespan = span
+ } )
end