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")
24 -- override entry(): check for existance <plugin>.so where <plugin> is derived from the called path
25 function _entry( path, ... )
26 local file = path[5] or path[4]
27 if nixio.fs.access( "/usr/lib/collectd/" .. file .. ".so" ) then
33 s_output = _("Output plugins"),
34 s_system = _("System plugins"),
35 s_network = _("Network plugins"),
37 conntrack = _("Conntrack"),
39 csv = _("CSV Output"),
40 df = _("Disk Space Usage"),
41 disk = _("Disk Usage"),
45 interface = _("Interfaces"),
46 iptables = _("Firewall"),
47 irq = _("Interrupts"),
48 iwinfo = _("Wireless"),
49 load = _("System Load"),
51 netlink = _("Netlink"),
52 network = _("Network"),
56 processes = _("Processes"),
57 rrdtool = _("RRDTool"),
58 tcpconns = _("TCP Connections"),
59 unixsock = _("UnixSock")
63 local collectd_menu = {
64 output = { "csv", "network", "rrdtool", "unixsock" },
65 system = { "cpu", "df", "disk", "email", "exec", "irq", "load", "memory", "nut", "processes" },
66 network = { "conntrack", "dns", "interface", "iptables", "netlink", "olsrd", "ping", "tcpconns", "iwinfo" }
69 -- create toplevel menu nodes
70 local st = entry({"admin", "statistics"}, template("admin_statistics/index"), _("Statistics"), 80)
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 firstchild(), labels["s_"..section], index * 10
86 for j, plugin in luci.util.vspairs( plugins ) do
88 { "admin", "statistics", "collectd", section, plugin },
89 cbi("luci_statistics/" .. plugin ),
90 labels[plugin], j * 10
98 local page = entry( { "admin", "statistics", "graph" }, template("admin_statistics/index"), _("Graphs"), 80)
99 page.setuser = "nobody"
100 page.setgroup = "nogroup"
102 local vars = luci.http.formvalue(nil, true)
103 local span = vars.timespan or nil
104 local host = vars.host or nil
107 local tree = luci.statistics.datatree.Instance(host)
109 for i, plugin in luci.util.vspairs( tree:plugins() ) do
111 -- get plugin instances
112 local instances = tree:plugin_instances( plugin )
116 { "admin", "statistics", "graph", plugin },
117 call("statistics_render"), labels[plugin], i
118 ).query = { timespan = span , host = host }
120 -- if more then one instance is found then generate submenu
121 if #instances > 1 then
122 for j, inst in luci.util.vspairs(instances) do
123 -- instance menu entry
125 { "admin", "statistics", "graph", plugin, inst },
126 call("statistics_render"), inst, j
127 ).query = { timespan = span , host = host }
133 function statistics_render()
135 require("luci.statistics.rrdtool")
136 require("luci.template")
137 require("luci.model.uci")
139 local vars = luci.http.formvalue()
140 local req = luci.dispatcher.context.request
141 local path = luci.dispatcher.context.path
142 local uci = luci.model.uci.cursor()
143 local spans = luci.util.split( uci:get( "luci_statistics", "collectd_rrdtool", "RRATimespans" ), "%s+", nil, true )
144 local span = vars.timespan or uci:get( "luci_statistics", "rrdtool", "default_timespan" ) or spans[1]
145 local host = vars.host or uci:get( "luci_statistics", "collectd", "Hostname" ) or luci.sys.hostname()
146 local opts = { host = vars.host }
147 local graph = luci.statistics.rrdtool.Graph( luci.util.parse_units( span ), opts )
148 local hosts = graph.tree:host_instances()
150 local is_index = false
154 local l12 = require "luci.ltn12"
155 local png = io.open(graph.opts.imgpath .. "/" .. vars.img:gsub("%.+", "."), "r")
157 luci.http.prepare_content("image/png")
158 l12.pump.all(l12.source.file(png), luci.http.write)
164 local plugin, instances
167 -- find requested plugin and instance
168 for i, p in ipairs( luci.dispatcher.context.path ) do
169 if luci.dispatcher.context.path[i] == "graph" then
170 plugin = luci.dispatcher.context.path[i+1]
171 instances = { luci.dispatcher.context.path[i+2] }
175 -- no instance requested, find all instances
176 if #instances == 0 then
177 --instances = { graph.tree:plugin_instances( plugin )[1] }
178 instances = graph.tree:plugin_instances( plugin )
181 -- index instance requested
182 elseif instances[1] == "-" then
189 for i, inst in ipairs( instances ) do
190 for i, img in ipairs( graph:render( plugin, inst, is_index ) ) do
191 table.insert( images, graph:strippngpath( img ) )
192 images[images[#images]] = inst
196 luci.template.render( "public_statistics/graph", {
200 current_timespan = span,