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