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)
23 require("luci.statistics.datatree")
25 -- load language files
26 luci.i18n.loadc("rrdtool")
27 luci.i18n.loadc("statistics")
30 local tree = luci.statistics.datatree.Instance()
32 -- override entry(): check for existance <plugin>.so where <plugin> is derived from the called path
33 function _entry( path, ... )
34 local file = path[5] or path[4]
35 if nixio.fs.access( "/usr/lib/collectd/" .. file .. ".so" ) then
40 local translate = luci.i18n.translate
43 s_output = translate("Output plugins"),
44 s_system = translate("System plugins"),
45 s_network = translate("Network plugins"),
47 rrdtool = translate("RRDTool"),
48 network = translate("Network"),
49 unixsock = translate("UnixSock"),
50 csv = translate("CSV Output"),
51 exec = translate("Exec"),
52 email = translate("Email"),
53 cpu = translate("Processor"),
54 df = translate("Disk Space Usage"),
55 disk = translate("Disk Usage"),
56 irq = translate("Interrupts"),
57 processes = translate("Processes"),
58 load = translate("System Load"),
59 interface = translate("Interfaces"),
60 netlink = translate("Netlink"),
61 iptables = translate("Firewall"),
62 tcpconns = translate("TCP Connections"),
63 ping = translate("Ping"),
64 dns = translate("DNS"),
65 wireless = translate("Wireless")
69 local collectd_menu = {
70 output = { "rrdtool", "network", "unixsock", "csv" },
71 system = { "exec", "email", "cpu", "df", "disk", "irq", "processes", "load" },
72 network = { "interface", "netlink", "iptables", "tcpconns", "ping", "dns", "wireless" }
75 -- create toplevel menu nodes
76 local st = entry({"admin", "statistics"}, call("statistics_index"), translate("Statistics"), 80)
77 st.i18n = "statistics"
80 entry({"admin", "statistics", "collectd"}, cbi("luci_statistics/collectd"), translate("Collectd"), 10).subindex = true
83 -- populate collectd plugin menu
85 for section, plugins in luci.util.kspairs( collectd_menu ) do
87 { "admin", "statistics", "collectd", section },
88 call( "statistics_" .. section .. "plugins" ),
89 labels["s_"..section], index * 10
92 for j, plugin in luci.util.vspairs( plugins ) do
94 { "admin", "statistics", "collectd", section, plugin },
95 cbi("luci_statistics/" .. plugin ),
96 labels[plugin], j * 10
104 local page = entry( { "admin", "statistics", "graph" }, call("statistics_index"), translate("Graphs"), 80)
105 page.i18n = "statistics"
106 page.setuser = "nobody"
107 page.setgroup = "nogroup"
109 local vars = luci.http.formvalue(nil, true)
110 local span = vars.timespan or nil
112 for i, plugin in luci.util.vspairs( tree:plugins() ) do
114 -- get plugin instances
115 local instances = tree:plugin_instances( plugin )
119 { "admin", "statistics", "graph", plugin },
120 call("statistics_render"), labels[plugin], i
121 ).query = { timespan = span }
123 -- if more then one instance is found then generate submenu
124 if #instances > 1 then
125 for j, inst in luci.util.vspairs(instances) do
126 -- instance menu entry
128 { "admin", "statistics", "graph", plugin, inst },
129 call("statistics_render"), inst, j
130 ).query = { timespan = span }
136 function statistics_index()
137 luci.template.render("admin_statistics/index")
140 function statistics_outputplugins()
141 local translate = luci.i18n.translate
143 rrdtool = translate("RRDTool"),
144 network = translate("Network"),
145 unixsock = translate("UnixSock"),
146 csv = translate("CSV Output")
149 luci.template.render("admin_statistics/outputplugins", {plugins=plugins})
152 function statistics_systemplugins()
153 local translate = luci.i18n.translate
155 exec = translate("Exec"),
156 email = translate("Email"),
157 cpu = translate("Processor"),
158 df = translate("Disk Space Usage"),
159 disk = translate("Disk Usage"),
160 irq = translate("Interrupts"),
161 processes = translate("Processes"),
162 load = translate("System Load"),
165 luci.template.render("admin_statistics/systemplugins", {plugins=plugins})
168 function statistics_networkplugins()
169 local translate = luci.i18n.translate
171 interface = translate("Interfaces"),
172 netlink = translate("Netlink"),
173 iptables = translate("Firewall"),
174 tcpconns = translate("TCP Connections"),
175 ping = translate("Ping"),
176 dns = translate("DNS"),
177 wireless = translate("Wireless")
180 luci.template.render("admin_statistics/networkplugins", {plugins=plugins})
184 function statistics_render()
186 require("luci.statistics.rrdtool")
187 require("luci.template")
188 require("luci.model.uci")
190 local vars = luci.http.formvalue()
191 local req = luci.dispatcher.context.request
192 local path = luci.dispatcher.context.path
193 local uci = luci.model.uci.cursor()
194 local spans = luci.util.split( uci:get( "luci_statistics", "collectd_rrdtool", "RRATimespans" ), "%s+", nil, true )
195 local span = vars.timespan or uci:get( "luci_statistics", "rrdtool", "default_timespan" ) or spans[1]
196 local graph = luci.statistics.rrdtool.Graph( luci.util.parse_units( span ) )
200 local l12 = require "luci.ltn12"
201 local png = io.open(graph.opts.imgpath .. "/" .. vars.img:gsub("%.+", "."), "r")
203 luci.http.prepare_content("image/png")
204 l12.pump.all(l12.source.file(png), luci.http.write)
210 local plugin, instances
213 -- find requested plugin and instance
214 for i, p in ipairs( luci.dispatcher.context.path ) do
215 if luci.dispatcher.context.path[i] == "graph" then
216 plugin = luci.dispatcher.context.path[i+1]
217 instances = { luci.dispatcher.context.path[i+2] }
221 -- no instance requested, find all instances
222 if #instances == 0 then
223 instances = { graph.tree:plugin_instances( plugin )[1] }
225 -- index instance requested
226 elseif instances[1] == "-" then
232 for i, inst in ipairs( instances ) do
233 for i, img in ipairs( graph:render( plugin, inst ) ) do
234 table.insert( images, graph:strippngpath( img ) )
238 luci.template.render( "public_statistics/graph", {
242 current_timespan = span