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 local tree = luci.statistics.datatree.Instance()
27 -- override entry(): check for existance <plugin>.so where <plugin> is derived from the called path
28 function _entry( path, ... )
29 local file = path[5] or path[4]
30 if nixio.fs.access( "/usr/lib/collectd/" .. file .. ".so" ) then
36 s_output = _("Output plugins"),
37 s_system = _("System plugins"),
38 s_network = _("Network plugins"),
40 rrdtool = _("RRDTool"),
41 network = _("Network"),
42 unixsock = _("UnixSock"),
43 csv = _("CSV Output"),
47 df = _("Disk Space Usage"),
48 disk = _("Disk Usage"),
49 irq = _("Interrupts"),
50 processes = _("Processes"),
51 load = _("System Load"),
52 interface = _("Interfaces"),
53 netlink = _("Netlink"),
54 iptables = _("Firewall"),
55 tcpconns = _("TCP Connections"),
58 wireless = _("Wireless")
62 local collectd_menu = {
63 output = { "rrdtool", "network", "unixsock", "csv" },
64 system = { "exec", "email", "cpu", "df", "disk", "irq", "processes", "load" },
65 network = { "interface", "netlink", "iptables", "tcpconns", "ping", "dns", "wireless" }
68 -- create toplevel menu nodes
69 local st = entry({"admin", "statistics"}, call("statistics_index"), _("Statistics"), 80)
70 st.i18n = "statistics"
73 entry({"admin", "statistics", "collectd"}, cbi("luci_statistics/collectd"), _("Collectd"), 10).subindex = true
76 -- populate collectd plugin menu
78 for section, plugins in luci.util.kspairs( collectd_menu ) do
80 { "admin", "statistics", "collectd", section },
81 call( "statistics_" .. section .. "plugins" ),
82 labels["s_"..section], index * 10
88 for j, plugin in luci.util.vspairs( plugins ) do
90 { "admin", "statistics", "collectd", section, plugin },
91 cbi("luci_statistics/" .. plugin ),
92 labels[plugin], j * 10
100 local page = entry( { "admin", "statistics", "graph" }, call("statistics_index"), _("Graphs"), 80)
101 page.i18n = "statistics"
102 page.setuser = "nobody"
103 page.setgroup = "nogroup"
105 local vars = luci.http.formvalue(nil, true)
106 local span = vars.timespan or nil
108 for i, plugin in luci.util.vspairs( tree:plugins() ) do
110 -- get plugin instances
111 local instances = tree:plugin_instances( plugin )
115 { "admin", "statistics", "graph", plugin },
116 call("statistics_render"), labels[plugin], i
117 ).query = { timespan = span }
119 -- if more then one instance is found then generate submenu
120 if #instances > 1 then
121 for j, inst in luci.util.vspairs(instances) do
122 -- instance menu entry
124 { "admin", "statistics", "graph", plugin, inst },
125 call("statistics_render"), inst, j
126 ).query = { timespan = span }
132 function statistics_index()
133 luci.template.render("admin_statistics/index")
136 function statistics_outputplugins()
137 local translate = luci.i18n.translate
139 rrdtool = _("RRDTool"),
140 network = _("Network"),
141 unixsock = _("UnixSock"),
142 csv = _("CSV Output")
145 luci.template.render("admin_statistics/outputplugins", {plugins=plugins})
148 function statistics_systemplugins()
149 local translate = luci.i18n.translate
153 cpu = _("Processor"),
154 df = _("Disk Space Usage"),
155 disk = _("Disk Usage"),
156 irq = _("Interrupts"),
157 processes = _("Processes"),
158 load = _("System Load"),
161 luci.template.render("admin_statistics/systemplugins", {plugins=plugins})
164 function statistics_networkplugins()
165 local translate = luci.i18n.translate
167 interface = _("Interfaces"),
168 netlink = _("Netlink"),
169 iptables = _("Firewall"),
170 tcpconns = _("TCP Connections"),
173 wireless = _("Wireless")
176 luci.template.render("admin_statistics/networkplugins", {plugins=plugins})
180 function statistics_render()
182 require("luci.statistics.rrdtool")
183 require("luci.template")
184 require("luci.model.uci")
186 local vars = luci.http.formvalue()
187 local req = luci.dispatcher.context.request
188 local path = luci.dispatcher.context.path
189 local uci = luci.model.uci.cursor()
190 local spans = luci.util.split( uci:get( "luci_statistics", "collectd_rrdtool", "RRATimespans" ), "%s+", nil, true )
191 local span = vars.timespan or uci:get( "luci_statistics", "rrdtool", "default_timespan" ) or spans[1]
192 local graph = luci.statistics.rrdtool.Graph( luci.util.parse_units( span ) )
196 local l12 = require "luci.ltn12"
197 local png = io.open(graph.opts.imgpath .. "/" .. vars.img:gsub("%.+", "."), "r")
199 luci.http.prepare_content("image/png")
200 l12.pump.all(l12.source.file(png), luci.http.write)
206 local plugin, instances
209 -- find requested plugin and instance
210 for i, p in ipairs( luci.dispatcher.context.path ) do
211 if luci.dispatcher.context.path[i] == "graph" then
212 plugin = luci.dispatcher.context.path[i+1]
213 instances = { luci.dispatcher.context.path[i+2] }
217 -- no instance requested, find all instances
218 if #instances == 0 then
219 instances = { graph.tree:plugin_instances( plugin )[1] }
221 -- index instance requested
222 elseif instances[1] == "-" then
228 for i, inst in ipairs( instances ) do
229 for i, img in ipairs( graph:render( plugin, inst ) ) do
230 table.insert( images, graph:strippngpath( img ) )
234 luci.template.render( "public_statistics/graph", {
238 current_timespan = span