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)
22 require("luci.statistics.datatree")
25 luci.i18n.load("statistics.en") -- XXX: temporary / replace with loadc()
28 local tree = luci.statistics.datatree.Instance()
30 -- override entry(): check for existance <plugin>.so where <plugin> is derived from the called path
31 function _entry( path, ... )
32 local file = path[4] or path[3]
33 if luci.fs.isfile( "/usr/lib/collectd/" .. file .. ".so" ) then
38 -- override i18n(): try to translate stat_<str> or fall back to <str>
40 return luci.i18n.translate( "stat_" .. str, str )
44 entry({"admin", "statistics"}, call("statistics_index"), _i18n("statistics"), 80).i18n = "statistics"
45 entry({"admin", "statistics", "collectd"}, cbi("luci_statistics/collectd"), _i18n("collectd"), 10)
47 entry({"admin", "statistics", "output"}, call("statistics_outputplugins"), _i18n("outputplugins"), 20)
48 _entry({"admin", "statistics", "output", "rrdtool"}, cbi("luci_statistics/rrdtool"), _i18n("rrdtool"), 10)
49 _entry({"admin", "statistics", "output", "network"}, cbi("luci_statistics/network"), _i18n("network"), 20)
50 _entry({"admin", "statistics", "output", "unixsock"}, cbi("luci_statistics/unixsock"), _i18n("unixsock"), 30)
51 _entry({"admin", "statistics", "output", "csv"}, cbi("luci_statistics/csv"), _i18n("csv"), 40)
53 entry({"admin", "statistics", "system"}, call("statistics_systemplugins"), _i18n("systemplugins"), 30)
54 _entry({"admin", "statistics", "system", "exec"}, cbi("luci_statistics/exec"), _i18n("exec"), 10)
55 _entry({"admin", "statistics", "system", "email"}, cbi("luci_statistics/email"), _i18n("email"), 20)
56 _entry({"admin", "statistics", "system", "cpu"}, cbi("luci_statistics/cpu"), _i18n("cpu"), 30)
57 _entry({"admin", "statistics", "system", "df"}, cbi("luci_statistics/df"), _i18n("df"), 40)
58 _entry({"admin", "statistics", "system", "disk"}, cbi("luci_statistics/disk"), _i18n("disk"), 50)
59 _entry({"admin", "statistics", "system", "irq"}, cbi("luci_statistics/irq"), _i18n("irq"), 60)
60 _entry({"admin", "statistics", "system", "processes"}, cbi("luci_statistics/processes"), _i18n("processes"), 70)
62 entry({"admin", "statistics", "network"}, call("statistics_networkplugins"), _i18n("networkplugins"),40)
63 _entry({"admin", "statistics", "network", "interface"}, cbi("luci_statistics/interface"), _i18n("interface"), 10)
64 _entry({"admin", "statistics", "network", "netlink"}, cbi("luci_statistics/netlink"), _i18n("netlink"), 20)
65 _entry({"admin", "statistics", "network", "iptables"}, cbi("luci_statistics/iptables"), _i18n("iptables"), 30)
66 _entry({"admin", "statistics", "network", "tcpconns"}, cbi("luci_statistics/tcpconns"), _i18n("tcpconns"), 40)
67 _entry({"admin", "statistics", "network", "ping"}, cbi("luci_statistics/ping"), _i18n("ping"), 50)
68 _entry({"admin", "statistics", "network", "dns"}, cbi("luci_statistics/dns"), _i18n("dns"), 60)
69 _entry({"admin", "statistics", "network", "wireless"}, cbi("luci_statistics/wireless"), _i18n("wireless"), 70)
73 entry( { "admin", "statistics", "graph" }, call("statistics_index"), _i18n("graphs"), 80).i18n = "statistics"
75 local vars = luci.http.formvalues()
76 local span = vars.timespan or nil
78 for i, plugin in ipairs( tree:plugins() ) do
80 -- get plugin instances
81 local instances = tree:plugin_instances( plugin )
85 { "admin", "statistics", "graph", plugin },
86 call("statistics_render"), _i18n( plugin ), i
87 ).query = { timespan = span }
89 -- if more then one instance is found then generate submenu
90 if #instances > 1 then
91 for j, inst in ipairs(instances) do
92 -- instance menu entry
94 { "admin", "statistics", "graph", plugin, inst },
95 call("statistics_render"), inst, j
96 ).query = { timespan = span }
102 function statistics_index()
103 luci.template.render("admin_statistics/index")
106 function statistics_outputplugins()
109 for i, p in ipairs({ "rrdtool", "network", "unixsock", "csv" }) do
110 plugins[p] = luci.i18n.translate( "stat_" .. p, p )
113 luci.template.render("admin_statistics/outputplugins", {plugins=plugins})
116 function statistics_systemplugins()
119 for i, p in ipairs({ "exec", "email", "df", "disk", "irq", "processes", "cpu" }) do
120 plugins[p] = luci.i18n.translate( "stat_" .. p, p )
123 luci.template.render("admin_statistics/systemplugins", {plugins=plugins})
126 function statistics_networkplugins()
129 for i, p in ipairs({ "interface", "netlink", "iptables", "tcpconns", "ping", "dns", "wireless" }) do
130 plugins[p] = luci.i18n.translate( "stat_" .. p, p )
133 luci.template.render("admin_statistics/networkplugins", {plugins=plugins})
137 function statistics_render()
139 require("luci.statistics.rrdtool")
140 require("luci.template")
141 require("luci.model.uci")
143 local vars = luci.http.formvalues()
144 local req = luci.dispatcher.request
145 local uci = luci.model.uci.Session()
146 local spans = luci.util.split( uci:get( "luci_statistics", "collectd_rrdtool", "RRATimespans" ), "%s+", nil, true )
147 local span = vars.timespan or uci:get( "luci_statistics", "rrdtool", "default_timespan" ) or spans[1]
148 local graph = luci.statistics.rrdtool.Graph( luci.util.parse_units( span ) )
150 local plugin = req[4]
151 local instances = { req[5] }
154 -- no instance requested, find all instances
155 if #instances == 0 then
157 instances = graph.tree:plugin_instances( plugin )
159 -- more than one available instance
160 if #instances > 1 then
162 -- redirect to first instance and return
163 local r = luci.dispatcher.request
164 local i = instances[1]
165 if i:len() == 0 then i = "-" end
167 luci.http.redirect( luci.dispatcher.build_url(
168 req[1], req[2], req[3], req[4], i
174 -- index instance requested
175 elseif instances[1] == "-" then
181 for i, inst in ipairs( instances ) do
182 for i, img in ipairs( graph:render( plugin, inst ) ) do
183 table.insert( images, graph:strippngpath( img ) )
187 luci.template.render( "public_statistics/graph", {
191 current_timespan = span