applications/luci-statistics: Sort menu items alphabetically
[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                 load            = _("System Load"),
52                 memory          = _("Memory"),
53                 netlink         = _("Netlink"),
54                 network         = _("Network"),
55                 olsrd           = _("OLSRd"),
56                 ping            = _("Ping"),
57                 processes       = _("Processes"),
58                 rrdtool         = _("RRDTool"),
59                 tcpconns        = _("TCP Connections"),
60                 unixsock        = _("UnixSock"),
61                 wireless        = _("Wireless")
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", "wireless" }
69         }
70
71         -- create toplevel menu nodes
72         local st = entry({"admin", "statistics"}, call("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                         call( "statistics_" .. section .. "plugins" ),
85                         labels["s_"..section], index * 10
86                 )
87
88                 e.index = true
89                 e.i18n  = "rrdtool"
90
91                 for j, plugin in luci.util.vspairs( plugins ) do
92                         _entry(
93                                 { "admin", "statistics", "collectd", section, plugin },
94                                 cbi("luci_statistics/" .. plugin ),
95                                 labels[plugin], j * 10
96                         )
97                 end
98
99                 index = index + 1
100         end
101
102         -- output views
103         local page = entry( { "admin", "statistics", "graph" }, call("statistics_index"), _("Graphs"), 80)
104               page.i18n     = "statistics"
105               page.setuser  = "nobody"
106               page.setgroup = "nogroup"
107
108         local vars = luci.http.formvalue(nil, true)
109         local span = vars.timespan or nil
110
111         for i, plugin in luci.util.vspairs( tree:plugins() ) do
112
113                 -- get plugin instances
114                 local instances = tree:plugin_instances( plugin )
115
116                 -- plugin menu entry
117                 entry(
118                         { "admin", "statistics", "graph", plugin },
119                         call("statistics_render"), labels[plugin], i
120                 ).query = { timespan = span }
121
122                 -- if more then one instance is found then generate submenu
123                 if #instances > 1 then
124                         for j, inst in luci.util.vspairs(instances) do
125                                 -- instance menu entry
126                                 entry(
127                                         { "admin", "statistics", "graph", plugin, inst },
128                                         call("statistics_render"), inst, j
129                                 ).query = { timespan = span }
130                         end
131                 end
132         end
133 end
134
135 function statistics_index()
136         luci.template.render("admin_statistics/index")
137 end
138
139 function statistics_outputplugins()
140         local translate = luci.i18n.translate
141         local plugins = {
142                 rrdtool         = translate("RRDTool"),
143                 network         = translate("Network"),
144                 unixsock        = translate("UnixSock"),
145                 csv                     = translate("CSV Output")
146         }
147
148         luci.template.render("admin_statistics/outputplugins", {plugins=plugins})
149 end
150
151 function statistics_systemplugins()
152         local translate = luci.i18n.translate
153         local plugins = {
154                 exec            = translate("Exec"),
155                 email           = translate("Email"),
156                 cpu                     = translate("Processor"),
157                 df                      = translate("Disk Space Usage"),
158                 disk            = translate("Disk Usage"),
159                 irq                     = translate("Interrupts"),
160                 processes       = translate("Processes"),
161                 load            = translate("System Load"),
162         }
163
164         luci.template.render("admin_statistics/systemplugins", {plugins=plugins})
165 end
166
167 function statistics_networkplugins()
168         local translate = luci.i18n.translate
169         local plugins = {
170                 interface       = translate("Interfaces"),
171                 netlink         = translate("Netlink"),
172                 iptables        = translate("Firewall"),
173                 tcpconns        = translate("TCP Connections"),
174                 ping            = translate("Ping"),
175                 dns                     = translate("DNS"),
176                 wireless        = translate("Wireless"),
177                 olsrd           = translate("OLSRd")
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         local is_index = false
199
200         -- deliver image
201         if vars.img then
202                 local l12 = require "luci.ltn12"
203                 local png = io.open(graph.opts.imgpath .. "/" .. vars.img:gsub("%.+", "."), "r")
204                 if png then
205                         luci.http.prepare_content("image/png")
206                         l12.pump.all(l12.source.file(png), luci.http.write)
207                         png:close()
208                 end
209                 return
210         end
211
212         local plugin, instances
213         local images = { }
214
215         -- find requested plugin and instance
216     for i, p in ipairs( luci.dispatcher.context.path ) do
217         if luci.dispatcher.context.path[i] == "graph" then
218             plugin    = luci.dispatcher.context.path[i+1]
219             instances = { luci.dispatcher.context.path[i+2] }
220         end
221     end
222
223         -- no instance requested, find all instances
224         if #instances == 0 then
225                 --instances = { graph.tree:plugin_instances( plugin )[1] }
226                 instances = graph.tree:plugin_instances( plugin )
227                 is_index = true
228
229         -- index instance requested
230         elseif instances[1] == "-" then
231                 instances[1] = ""
232                 is_index = true
233         end
234
235
236         -- render graphs
237         for i, inst in ipairs( instances ) do
238                 for i, img in ipairs( graph:render( plugin, inst, is_index ) ) do
239                         table.insert( images, graph:strippngpath( img ) )
240                         images[images[#images]] = inst
241                 end
242         end
243
244         luci.template.render( "public_statistics/graph", {
245                 images           = images,
246                 plugin           = plugin,
247                 timespans        = spans,
248                 current_timespan = span,
249                 is_index         = is_index
250         } )
251 end