applications/luci-statistics: fix controller (#7344)
[project/luci.git] / applications / luci-statistics / luasrc / controller / luci_statistics / luci_statistics.lua
1 --[[
2
3 Luci statistics - statistics controller module
4 (c) 2008 Freifunk Leipzig / Jo-Philipp Wich <xm@leipzig.freifunk.net>
5
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
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 $Id$
13
14 ]]--
15
16 module("luci.controller.luci_statistics.luci_statistics", package.seeall)
17
18 function index()
19
20 require("nixio.fs")
21 require("luci.util")
22 require("luci.statistics.datatree")
23
24 -- get rrd data tree
25 local tree = luci.statistics.datatree.Instance()
26
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
31 entry( path, ... )
32 end
33 end
34
35 local labels = {
36 s_output = _("Output plugins"),
37 s_system = _("System plugins"),
38 s_network = _("Network plugins"),
39
40 conntrack = _("Conntrack"),
41 cpu = _("Processor"),
42 csv = _("CSV Output"),
43 df = _("Disk Space Usage"),
44 disk = _("Disk Usage"),
45 dns = _("DNS"),
46 email = _("Email"),
47 exec = _("Exec"),
48 interface = _("Interfaces"),
49 iptables = _("Firewall"),
50 irq = _("Interrupts"),
51 iwinfo = _("Wireless"),
52 load = _("System Load"),
53 memory = _("Memory"),
54 netlink = _("Netlink"),
55 network = _("Network"),
56 olsrd = _("OLSRd"),
57 ping = _("Ping"),
58 processes = _("Processes"),
59 rrdtool = _("RRDTool"),
60 tcpconns = _("TCP Connections"),
61 unixsock = _("UnixSock")
62 }
63
64 -- our collectd menu
65 local collectd_menu = {
66 output = { "csv", "network", "rrdtool", "unixsock" },
67 system = { "cpu", "df", "disk", "email", "exec", "irq", "load", "memory", "processes" },
68 network = { "conntrack", "dns", "interface", "iptables", "netlink", "olsrd", "ping", "tcpconns", "iwinfo" }
69 }
70
71 -- create toplevel menu nodes
72 local st = entry({"admin", "statistics"}, template("admin_statistics/index"), _("Statistics"), 80)
73 st.i18n = "statistics"
74 st.index = true
75
76 entry({"admin", "statistics", "collectd"}, cbi("luci_statistics/collectd"), _("Collectd"), 10).subindex = true
77
78
79 -- populate collectd plugin menu
80 local index = 1
81 for section, plugins in luci.util.kspairs( collectd_menu ) do
82 local e = entry(
83 { "admin", "statistics", "collectd", section },
84 firstchild(), labels["s_"..section], index * 10
85 )
86
87 e.index = true
88 e.i18n = "rrdtool"
89
90 for j, plugin in luci.util.vspairs( plugins ) do
91 _entry(
92 { "admin", "statistics", "collectd", section, plugin },
93 cbi("luci_statistics/" .. plugin ),
94 labels[plugin], j * 10
95 )
96 end
97
98 index = index + 1
99 end
100
101 -- output views
102 local page = entry( { "admin", "statistics", "graph" }, template("admin_statistics/index"), _("Graphs"), 80)
103 page.i18n = "statistics"
104 page.setuser = "nobody"
105 page.setgroup = "nogroup"
106
107 local vars = luci.http.formvalue(nil, true)
108 local span = vars.timespan or nil
109
110 for i, plugin in luci.util.vspairs( tree:plugins() ) do
111
112 -- get plugin instances
113 local instances = tree:plugin_instances( plugin )
114
115 -- plugin menu entry
116 entry(
117 { "admin", "statistics", "graph", plugin },
118 call("statistics_render"), labels[plugin], i
119 ).query = { timespan = span }
120
121 -- if more then one instance is found then generate submenu
122 if #instances > 1 then
123 for j, inst in luci.util.vspairs(instances) do
124 -- instance menu entry
125 entry(
126 { "admin", "statistics", "graph", plugin, inst },
127 call("statistics_render"), inst, j
128 ).query = { timespan = span }
129 end
130 end
131 end
132 end
133
134 function statistics_render()
135
136 require("luci.statistics.rrdtool")
137 require("luci.template")
138 require("luci.model.uci")
139
140 local vars = luci.http.formvalue()
141 local req = luci.dispatcher.context.request
142 local path = luci.dispatcher.context.path
143 local uci = luci.model.uci.cursor()
144 local spans = luci.util.split( uci:get( "luci_statistics", "collectd_rrdtool", "RRATimespans" ), "%s+", nil, true )
145 local span = vars.timespan or uci:get( "luci_statistics", "rrdtool", "default_timespan" ) or spans[1]
146 local graph = luci.statistics.rrdtool.Graph( luci.util.parse_units( span ) )
147
148 local is_index = false
149
150 -- deliver image
151 if vars.img then
152 local l12 = require "luci.ltn12"
153 local png = io.open(graph.opts.imgpath .. "/" .. vars.img:gsub("%.+", "."), "r")
154 if png then
155 luci.http.prepare_content("image/png")
156 l12.pump.all(l12.source.file(png), luci.http.write)
157 png:close()
158 end
159 return
160 end
161
162 local plugin, instances
163 local images = { }
164
165 -- find requested plugin and instance
166 for i, p in ipairs( luci.dispatcher.context.path ) do
167 if luci.dispatcher.context.path[i] == "graph" then
168 plugin = luci.dispatcher.context.path[i+1]
169 instances = { luci.dispatcher.context.path[i+2] }
170 end
171 end
172
173 -- no instance requested, find all instances
174 if #instances == 0 then
175 --instances = { graph.tree:plugin_instances( plugin )[1] }
176 instances = graph.tree:plugin_instances( plugin )
177 is_index = true
178
179 -- index instance requested
180 elseif instances[1] == "-" then
181 instances[1] = ""
182 is_index = true
183 end
184
185
186 -- render graphs
187 for i, inst in ipairs( instances ) do
188 for i, img in ipairs( graph:render( plugin, inst, is_index ) ) do
189 table.insert( images, graph:strippngpath( img ) )
190 images[images[#images]] = inst
191 end
192 end
193
194 luci.template.render( "public_statistics/graph", {
195 images = images,
196 plugin = plugin,
197 timespans = spans,
198 current_timespan = span,
199 is_index = is_index
200 } )
201 end