luci-0.11: merge outstanding trunk changes
[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 nut = _("UPS"),
54 olsrd = _("OLSRd"),
55 ping = _("Ping"),
56 processes = _("Processes"),
57 rrdtool = _("RRDTool"),
58 tcpconns = _("TCP Connections"),
59 unixsock = _("UnixSock")
60 }
61
62 -- our collectd menu
63 local collectd_menu = {
64 output = { "csv", "network", "rrdtool", "unixsock" },
65 system = { "cpu", "df", "disk", "email", "exec", "irq", "load", "memory", "nut", "processes" },
66 network = { "conntrack", "dns", "interface", "iptables", "netlink", "olsrd", "ping", "tcpconns", "iwinfo" }
67 }
68
69 -- create toplevel menu nodes
70 local st = entry({"admin", "statistics"}, template("admin_statistics/index"), _("Statistics"), 80)
71 st.index = true
72
73 entry({"admin", "statistics", "collectd"}, cbi("luci_statistics/collectd"), _("Collectd"), 10).subindex = true
74
75
76 -- populate collectd plugin menu
77 local index = 1
78 for section, plugins in luci.util.kspairs( collectd_menu ) do
79 local e = entry(
80 { "admin", "statistics", "collectd", section },
81 firstchild(), labels["s_"..section], index * 10
82 )
83
84 e.index = true
85
86 for j, plugin in luci.util.vspairs( plugins ) do
87 _entry(
88 { "admin", "statistics", "collectd", section, plugin },
89 cbi("luci_statistics/" .. plugin ),
90 labels[plugin], j * 10
91 )
92 end
93
94 index = index + 1
95 end
96
97 -- output views
98 local page = entry( { "admin", "statistics", "graph" }, template("admin_statistics/index"), _("Graphs"), 80)
99 page.setuser = "nobody"
100 page.setgroup = "nogroup"
101
102 local vars = luci.http.formvalue(nil, true)
103 local span = vars.timespan or nil
104 local host = vars.host or nil
105
106 -- get rrd data tree
107 local tree = luci.statistics.datatree.Instance(host)
108
109 for i, plugin 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], i
118 ).query = { timespan = span , host = host }
119
120 -- if more then one instance is found then generate submenu
121 if #instances > 1 then
122 for j, inst in luci.util.vspairs(instances) do
123 -- instance menu entry
124 entry(
125 { "admin", "statistics", "graph", plugin, inst },
126 call("statistics_render"), inst, j
127 ).query = { timespan = span , host = host }
128 end
129 end
130 end
131 end
132
133 function statistics_render()
134
135 require("luci.statistics.rrdtool")
136 require("luci.template")
137 require("luci.model.uci")
138
139 local vars = luci.http.formvalue()
140 local req = luci.dispatcher.context.request
141 local path = luci.dispatcher.context.path
142 local uci = luci.model.uci.cursor()
143 local spans = luci.util.split( uci:get( "luci_statistics", "collectd_rrdtool", "RRATimespans" ), "%s+", nil, true )
144 local span = vars.timespan or uci:get( "luci_statistics", "rrdtool", "default_timespan" ) or spans[1]
145 local host = vars.host or uci:get( "luci_statistics", "collectd", "Hostname" ) or luci.sys.hostname()
146 local opts = { host = vars.host }
147 local graph = luci.statistics.rrdtool.Graph( luci.util.parse_units( span ), opts )
148 local hosts = graph.tree:host_instances()
149
150 local is_index = false
151
152 -- deliver image
153 if vars.img then
154 local l12 = require "luci.ltn12"
155 local png = io.open(graph.opts.imgpath .. "/" .. vars.img:gsub("%.+", "."), "r")
156 if png then
157 luci.http.prepare_content("image/png")
158 l12.pump.all(l12.source.file(png), luci.http.write)
159 png:close()
160 end
161 return
162 end
163
164 local plugin, instances
165 local images = { }
166
167 -- find requested plugin and instance
168 for i, p in ipairs( luci.dispatcher.context.path ) do
169 if luci.dispatcher.context.path[i] == "graph" then
170 plugin = luci.dispatcher.context.path[i+1]
171 instances = { luci.dispatcher.context.path[i+2] }
172 end
173 end
174
175 -- no instance requested, find all instances
176 if #instances == 0 then
177 --instances = { graph.tree:plugin_instances( plugin )[1] }
178 instances = graph.tree:plugin_instances( plugin )
179 is_index = true
180
181 -- index instance requested
182 elseif instances[1] == "-" then
183 instances[1] = ""
184 is_index = true
185 end
186
187
188 -- render graphs
189 for i, inst in ipairs( instances ) do
190 for i, img in ipairs( graph:render( plugin, inst, is_index ) ) do
191 table.insert( images, graph:strippngpath( img ) )
192 images[images[#images]] = inst
193 end
194 end
195
196 luci.template.render( "public_statistics/graph", {
197 images = images,
198 plugin = plugin,
199 timespans = spans,
200 current_timespan = span,
201 hosts = hosts,
202 current_host = host,
203 is_index = is_index
204 } )
205 end