3 Luci statistics - statistics controller module
4 (c) 2008 Freifunk Leipzig / Jo-Philipp Wich <xm@leipzig.freifunk.net>
5 (c) 2012 Jo-Philipp Wich <xm@subsignal.org>
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
11 http://www.apache.org/licenses/LICENSE-2.0
15 module("luci.controller.luci_statistics.luci_statistics", package.seeall)
21 require("luci.statistics.datatree")
23 -- override entry(): check for existance <plugin>.so where <plugin> is derived from the called path
24 function _entry( path, ... )
25 local file = path[5] or path[4]
26 if nixio.fs.access( "/usr/lib/collectd/" .. file .. ".so" ) then
32 s_output = _("Output plugins"),
33 s_system = _("System plugins"),
34 s_network = _("Network plugins"),
36 conntrack = _("Conntrack"),
38 csv = _("CSV Output"),
39 df = _("Disk Space Usage"),
40 disk = _("Disk Usage"),
44 interface = _("Interfaces"),
45 iptables = _("Firewall"),
46 irq = _("Interrupts"),
47 iwinfo = _("Wireless"),
48 load = _("System Load"),
50 netlink = _("Netlink"),
51 network = _("Network"),
55 processes = _("Processes"),
56 rrdtool = _("RRDTool"),
57 splash_leases = _("Splash Leases"),
58 tcpconns = _("TCP Connections"),
59 unixsock = _("UnixSock"),
64 local collectd_menu = {
65 output = { "csv", "network", "rrdtool", "unixsock" },
66 system = { "cpu", "df", "disk", "email", "exec", "irq", "load", "memory", "nut", "processes", "uptime" },
67 network = { "conntrack", "dns", "interface", "iptables", "netlink", "olsrd", "ping", "splash_leases", "tcpconns", "iwinfo" }
70 -- create toplevel menu nodes
71 local st = entry({"admin", "statistics"}, template("admin_statistics/index"), _("Statistics"), 80)
74 entry({"admin", "statistics", "collectd"}, cbi("luci_statistics/collectd"), _("Collectd"), 10).subindex = true
77 -- populate collectd plugin menu
79 for section, plugins in luci.util.kspairs( collectd_menu ) do
81 { "admin", "statistics", "collectd", section },
82 firstchild(), labels["s_"..section], index * 10
87 for j, plugin in luci.util.vspairs( plugins ) do
89 { "admin", "statistics", "collectd", section, plugin },
90 cbi("luci_statistics/" .. plugin ),
91 labels[plugin], j * 10
99 local page = entry( { "admin", "statistics", "graph" }, template("admin_statistics/index"), _("Graphs"), 80)
100 page.setuser = "nobody"
101 page.setgroup = "nogroup"
103 local vars = luci.http.formvalue(nil, true)
104 local span = vars.timespan or nil
105 local host = vars.host or nil
108 local tree = luci.statistics.datatree.Instance(host)
111 for _, plugin, idx in luci.util.vspairs( tree:plugins() ) do
113 -- get plugin instances
114 local instances = tree:plugin_instances( plugin )
118 { "admin", "statistics", "graph", plugin },
119 call("statistics_render"), labels[plugin], idx
120 ).query = { timespan = span , host = host }
122 -- if more then one instance is found then generate submenu
123 if #instances > 1 then
125 for _, inst, idx2 in luci.util.vspairs(instances) do
126 -- instance menu entry
128 { "admin", "statistics", "graph", plugin, inst },
129 call("statistics_render"), inst, idx2
130 ).query = { timespan = span , host = host }
136 function statistics_render()
138 require("luci.statistics.rrdtool")
139 require("luci.template")
140 require("luci.model.uci")
142 local vars = luci.http.formvalue()
143 local req = luci.dispatcher.context.request
144 local path = luci.dispatcher.context.path
145 local uci = luci.model.uci.cursor()
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 host = vars.host or uci:get( "luci_statistics", "collectd", "Hostname" ) or luci.sys.hostname()
149 local opts = { host = vars.host }
150 local graph = luci.statistics.rrdtool.Graph( luci.util.parse_units( span ), opts )
151 local hosts = graph.tree:host_instances()
153 local is_index = false
154 local i, p, inst, idx
158 local l12 = require "luci.ltn12"
159 local png = io.open(graph.opts.imgpath .. "/" .. vars.img:gsub("%.+", "."), "r")
161 luci.http.prepare_content("image/png")
162 l12.pump.all(l12.source.file(png), luci.http.write)
168 local plugin, instances
171 -- find requested plugin and instance
172 for i, p in ipairs( luci.dispatcher.context.path ) do
173 if luci.dispatcher.context.path[i] == "graph" then
174 plugin = luci.dispatcher.context.path[i+1]
175 instances = { luci.dispatcher.context.path[i+2] }
179 -- no instance requested, find all instances
180 if #instances == 0 then
181 --instances = { graph.tree:plugin_instances( plugin )[1] }
182 instances = graph.tree:plugin_instances( plugin )
185 -- index instance requested
186 elseif instances[1] == "-" then
193 for i, inst in luci.util.vspairs( instances ) do
194 for i, img in luci.util.vspairs( graph:render( plugin, inst, is_index ) ) do
195 table.insert( images, graph:strippngpath( img ) )
196 images[images[#images]] = inst
200 luci.template.render( "public_statistics/graph", {
204 current_timespan = span,