Merge pull request #656 from nlhintz/pull-request
[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                 cpu                     = _("Processor"),
28                 cpufreq         = _("CPU Frequency"),
29                 csv                     = _("CSV Output"),
30                 df                      = _("Disk Space Usage"),
31                 disk            = _("Disk Usage"),
32                 dns                     = _("DNS"),
33                 email           = _("Email"),
34                 entropy         = _("Entropy"),
35                 exec            = _("Exec"),
36                 interface       = _("Interfaces"),
37                 iptables        = _("Firewall"),
38                 irq                     = _("Interrupts"),
39                 iwinfo          = _("Wireless"),
40                 load            = _("System Load"),
41                 memory          = _("Memory"),
42                 netlink         = _("Netlink"),
43                 network         = _("Network"),
44                 nut                     = _("UPS"),
45                 olsrd           = _("OLSRd"),
46                 openvpn         = _("OpenVPN"),
47                 ping            = _("Ping"),
48                 processes       = _("Processes"),
49                 rrdtool         = _("RRDTool"),
50                 sensors     = _("Sensors"),
51                 splash_leases = _("Splash Leases"),
52                 tcpconns        = _("TCP Connections"),
53                 thermal =       _("Thermal"),
54                 unixsock        = _("UnixSock"),
55                 uptime          = _("Uptime")
56         }
57
58         -- our collectd menu
59         local collectd_menu = {
60                 output  = { "csv", "network", "rrdtool", "unixsock" },
61                 general = { "cpu", "cpufreq", "df", "disk", "email",
62                         "entropy", "exec", "irq", "load", "memory",
63                         "nut", "processes", "sensors", "thermal", "uptime" },
64                 network = { "conntrack", "dns", "interface", "iptables",
65                         "netlink", "olsrd", "openvpn", "ping",
66                         "splash_leases", "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"), _("Setup"), 20).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"), 10)
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         local _, plugin, idx
110         for _, plugin, idx in luci.util.vspairs( tree:plugins() ) do
111
112                 -- get plugin instances
113                 local instances = tree:plugin_instances( plugin )
114
115                 -- plugin menu entry
116                 entry(
117                         { "admin", "statistics", "graph", plugin },
118                         call("statistics_render"), labels[plugin], idx
119                 ).query = { timespan = span , host = host }
120
121                 -- if more then one instance is found then generate submenu
122                 if #instances > 1 then
123                         local _, inst, idx2
124                         for _, inst, idx2 in luci.util.vspairs(instances) do
125                                 -- instance menu entry
126                                 entry(
127                                         { "admin", "statistics", "graph", plugin, inst },
128                                         call("statistics_render"), inst, idx2
129                                 ).query = { timespan = span , host = host }
130                         end
131                 end
132         end
133 end
134
135 function statistics_render()
136
137         require("luci.statistics.rrdtool")
138         require("luci.template")
139         require("luci.model.uci")
140
141         local vars  = luci.http.formvalue()
142         local req   = luci.dispatcher.context.request
143         local path  = luci.dispatcher.context.path
144         local uci   = luci.model.uci.cursor()
145         local spans = luci.util.split( uci:get( "luci_statistics", "collectd_rrdtool", "RRATimespans" ), "%s+", nil, true )
146         local span  = vars.timespan or uci:get( "luci_statistics", "rrdtool", "default_timespan" ) or spans[1]
147         local host  = vars.host     or uci:get( "luci_statistics", "collectd", "Hostname" ) or luci.sys.hostname()
148         local opts = { host = vars.host }
149         local graph = luci.statistics.rrdtool.Graph( luci.util.parse_units( span ), opts )
150         local hosts = graph.tree:host_instances()
151
152         local is_index = false
153         local i, p, inst, idx
154
155         -- deliver image
156         if vars.img then
157                 local l12 = require "luci.ltn12"
158                 local png = io.open(graph.opts.imgpath .. "/" .. vars.img:gsub("%.+", "."), "r")
159                 if png then
160                         luci.http.prepare_content("image/png")
161                         l12.pump.all(l12.source.file(png), luci.http.write)
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 = (#instances > 1)
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