ae1679bee7ec5914d71f2108fecdb4e6fe14da0e
[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 entry( { "admin", "statistics", "graph" }, call("statistics_index"), _i18n("graphs"), 80).i18n = "statistics"
74
75 local vars = luci.http.formvalues()
76 local span = vars.timespan or nil
77
78 for i, plugin in ipairs( tree:plugins() ) do
79
80 -- get plugin instances
81 local instances = tree:plugin_instances( plugin )
82
83 -- plugin menu entry
84 entry(
85 { "admin", "statistics", "graph", plugin },
86 call("statistics_render"), _i18n( plugin ), i
87 ).query = { timespan = span }
88
89 -- if more then one instance is found then generate submenu
90 if #instances > 1 then
91 for j, inst in ipairs(instances) do
92 -- instance menu entry
93 entry(
94 { "admin", "statistics", "graph", plugin, inst },
95 call("statistics_render"), inst, j
96 ).query = { timespan = span }
97 end
98 end
99 end
100 end
101
102 function statistics_index()
103 luci.template.render("admin_statistics/index")
104 end
105
106 function statistics_outputplugins()
107 local plugins = { }
108
109 for i, p in ipairs({ "rrdtool", "network", "unixsock", "csv" }) do
110 plugins[p] = luci.i18n.translate( "stat_" .. p, p )
111 end
112
113 luci.template.render("admin_statistics/outputplugins", {plugins=plugins})
114 end
115
116 function statistics_systemplugins()
117 local plugins = { }
118
119 for i, p in ipairs({ "exec", "email", "df", "disk", "irq", "processes", "cpu" }) do
120 plugins[p] = luci.i18n.translate( "stat_" .. p, p )
121 end
122
123 luci.template.render("admin_statistics/systemplugins", {plugins=plugins})
124 end
125
126 function statistics_networkplugins()
127 local plugins = { }
128
129 for i, p in ipairs({ "interface", "netlink", "iptables", "tcpconns", "ping", "dns", "wireless" }) do
130 plugins[p] = luci.i18n.translate( "stat_" .. p, p )
131 end
132
133 luci.template.render("admin_statistics/networkplugins", {plugins=plugins})
134 end
135
136
137 function statistics_render()
138
139 require("luci.statistics.rrdtool")
140 require("luci.template")
141 require("luci.model.uci")
142
143 local vars = luci.http.formvalues()
144 local req = luci.dispatcher.request
145 local uci = luci.model.uci.Session()
146 local spans = luci.util.split( uci:get( "luci_statistics", "collectd_rrdtool", "RRATimespans" ), "%s+", nil, true )
147 local span = vars.timespan or uci:get( "luci_statistics", "rrdtool", "default_timespan" ) or spans[1]
148 local graph = luci.statistics.rrdtool.Graph( luci.util.parse_units( span ) )
149
150 local plugin = req[4]
151 local instances = { req[5] }
152 local images = { }
153
154 -- no instance requested, find all instances
155 if #instances == 0 then
156
157 instances = graph.tree:plugin_instances( plugin )
158
159 -- more than one available instance
160 if #instances > 1 then
161
162 -- redirect to first instance and return
163 local r = luci.dispatcher.request
164 local i = instances[1]
165 if i:len() == 0 then i = "-" end
166
167 luci.http.redirect( luci.dispatcher.build_url(
168 req[1], req[2], req[3], req[4], i
169 ) )
170
171 return
172 end
173
174 -- index instance requested
175 elseif instances[1] == "-" then
176 instances[1] = ""
177 end
178
179
180 -- render graphs
181 for i, inst in ipairs( instances ) do
182 for i, img in ipairs( graph:render( plugin, inst ) ) do
183 table.insert( images, graph:strippngpath( img ) )
184 end
185 end
186
187 luci.template.render( "public_statistics/graph", {
188 images = images,
189 plugin = plugin,
190 timespans = spans,
191 current_timespan = span
192 } )
193 end