remove .i18n annotations from controller files
[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         -- override entry(): check for existance <plugin>.so where <plugin> is derived from the called path
25         function _entry( path, ... )
26                 local file = path[5] or path[4]
27                 if nixio.fs.access( "/usr/lib/collectd/" .. file .. ".so" ) then
28                         entry( path, ... )
29                 end
30         end
31
32         local labels = {
33                 s_output        = _("Output plugins"),
34                 s_system        = _("System plugins"),
35                 s_network       = _("Network plugins"),
36
37                 conntrack       = _("Conntrack"),
38                 cpu                     = _("Processor"),
39                 csv                     = _("CSV Output"),
40                 df                      = _("Disk Space Usage"),
41                 disk            = _("Disk Usage"),
42                 dns                     = _("DNS"),
43                 email           = _("Email"),
44                 exec            = _("Exec"),
45                 interface       = _("Interfaces"),
46                 iptables        = _("Firewall"),
47                 irq                     = _("Interrupts"),
48                 iwinfo          = _("Wireless"),
49                 load            = _("System Load"),
50                 memory          = _("Memory"),
51                 netlink         = _("Netlink"),
52                 network         = _("Network"),
53                 olsrd           = _("OLSRd"),
54                 ping            = _("Ping"),
55                 processes       = _("Processes"),
56                 rrdtool         = _("RRDTool"),
57                 tcpconns        = _("TCP Connections"),
58                 unixsock        = _("UnixSock")
59         }
60
61         -- our collectd menu
62         local collectd_menu = {
63                 output  = { "csv", "network", "rrdtool", "unixsock" },
64                 system  = { "cpu", "df", "disk", "email", "exec", "irq", "load", "memory", "processes" },
65                 network = { "conntrack", "dns", "interface", "iptables", "netlink", "olsrd", "ping", "tcpconns", "iwinfo" }
66         }
67
68         -- create toplevel menu nodes
69         local st = entry({"admin", "statistics"}, template("admin_statistics/index"), _("Statistics"), 80)
70         st.index = true
71         
72         entry({"admin", "statistics", "collectd"}, cbi("luci_statistics/collectd"), _("Collectd"), 10).subindex = true
73         
74
75         -- populate collectd plugin menu
76         local index = 1
77         for section, plugins in luci.util.kspairs( collectd_menu ) do
78                 local e = entry(
79                         { "admin", "statistics", "collectd", section },
80                         firstchild(), labels["s_"..section], index * 10
81                 )
82
83                 e.index = true
84
85                 for j, plugin in luci.util.vspairs( plugins ) do
86                         _entry(
87                                 { "admin", "statistics", "collectd", section, plugin },
88                                 cbi("luci_statistics/" .. plugin ),
89                                 labels[plugin], j * 10
90                         )
91                 end
92
93                 index = index + 1
94         end
95
96         -- output views
97         local page = entry( { "admin", "statistics", "graph" }, template("admin_statistics/index"), _("Graphs"), 80)
98               page.setuser  = "nobody"
99               page.setgroup = "nogroup"
100
101         local vars = luci.http.formvalue(nil, true)
102         local span = vars.timespan or nil
103         local host = vars.host or nil
104
105         -- get rrd data tree
106         local tree = luci.statistics.datatree.Instance(host)
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 , host = host }
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 , host = host }
127                         end
128                 end
129         end
130 end
131
132 function statistics_render()
133
134         require("luci.statistics.rrdtool")
135         require("luci.template")
136         require("luci.model.uci")
137
138         local vars  = luci.http.formvalue()
139         local req   = luci.dispatcher.context.request
140         local path  = luci.dispatcher.context.path
141         local uci   = luci.model.uci.cursor()
142         local spans = luci.util.split( uci:get( "luci_statistics", "collectd_rrdtool", "RRATimespans" ), "%s+", nil, true )
143         local span  = vars.timespan or uci:get( "luci_statistics", "rrdtool", "default_timespan" ) or spans[1]
144         local host  = vars.host     or uci:get( "luci_statistics", "collectd", "Hostname" ) or luci.sys.hostname()
145         local opts = { host = vars.host }
146         local graph = luci.statistics.rrdtool.Graph( luci.util.parse_units( span ), opts )
147         local hosts = graph.tree:host_instances()
148
149         local is_index = false
150
151         -- deliver image
152         if vars.img then
153                 local l12 = require "luci.ltn12"
154                 local png = io.open(graph.opts.imgpath .. "/" .. vars.img:gsub("%.+", "."), "r")
155                 if png then
156                         luci.http.prepare_content("image/png")
157                         l12.pump.all(l12.source.file(png), luci.http.write)
158                         png:close()
159                 end
160                 return
161         end
162
163         local plugin, instances
164         local images = { }
165
166         -- find requested plugin and instance
167     for i, p in ipairs( luci.dispatcher.context.path ) do
168         if luci.dispatcher.context.path[i] == "graph" then
169             plugin    = luci.dispatcher.context.path[i+1]
170             instances = { luci.dispatcher.context.path[i+2] }
171         end
172     end
173
174         -- no instance requested, find all instances
175         if #instances == 0 then
176                 --instances = { graph.tree:plugin_instances( plugin )[1] }
177                 instances = graph.tree:plugin_instances( plugin )
178                 is_index = true
179
180         -- index instance requested
181         elseif instances[1] == "-" then
182                 instances[1] = ""
183                 is_index = true
184         end
185
186
187         -- render graphs
188         for i, inst in ipairs( instances ) do
189                 for i, img in ipairs( graph:render( plugin, inst, is_index ) ) do
190                         table.insert( images, graph:strippngpath( img ) )
191                         images[images[#images]] = inst
192                 end
193         end
194
195         luci.template.render( "public_statistics/graph", {
196                 images           = images,
197                 plugin           = plugin,
198                 timespans        = spans,
199                 current_timespan = span,
200                 hosts            = hosts,
201                 current_host     = host,
202                 is_index         = is_index
203         } )
204 end