d69b68e17aad478a6f0010cb6a6440c19d268297
[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 (c) 2012 Jo-Philipp Wich <xm@subsignal.org>
6
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
10
11 http://www.apache.org/licenses/LICENSE-2.0
12
13 ]]--
14
15 module("luci.controller.luci_statistics.luci_statistics", package.seeall)
16
17 function index()
18
19 require("nixio.fs")
20 require("luci.util")
21 require("luci.statistics.datatree")
22
23 -- override entry(): check for existance <plugin>.so where <plugin> is derived from the called path
24 function _entry( path, ... )
25 local file = path[5] or path[4]
26 if nixio.fs.access( "/usr/lib/collectd/" .. file .. ".so" ) then
27 entry( path, ... )
28 end
29 end
30
31 local labels = {
32 s_output = _("Output plugins"),
33 s_system = _("System plugins"),
34 s_network = _("Network plugins"),
35
36 conntrack = _("Conntrack"),
37 cpu = _("Processor"),
38 csv = _("CSV Output"),
39 df = _("Disk Space Usage"),
40 disk = _("Disk Usage"),
41 dns = _("DNS"),
42 email = _("Email"),
43 exec = _("Exec"),
44 interface = _("Interfaces"),
45 iptables = _("Firewall"),
46 irq = _("Interrupts"),
47 iwinfo = _("Wireless"),
48 load = _("System Load"),
49 memory = _("Memory"),
50 netlink = _("Netlink"),
51 network = _("Network"),
52 nut = _("UPS"),
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", "nut", "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 local _, plugin, idx
109 for _, plugin, idx in luci.util.vspairs( tree:plugins() ) do
110
111 -- get plugin instances
112 local instances = tree:plugin_instances( plugin )
113
114 -- plugin menu entry
115 entry(
116 { "admin", "statistics", "graph", plugin },
117 call("statistics_render"), labels[plugin], idx
118 ).query = { timespan = span , host = host }
119
120 -- if more then one instance is found then generate submenu
121 if #instances > 1 then
122 local _, inst, idx2
123 for _, inst, idx2 in luci.util.vspairs(instances) do
124 -- instance menu entry
125 entry(
126 { "admin", "statistics", "graph", plugin, inst },
127 call("statistics_render"), inst, idx2
128 ).query = { timespan = span , host = host }
129 end
130 end
131 end
132 end
133
134 function statistics_render()
135
136 require("luci.statistics.rrdtool")
137 require("luci.template")
138 require("luci.model.uci")
139
140 local vars = luci.http.formvalue()
141 local req = luci.dispatcher.context.request
142 local path = luci.dispatcher.context.path
143 local uci = luci.model.uci.cursor()
144 local spans = luci.util.split( uci:get( "luci_statistics", "collectd_rrdtool", "RRATimespans" ), "%s+", nil, true )
145 local span = vars.timespan or uci:get( "luci_statistics", "rrdtool", "default_timespan" ) or spans[1]
146 local host = vars.host or uci:get( "luci_statistics", "collectd", "Hostname" ) or luci.sys.hostname()
147 local opts = { host = vars.host }
148 local graph = luci.statistics.rrdtool.Graph( luci.util.parse_units( span ), opts )
149 local hosts = graph.tree:host_instances()
150
151 local is_index = false
152 local i, p, inst, idx
153
154 -- deliver image
155 if vars.img then
156 local l12 = require "luci.ltn12"
157 local png = io.open(graph.opts.imgpath .. "/" .. vars.img:gsub("%.+", "."), "r")
158 if png then
159 luci.http.prepare_content("image/png")
160 l12.pump.all(l12.source.file(png), luci.http.write)
161 png:close()
162 end
163 return
164 end
165
166 local plugin, instances
167 local images = { }
168
169 -- find requested plugin and instance
170 for i, p in ipairs( luci.dispatcher.context.path ) do
171 if luci.dispatcher.context.path[i] == "graph" then
172 plugin = luci.dispatcher.context.path[i+1]
173 instances = { luci.dispatcher.context.path[i+2] }
174 end
175 end
176
177 -- no instance requested, find all instances
178 if #instances == 0 then
179 --instances = { graph.tree:plugin_instances( plugin )[1] }
180 instances = graph.tree:plugin_instances( plugin )
181 is_index = true
182
183 -- index instance requested
184 elseif instances[1] == "-" then
185 instances[1] = ""
186 is_index = true
187 end
188
189
190 -- render graphs
191 for i, inst in luci.util.vspairs( instances ) do
192 for i, img in luci.util.vspairs( graph:render( plugin, inst, is_index ) ) do
193 table.insert( images, graph:strippngpath( img ) )
194 images[images[#images]] = inst
195 end
196 end
197
198 luci.template.render( "public_statistics/graph", {
199 images = images,
200 plugin = plugin,
201 timespans = spans,
202 current_timespan = span,
203 hosts = hosts,
204 current_host = host,
205 is_index = is_index
206 } )
207 end