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=869384c0a693a1fc87a6c8223c791d82d65e457a;hp=f19d2cdb7fe6af7c0829f421a9c76f85785bc1c1;hb=61e5292c34a882228e1ae1a4f2b220442e825d45;hpb=1da5feb9f720fd48a886aad09df91bd8cc9df4c8 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 f19d2cdb7..869384c0a 100644 --- a/applications/luci-statistics/luasrc/controller/luci_statistics/luci_statistics.lua +++ b/applications/luci-statistics/luasrc/controller/luci_statistics/luci_statistics.lua @@ -1,178 +1,205 @@ -module("luci.controller.luci_statistics.luci_statistics", package.seeall) +--[[ -require("luci.fs") -require("luci.i18n") -require("luci.template") +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 -function index() + http://www.apache.org/licenses/LICENSE-2.0 - require("luci.i18n") - require("luci.statistics.datatree") +$Id$ - -- load language file - luci.i18n.load("statistics.en") +]]-- - -- get rrd data tree - local tree = luci.statistics.datatree.Instance() +module("luci.controller.luci_statistics.luci_statistics", package.seeall) + +function index() + + require("nixio.fs") + require("luci.util") + require("luci.statistics.datatree") -- override entry(): check for existance .so where is derived from the called path function _entry( path, ... ) - local file = path[4] or path[3] - if luci.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 - -- override call(): call requested action function with supplied parameters - function _call( func, tree, plugin ) - return function() getfenv()[func]( tree, plugin ) end - end + local labels = { + s_output = _("Output plugins"), + s_system = _("System plugins"), + s_network = _("Network plugins"), + + conntrack = _("Conntrack"), + cpu = _("Processor"), + csv = _("CSV Output"), + df = _("Disk Space Usage"), + disk = _("Disk Usage"), + dns = _("DNS"), + email = _("Email"), + exec = _("Exec"), + interface = _("Interfaces"), + iptables = _("Firewall"), + irq = _("Interrupts"), + iwinfo = _("Wireless"), + load = _("System Load"), + memory = _("Memory"), + netlink = _("Netlink"), + network = _("Network"), + nut = _("UPS"), + olsrd = _("OLSRd"), + ping = _("Ping"), + processes = _("Processes"), + rrdtool = _("RRDTool"), + tcpconns = _("TCP Connections"), + unixsock = _("UnixSock") + } - -- override i18n(): try to translate stat_ or fall back to - function _i18n( str ) - return luci.i18n.translate( "stat_" .. str, str ) + -- our collectd menu + local collectd_menu = { + output = { "csv", "network", "rrdtool", "unixsock" }, + system = { "cpu", "df", "disk", "email", "exec", "irq", "load", "memory", "nut", "processes" }, + network = { "conntrack", "dns", "interface", "iptables", "netlink", "olsrd", "ping", "tcpconns", "iwinfo" } + } + + -- create toplevel menu nodes + local st = entry({"admin", "statistics"}, template("admin_statistics/index"), _("Statistics"), 80) + st.index = true + + entry({"admin", "statistics", "collectd"}, cbi("luci_statistics/collectd"), _("Collectd"), 10).subindex = true + + + -- populate collectd plugin menu + local index = 1 + for section, plugins in luci.util.kspairs( collectd_menu ) do + local e = entry( + { "admin", "statistics", "collectd", section }, + firstchild(), labels["s_"..section], index * 10 + ) + + e.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" }, template("admin_statistics/index"), _("Graphs"), 80) + page.setuser = "nobody" + page.setgroup = "nogroup" - 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) - _entry({"admin", "statistics", "network", "wireless"}, cbi("luci_statistics/wireless"), "Drahtlos", 70) + local vars = luci.http.formvalue(nil, true) + local span = vars.timespan or nil + local host = vars.host or nil - - -- public views - entry({"freifunk", "statistics"}, call("statistics_index"), "Statistiken", 80).i18n = "statistics" + -- get rrd data tree + local tree = luci.statistics.datatree.Instance(host) - 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 ) -- 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 , host = host } -- 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 }, + { "admin", "statistics", "graph", plugin, inst }, call("statistics_render"), inst, j - ) + ).query = { timespan = span , host = host } end - end + end end end - -function statistics_index() - luci.template.render("admin_statistics/index") -end - -function statistics_outputplugins() - plugins = { - rrdtool="RRDTool", - network="Netzwerk", - unixsock="Unix Socket", - csv="CSV" - } - - 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" - } - - 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" - } - - luci.template.render("admin_statistics/networkplugins", {plugins=plugins}) -end - - -function statistics_render( tree ) +function statistics_render() 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 host = vars.host or uci:get( "luci_statistics", "collectd", "Hostname" ) or luci.sys.hostname() + local opts = { host = vars.host } + local graph = luci.statistics.rrdtool.Graph( luci.util.parse_units( span ), opts ) + local hosts = graph.tree:host_instances() + + local is_index = false + + -- 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 - local req = luci.dispatcher.request - local graph = luci.statistics.rrdtool.Graph() + local plugin, instances + local images = { } - local plugin = req[3] - local instances = { req[4] } - 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 )[1] } 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], i - ) ) - - return - end + is_index = true -- index instance requested elseif instances[1] == "-" then instances[1] = "" + is_index = true end -- render graphs for i, inst in ipairs( instances ) do - for i, img in ipairs( graph:render( plugin, inst ) ) do + for i, img in ipairs( graph:render( plugin, inst, is_index ) ) do table.insert( images, graph:strippngpath( img ) ) + images[images[#images]] = inst end end - luci.template.render("public_statistics/graph", { images=images, plugin=plugin } ) + luci.template.render( "public_statistics/graph", { + images = images, + plugin = plugin, + timespans = spans, + current_timespan = span, + hosts = hosts, + current_host = host, + is_index = is_index + } ) end