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