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