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