* luci/applications: statistics: pick up new rrdtool translations in controller and...
[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("luci.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 luci.fs.isfile( "/usr/lib/collectd/" .. file .. ".so" ) then
36                         entry( path, ... )
37                 end
38         end
39
40         -- override i18n(): try to translate stat_<str> or fall back to <str>
41         function _i18n( str )
42                 return luci.i18n.translate( "stat_" .. str, str )
43         end
44
45         -- our collectd menu
46         local collectd_menu = {
47                 output  = { "rrdtool", "network", "unixsock", "csv" },
48                 system  = { "exec", "email", "cpu", "df", "disk", "irq", "processes", "load" },
49                 network = { "interface", "netlink", "iptables", "tcpconns", "ping", "dns", "wireless" }
50         }
51
52         -- create toplevel menu nodes
53         entry({"admin", "statistics"},             call("statistics_index"),        _i18n("statistics"), 80).i18n = "statistics"
54         entry({"admin", "statistics", "collectd"}, cbi("luci_statistics/collectd"), _i18n("collectd"),   10)
55
56         -- populate collectd plugin menu
57         local index = 1
58         for section, plugins in luci.util.kspairs( collectd_menu ) do
59                 entry(
60                         { "admin", "statistics", "collectd", section },
61                         call( "statistics_" .. section .. "plugins" ),
62                         _i18n( section .. "plugins" ),
63                         index * 10
64                 )
65
66                 for j, plugin in luci.util.vspairs( plugins ) do
67                         _entry(
68                                 { "admin", "statistics", "collectd", section, plugin },
69                                 cbi("luci_statistics/" .. plugin ),
70                                 _i18n( plugin ),
71                                 j * 10
72                         )
73                 end
74
75                 index = index + 1
76         end
77
78         -- output views
79         local page = entry( { "admin", "statistics", "graph" }, call("statistics_index"), _i18n("graphs"), 80)
80               page.i18n     = "statistics"
81               page.setuser  = "nobody"
82               page.setgroup = "nogroup"
83
84         local vars = luci.http.formvalue(nil, true)
85         local span = vars.timespan or nil
86
87         for i, plugin in luci.util.vspairs( tree:plugins() ) do
88
89                 -- get plugin instances
90                 local instances = tree:plugin_instances( plugin )
91
92                 -- plugin menu entry
93                 entry(
94                         { "admin", "statistics", "graph", plugin },
95                         call("statistics_render"), _i18n( plugin ), i
96                 ).query = { timespan = span }
97
98                 -- if more then one instance is found then generate submenu
99                 if #instances > 1 then
100                         for j, inst in luci.util.vspairs(instances) do
101                                 -- instance menu entry
102                                 entry(
103                                         { "admin", "statistics", "graph", plugin, inst },
104                                         call("statistics_render"), inst, j
105                                 ).query = { timespan = span }
106                         end
107                 end
108         end
109 end
110
111 function statistics_index()
112         luci.template.render("admin_statistics/index")
113 end
114
115 function statistics_outputplugins()
116         local plugins = { }
117
118         for i, p in ipairs({ "rrdtool", "network", "unixsock", "csv" }) do
119                 plugins[p] = luci.i18n.translate( "stat_" .. p, p )
120         end
121
122         luci.template.render("admin_statistics/outputplugins", {plugins=plugins})
123 end
124
125 function statistics_systemplugins()
126         local plugins = { }
127
128         for i, p in ipairs({ "exec", "email", "df", "disk", "irq", "processes", "cpu" }) do
129                 plugins[p] = luci.i18n.translate( "stat_" .. p, p )
130         end
131
132         luci.template.render("admin_statistics/systemplugins", {plugins=plugins})
133 end
134
135 function statistics_networkplugins()
136         local plugins = { }
137
138         for i, p in ipairs({ "interface", "netlink", "iptables", "tcpconns", "ping", "dns", "wireless" }) do
139                 plugins[p] = luci.i18n.translate( "stat_" .. p, p )
140         end
141
142         luci.template.render("admin_statistics/networkplugins", {plugins=plugins})
143 end
144
145
146 function statistics_render()
147
148         require("luci.statistics.rrdtool")
149         require("luci.template")
150         require("luci.model.uci")
151
152         local vars  = luci.http.formvalue()
153         local req   = luci.dispatcher.context.request
154         local path  = luci.dispatcher.context.dispatched.path
155         local uci   = luci.model.uci.cursor()
156         local spans = luci.util.split( uci:get( "luci_statistics", "collectd_rrdtool", "RRATimespans" ), "%s+", nil, true )
157         local span  = vars.timespan or uci:get( "luci_statistics", "rrdtool", "default_timespan" ) or spans[1]
158         local graph = luci.statistics.rrdtool.Graph( luci.util.parse_units( span ) )
159
160         local plugin, instances
161         local images = { }
162
163         -- find requested plugin and instance
164         for i, p in ipairs( luci.dispatcher.context.dispatched.path ) do
165                 if luci.dispatcher.context.dispatched.path[i] == "graph" then
166                         plugin    = luci.dispatcher.context.dispatched.path[i+1]
167                         instances = { luci.dispatcher.context.dispatched.path[i+2] }
168                 end
169         end
170
171         -- no instance requested, find all instances
172         if #instances == 0 then
173                 instances = { graph.tree:plugin_instances( plugin )[1] }
174
175         -- index instance requested
176         elseif instances[1] == "-" then
177                 instances[1] = ""
178         end
179
180
181         -- render graphs
182         for i, inst in ipairs( instances ) do
183                 for i, img in ipairs( graph:render( plugin, inst ) ) do
184                         table.insert( images, graph:strippngpath( img ) )
185                 end
186         end
187
188         luci.template.render( "public_statistics/graph", {
189                 images           = images,
190                 plugin           = plugin,
191                 timespans        = spans,
192                 current_timespan = span
193         } )
194 end