applications/luci-statistics: fix translations of cbi models and controller
[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.i18n")
23 require("luci.statistics.datatree")
24
25 -- load language files
26 luci.i18n.loadc("rrdtool")
27 luci.i18n.loadc("statistics")
28
29 -- get rrd data tree
30 local tree = luci.statistics.datatree.Instance()
31
32 -- override entry(): check for existance <plugin>.so where <plugin> is derived from the called path
33 function _entry( path, ... )
34 local file = path[5] or path[4]
35 if nixio.fs.access( "/usr/lib/collectd/" .. file .. ".so" ) then
36 entry( path, ... )
37 end
38 end
39
40 local translate = luci.i18n.translate
41
42 local labels = {
43 s_output = translate("Output plugins"),
44 s_system = translate("System plugins"),
45 s_network = translate("Network plugins"),
46
47 rrdtool = translate("RRDTool"),
48 network = translate("Network"),
49 unixsock = translate("UnixSock"),
50 csv = translate("CSV Output"),
51 exec = translate("Exec"),
52 email = translate("Email"),
53 cpu = translate("Processor"),
54 df = translate("Disk Space Usage"),
55 disk = translate("Disk Usage"),
56 irq = translate("Interrupts"),
57 processes = translate("Processes"),
58 load = translate("System Load"),
59 interface = translate("Interfaces"),
60 netlink = translate("Netlink"),
61 iptables = translate("Firewall"),
62 tcpconns = translate("TCP Connections"),
63 ping = translate("Ping"),
64 dns = translate("DNS"),
65 wireless = translate("Wireless")
66 }
67
68 -- our collectd menu
69 local collectd_menu = {
70 output = { "rrdtool", "network", "unixsock", "csv" },
71 system = { "exec", "email", "cpu", "df", "disk", "irq", "processes", "load" },
72 network = { "interface", "netlink", "iptables", "tcpconns", "ping", "dns", "wireless" }
73 }
74
75 -- create toplevel menu nodes
76 local st = entry({"admin", "statistics"}, call("statistics_index"), translate("Statistics"), 80)
77 st.i18n = "statistics"
78 st.index = true
79
80 entry({"admin", "statistics", "collectd"}, cbi("luci_statistics/collectd"), translate("Collectd"), 10).subindex = true
81
82
83 -- populate collectd plugin menu
84 local index = 1
85 for section, plugins in luci.util.kspairs( collectd_menu ) do
86 entry(
87 { "admin", "statistics", "collectd", section },
88 call( "statistics_" .. section .. "plugins" ),
89 labels["s_"..section], index * 10
90 ).index = true
91
92 for j, plugin in luci.util.vspairs( plugins ) do
93 _entry(
94 { "admin", "statistics", "collectd", section, plugin },
95 cbi("luci_statistics/" .. plugin ),
96 labels[plugin], j * 10
97 )
98 end
99
100 index = index + 1
101 end
102
103 -- output views
104 local page = entry( { "admin", "statistics", "graph" }, call("statistics_index"), translate("Graphs"), 80)
105 page.i18n = "statistics"
106 page.setuser = "nobody"
107 page.setgroup = "nogroup"
108
109 local vars = luci.http.formvalue(nil, true)
110 local span = vars.timespan or nil
111
112 for i, plugin in luci.util.vspairs( tree:plugins() ) do
113
114 -- get plugin instances
115 local instances = tree:plugin_instances( plugin )
116
117 -- plugin menu entry
118 entry(
119 { "admin", "statistics", "graph", plugin },
120 call("statistics_render"), labels[plugin], i
121 ).query = { timespan = span }
122
123 -- if more then one instance is found then generate submenu
124 if #instances > 1 then
125 for j, inst in luci.util.vspairs(instances) do
126 -- instance menu entry
127 entry(
128 { "admin", "statistics", "graph", plugin, inst },
129 call("statistics_render"), inst, j
130 ).query = { timespan = span }
131 end
132 end
133 end
134 end
135
136 function statistics_index()
137 luci.template.render("admin_statistics/index")
138 end
139
140 function statistics_outputplugins()
141 local translate = luci.i18n.translate
142 local plugins = {
143 rrdtool = translate("RRDTool"),
144 network = translate("Network"),
145 unixsock = translate("UnixSock"),
146 csv = translate("CSV Output")
147 }
148
149 luci.template.render("admin_statistics/outputplugins", {plugins=plugins})
150 end
151
152 function statistics_systemplugins()
153 local translate = luci.i18n.translate
154 local plugins = {
155 exec = translate("Exec"),
156 email = translate("Email"),
157 cpu = translate("Processor"),
158 df = translate("Disk Space Usage"),
159 disk = translate("Disk Usage"),
160 irq = translate("Interrupts"),
161 processes = translate("Processes"),
162 load = translate("System Load"),
163 }
164
165 luci.template.render("admin_statistics/systemplugins", {plugins=plugins})
166 end
167
168 function statistics_networkplugins()
169 local translate = luci.i18n.translate
170 local plugins = {
171 interface = translate("Interfaces"),
172 netlink = translate("Netlink"),
173 iptables = translate("Firewall"),
174 tcpconns = translate("TCP Connections"),
175 ping = translate("Ping"),
176 dns = translate("DNS"),
177 wireless = translate("Wireless")
178 }
179
180 luci.template.render("admin_statistics/networkplugins", {plugins=plugins})
181 end
182
183
184 function statistics_render()
185
186 require("luci.statistics.rrdtool")
187 require("luci.template")
188 require("luci.model.uci")
189
190 local vars = luci.http.formvalue()
191 local req = luci.dispatcher.context.request
192 local path = luci.dispatcher.context.path
193 local uci = luci.model.uci.cursor()
194 local spans = luci.util.split( uci:get( "luci_statistics", "collectd_rrdtool", "RRATimespans" ), "%s+", nil, true )
195 local span = vars.timespan or uci:get( "luci_statistics", "rrdtool", "default_timespan" ) or spans[1]
196 local graph = luci.statistics.rrdtool.Graph( luci.util.parse_units( span ) )
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
225 -- index instance requested
226 elseif instances[1] == "-" then
227 instances[1] = ""
228 end
229
230
231 -- render graphs
232 for i, inst in ipairs( instances ) do
233 for i, img in ipairs( graph:render( plugin, inst ) ) do
234 table.insert( images, graph:strippngpath( img ) )
235 end
236 end
237
238 luci.template.render( "public_statistics/graph", {
239 images = images,
240 plugin = plugin,
241 timespans = spans,
242 current_timespan = span
243 } )
244 end