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 conntrack = _("Conntrack"),
42 csv = _("CSV Output"),
43 df = _("Disk Space Usage"),
44 disk = _("Disk Usage"),
48 interface = _("Interfaces"),
49 iptables = _("Firewall"),
50 irq = _("Interrupts"),
51 iwinfo = _("Wireless"),
52 load = _("System Load"),
54 netlink = _("Netlink"),
55 network = _("Network"),
58 processes = _("Processes"),
59 rrdtool = _("RRDTool"),
60 tcpconns = _("TCP Connections"),
61 unixsock = _("UnixSock"),
62 wireless = _("Wireless")
66 local collectd_menu = {
67 output = { "csv", "network", "rrdtool", "unixsock" },
68 system = { "cpu", "df", "disk", "email", "exec", "irq", "load", "memory", "processes" },
69 network = { "conntrack", "dns", "interface", "iptables", "netlink", "olsrd", "ping", "tcpconns", "wireless", "iwinfo" }
72 -- create toplevel menu nodes
73 local st = entry({"admin", "statistics"}, call("statistics_index"), _("Statistics"), 80)
74 st.i18n = "statistics"
77 entry({"admin", "statistics", "collectd"}, cbi("luci_statistics/collectd"), _("Collectd"), 10).subindex = true
80 -- populate collectd plugin menu
82 for section, plugins in luci.util.kspairs( collectd_menu ) do
84 { "admin", "statistics", "collectd", section },
85 firstchild(), labels["s_"..section], index * 10
91 for j, plugin in luci.util.vspairs( plugins ) do
93 { "admin", "statistics", "collectd", section, plugin },
94 cbi("luci_statistics/" .. plugin ),
95 labels[plugin], j * 10
103 local page = entry( { "admin", "statistics", "graph" }, call("statistics_index"), _("Graphs"), 80)
104 page.i18n = "statistics"
105 page.setuser = "nobody"
106 page.setgroup = "nogroup"
108 local vars = luci.http.formvalue(nil, true)
109 local span = vars.timespan or nil
111 for i, plugin in luci.util.vspairs( tree:plugins() ) do
113 -- get plugin instances
114 local instances = tree:plugin_instances( plugin )
118 { "admin", "statistics", "graph", plugin },
119 template("admin_statistics/index"), labels[plugin], i
120 ).query = { timespan = span }
122 -- if more then one instance is found then generate submenu
123 if #instances > 1 then
124 for j, inst in luci.util.vspairs(instances) do
125 -- instance menu entry
127 { "admin", "statistics", "graph", plugin, inst },
128 call("statistics_render"), inst, j
129 ).query = { timespan = span }
135 function statistics_render()
137 require("luci.statistics.rrdtool")
138 require("luci.template")
139 require("luci.model.uci")
141 local vars = luci.http.formvalue()
142 local req = luci.dispatcher.context.request
143 local path = luci.dispatcher.context.path
144 local uci = luci.model.uci.cursor()
145 local spans = luci.util.split( uci:get( "luci_statistics", "collectd_rrdtool", "RRATimespans" ), "%s+", nil, true )
146 local span = vars.timespan or uci:get( "luci_statistics", "rrdtool", "default_timespan" ) or spans[1]
147 local graph = luci.statistics.rrdtool.Graph( luci.util.parse_units( span ) )
149 local is_index = false
153 local l12 = require "luci.ltn12"
154 local png = io.open(graph.opts.imgpath .. "/" .. vars.img:gsub("%.+", "."), "r")
156 luci.http.prepare_content("image/png")
157 l12.pump.all(l12.source.file(png), luci.http.write)
163 local plugin, instances
166 -- find requested plugin and instance
167 for i, p in ipairs( luci.dispatcher.context.path ) do
168 if luci.dispatcher.context.path[i] == "graph" then
169 plugin = luci.dispatcher.context.path[i+1]
170 instances = { luci.dispatcher.context.path[i+2] }
174 -- no instance requested, find all instances
175 if #instances == 0 then
176 --instances = { graph.tree:plugin_instances( plugin )[1] }
177 instances = graph.tree:plugin_instances( plugin )
180 -- index instance requested
181 elseif instances[1] == "-" then
188 for i, inst in ipairs( instances ) do
189 for i, img in ipairs( graph:render( plugin, inst, is_index ) ) do
190 table.insert( images, graph:strippngpath( img ) )
191 images[images[#images]] = inst
195 luci.template.render( "public_statistics/graph", {
199 current_timespan = span,