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