1a7472781d59ec265f7a6b72ee92e158699c23d8
[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 rrdtool = _("RRDTool"),
41 network = _("Network"),
42 unixsock = _("UnixSock"),
43 csv = _("CSV Output"),
44 exec = _("Exec"),
45 email = _("Email"),
46 cpu = _("Processor"),
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"),
56 ping = _("Ping"),
57 dns = _("DNS"),
58 wireless = _("Wireless"),
59 olsrd = _("OLSRd")
60 }
61
62 -- our collectd menu
63 local collectd_menu = {
64 output = { "rrdtool", "network", "unixsock", "csv" },
65 system = { "exec", "email", "cpu", "df", "disk", "irq", "processes", "load" },
66 network = { "interface", "netlink", "iptables", "tcpconns", "ping", "dns", "wireless", "olsrd" }
67 }
68
69 -- create toplevel menu nodes
70 local st = entry({"admin", "statistics"}, call("statistics_index"), _("Statistics"), 80)
71 st.i18n = "statistics"
72 st.index = true
73
74 entry({"admin", "statistics", "collectd"}, cbi("luci_statistics/collectd"), _("Collectd"), 10).subindex = true
75
76
77 -- populate collectd plugin menu
78 local index = 1
79 for section, plugins in luci.util.kspairs( collectd_menu ) do
80 local e = entry(
81 { "admin", "statistics", "collectd", section },
82 call( "statistics_" .. section .. "plugins" ),
83 labels["s_"..section], index * 10
84 )
85
86 e.index = true
87 e.i18n = "rrdtool"
88
89 for j, plugin in luci.util.vspairs( plugins ) do
90 _entry(
91 { "admin", "statistics", "collectd", section, plugin },
92 cbi("luci_statistics/" .. plugin ),
93 labels[plugin], j * 10
94 )
95 end
96
97 index = index + 1
98 end
99
100 -- output views
101 local page = entry( { "admin", "statistics", "graph" }, call("statistics_index"), _("Graphs"), 80)
102 page.i18n = "statistics"
103 page.setuser = "nobody"
104 page.setgroup = "nogroup"
105
106 local vars = luci.http.formvalue(nil, true)
107 local span = vars.timespan or nil
108
109 for i, plugin in luci.util.vspairs( tree:plugins() ) do
110
111 -- get plugin instances
112 local instances = tree:plugin_instances( plugin )
113
114 -- plugin menu entry
115 entry(
116 { "admin", "statistics", "graph", plugin },
117 call("statistics_render"), labels[plugin], i
118 ).query = { timespan = span }
119
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
124 entry(
125 { "admin", "statistics", "graph", plugin, inst },
126 call("statistics_render"), inst, j
127 ).query = { timespan = span }
128 end
129 end
130 end
131 end
132
133 function statistics_index()
134 luci.template.render("admin_statistics/index")
135 end
136
137 function statistics_outputplugins()
138 local translate = luci.i18n.translate
139 local plugins = {
140 rrdtool = translate("RRDTool"),
141 network = translate("Network"),
142 unixsock = translate("UnixSock"),
143 csv = translate("CSV Output")
144 }
145
146 luci.template.render("admin_statistics/outputplugins", {plugins=plugins})
147 end
148
149 function statistics_systemplugins()
150 local translate = luci.i18n.translate
151 local plugins = {
152 exec = translate("Exec"),
153 email = translate("Email"),
154 cpu = translate("Processor"),
155 df = translate("Disk Space Usage"),
156 disk = translate("Disk Usage"),
157 irq = translate("Interrupts"),
158 processes = translate("Processes"),
159 load = translate("System Load"),
160 }
161
162 luci.template.render("admin_statistics/systemplugins", {plugins=plugins})
163 end
164
165 function statistics_networkplugins()
166 local translate = luci.i18n.translate
167 local plugins = {
168 interface = translate("Interfaces"),
169 netlink = translate("Netlink"),
170 iptables = translate("Firewall"),
171 tcpconns = translate("TCP Connections"),
172 ping = translate("Ping"),
173 dns = translate("DNS"),
174 wireless = translate("Wireless"),
175 olsrd = translate("OLSRd")
176 }
177
178 luci.template.render("admin_statistics/networkplugins", {plugins=plugins})
179 end
180
181
182 function statistics_render()
183
184 require("luci.statistics.rrdtool")
185 require("luci.template")
186 require("luci.model.uci")
187
188 local vars = luci.http.formvalue()
189 local req = luci.dispatcher.context.request
190 local path = luci.dispatcher.context.path
191 local uci = luci.model.uci.cursor()
192 local spans = luci.util.split( uci:get( "luci_statistics", "collectd_rrdtool", "RRATimespans" ), "%s+", nil, true )
193 local span = vars.timespan or uci:get( "luci_statistics", "rrdtool", "default_timespan" ) or spans[1]
194 local graph = luci.statistics.rrdtool.Graph( luci.util.parse_units( span ) )
195
196 local is_index = false
197
198 -- deliver image
199 if vars.img then
200 local l12 = require "luci.ltn12"
201 local png = io.open(graph.opts.imgpath .. "/" .. vars.img:gsub("%.+", "."), "r")
202 if png then
203 luci.http.prepare_content("image/png")
204 l12.pump.all(l12.source.file(png), luci.http.write)
205 png:close()
206 end
207 return
208 end
209
210 local plugin, instances
211 local images = { }
212
213 -- find requested plugin and instance
214 for i, p in ipairs( luci.dispatcher.context.path ) do
215 if luci.dispatcher.context.path[i] == "graph" then
216 plugin = luci.dispatcher.context.path[i+1]
217 instances = { luci.dispatcher.context.path[i+2] }
218 end
219 end
220
221 -- no instance requested, find all instances
222 if #instances == 0 then
223 --instances = { graph.tree:plugin_instances( plugin )[1] }
224 instances = graph.tree:plugin_instances( plugin )
225 is_index = true
226
227 -- index instance requested
228 elseif instances[1] == "-" then
229 instances[1] = ""
230 is_index = true
231 end
232
233
234 -- render graphs
235 for i, inst in ipairs( instances ) do
236 for i, img in ipairs( graph:render( plugin, inst, is_index ) ) do
237 table.insert( images, graph:strippngpath( img ) )
238 images[images[#images]] = inst
239 end
240 end
241
242 luci.template.render( "public_statistics/graph", {
243 images = images,
244 plugin = plugin,
245 timespans = spans,
246 current_timespan = span,
247 is_index = is_index
248 } )
249 end