Support for collectd-mod-contextswitch
[project/luci.git] / applications / luci-app-statistics / luasrc / controller / luci_statistics / luci_statistics.lua
1 -- Copyright 2008 Freifunk Leipzig / Jo-Philipp Wich <jow@openwrt.org>
2 -- Copyright 2012 Jo-Philipp Wich <jow@openwrt.org>
3 -- Licensed to the public under the Apache License 2.0.
4
5 module("luci.controller.luci_statistics.luci_statistics", package.seeall)
6
7 function index()
8
9 require("nixio.fs")
10 require("luci.util")
11 require("luci.statistics.datatree")
12
13 -- override entry(): check for existance <plugin>.so where <plugin> is derived from the called path
14 function _entry( path, ... )
15 local file = path[5] or path[4]
16 if nixio.fs.access( "/usr/lib/collectd/" .. file .. ".so" ) then
17 entry( path, ... )
18 end
19 end
20
21 local labels = {
22 s_output = _("Output plugins"),
23 s_general = _("General plugins"),
24 s_network = _("Network plugins"),
25
26 conntrack = _("Conntrack"),
27 contextswitch = _("Context Switches"),
28 cpu = _("Processor"),
29 cpufreq = _("CPU Frequency"),
30 csv = _("CSV Output"),
31 df = _("Disk Space Usage"),
32 disk = _("Disk Usage"),
33 dns = _("DNS"),
34 email = _("Email"),
35 entropy = _("Entropy"),
36 exec = _("Exec"),
37 interface = _("Interfaces"),
38 iptables = _("Firewall"),
39 irq = _("Interrupts"),
40 iwinfo = _("Wireless"),
41 load = _("System Load"),
42 memory = _("Memory"),
43 netlink = _("Netlink"),
44 network = _("Network"),
45 nut = _("UPS"),
46 olsrd = _("OLSRd"),
47 openvpn = _("OpenVPN"),
48 ping = _("Ping"),
49 processes = _("Processes"),
50 rrdtool = _("RRDTool"),
51 sensors = _("Sensors"),
52 splash_leases = _("Splash Leases"),
53 tcpconns = _("TCP Connections"),
54 thermal = _("Thermal"),
55 unixsock = _("UnixSock"),
56 uptime = _("Uptime")
57 }
58
59 -- our collectd menu
60 local collectd_menu = {
61 output = { "csv", "network", "rrdtool", "unixsock" },
62 general = { "contextswitch", "cpu", "cpufreq", "df", "disk", "email",
63 "entropy", "exec", "irq", "load", "memory",
64 "nut", "processes", "sensors", "thermal", "uptime" },
65 network = { "conntrack", "dns", "interface", "iptables",
66 "netlink", "olsrd", "openvpn", "ping",
67 "splash_leases", "tcpconns", "iwinfo" }
68 }
69
70 -- create toplevel menu nodes
71 local st = entry({"admin", "statistics"}, template("admin_statistics/index"), _("Statistics"), 80)
72 st.index = true
73
74 entry({"admin", "statistics", "collectd"}, cbi("luci_statistics/collectd"), _("Setup"), 20).subindex = true
75
76
77 -- populate collectd plugin menu
78 local index = 1
79 for section, plugins in luci.util.kspairs( collectd_menu ) do
80 local e = entry(
81 { "admin", "statistics", "collectd", section },
82 firstchild(), labels["s_"..section], index * 10
83 )
84
85 e.index = true
86
87 for j, plugin in luci.util.vspairs( plugins ) do
88 _entry(
89 { "admin", "statistics", "collectd", section, plugin },
90 cbi("luci_statistics/" .. plugin ),
91 labels[plugin], j * 10
92 )
93 end
94
95 index = index + 1
96 end
97
98 -- output views
99 local page = entry( { "admin", "statistics", "graph" }, template("admin_statistics/index"), _("Graphs"), 10)
100 page.setuser = "nobody"
101 page.setgroup = "nogroup"
102
103 local vars = luci.http.formvalue(nil, true)
104 local span = vars.timespan or nil
105 local host = vars.host or nil
106
107 -- get rrd data tree
108 local tree = luci.statistics.datatree.Instance(host)
109
110 local _, plugin, idx
111 for _, plugin, idx in luci.util.vspairs( tree:plugins() ) do
112
113 -- get plugin instances
114 local instances = tree:plugin_instances( plugin )
115
116 -- plugin menu entry
117 entry(
118 { "admin", "statistics", "graph", plugin },
119 call("statistics_render"), labels[plugin], idx
120 ).query = { timespan = span , host = host }
121
122 -- if more then one instance is found then generate submenu
123 if #instances > 1 then
124 local _, inst, idx2
125 for _, inst, idx2 in luci.util.vspairs(instances) do
126 -- instance menu entry
127 entry(
128 { "admin", "statistics", "graph", plugin, inst },
129 call("statistics_render"), inst, idx2
130 ).query = { timespan = span , host = host }
131 end
132 end
133 end
134 end
135
136 function statistics_render()
137
138 require("luci.statistics.rrdtool")
139 require("luci.template")
140 require("luci.model.uci")
141
142 local vars = luci.http.formvalue()
143 local req = luci.dispatcher.context.request
144 local path = luci.dispatcher.context.path
145 local uci = luci.model.uci.cursor()
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 host = vars.host or uci:get( "luci_statistics", "collectd", "Hostname" ) or luci.sys.hostname()
149 local opts = { host = vars.host }
150 local graph = luci.statistics.rrdtool.Graph( luci.util.parse_units( span ), opts )
151 local hosts = graph.tree:host_instances()
152
153 local is_index = false
154 local i, p, inst, idx
155
156 -- deliver image
157 if vars.img then
158 local l12 = require "luci.ltn12"
159 local png = io.open(graph.opts.imgpath .. "/" .. vars.img:gsub("%.+", "."), "r")
160 if png then
161 luci.http.prepare_content("image/png")
162 l12.pump.all(l12.source.file(png), luci.http.write)
163 end
164 return
165 end
166
167 local plugin, instances
168 local images = { }
169
170 -- find requested plugin and instance
171 for i, p in ipairs( luci.dispatcher.context.path ) do
172 if luci.dispatcher.context.path[i] == "graph" then
173 plugin = luci.dispatcher.context.path[i+1]
174 instances = { luci.dispatcher.context.path[i+2] }
175 end
176 end
177
178 -- no instance requested, find all instances
179 if #instances == 0 then
180 --instances = { graph.tree:plugin_instances( plugin )[1] }
181 instances = graph.tree:plugin_instances( plugin )
182 is_index = (#instances > 1)
183
184 -- index instance requested
185 elseif instances[1] == "-" then
186 instances[1] = ""
187 is_index = true
188 end
189
190
191 -- render graphs
192 for i, inst in luci.util.vspairs( instances ) do
193 for i, img in luci.util.vspairs( graph:render( plugin, inst, is_index ) ) do
194 table.insert( images, graph:strippngpath( img ) )
195 images[images[#images]] = inst
196 end
197 end
198
199 luci.template.render( "public_statistics/graph", {
200 images = images,
201 plugin = plugin,
202 timespans = spans,
203 current_timespan = span,
204 hosts = hosts,
205 current_host = host,
206 is_index = is_index
207 } )
208 end