add support for splash leases plugin
[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 (c) 2012 Jo-Philipp Wich <xm@subsignal.org>
6
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
10
11         http://www.apache.org/licenses/LICENSE-2.0
12
13 ]]--
14
15 module("luci.controller.luci_statistics.luci_statistics", package.seeall)
16
17 function index()
18
19         require("nixio.fs")
20         require("luci.util")
21         require("luci.statistics.datatree")
22
23         -- override entry(): check for existance <plugin>.so where <plugin> is derived from the called path
24         function _entry( path, ... )
25                 local file = path[5] or path[4]
26                 if nixio.fs.access( "/usr/lib/collectd/" .. file .. ".so" ) then
27                         entry( path, ... )
28                 end
29         end
30
31         local labels = {
32                 s_output        = _("Output plugins"),
33                 s_system        = _("System plugins"),
34                 s_network       = _("Network plugins"),
35
36                 conntrack       = _("Conntrack"),
37                 cpu                     = _("Processor"),
38                 csv                     = _("CSV Output"),
39                 df                      = _("Disk Space Usage"),
40                 disk            = _("Disk Usage"),
41                 dns                     = _("DNS"),
42                 email           = _("Email"),
43                 exec            = _("Exec"),
44                 interface       = _("Interfaces"),
45                 iptables        = _("Firewall"),
46                 irq                     = _("Interrupts"),
47                 iwinfo          = _("Wireless"),
48                 load            = _("System Load"),
49                 memory          = _("Memory"),
50                 netlink         = _("Netlink"),
51                 network         = _("Network"),
52                 nut                     = _("UPS"),
53                 olsrd           = _("OLSRd"),
54                 ping            = _("Ping"),
55                 processes       = _("Processes"),
56                 rrdtool         = _("RRDTool"),
57                 splash_leases = _("Splash Leases"),
58                 tcpconns        = _("TCP Connections"),
59                 unixsock        = _("UnixSock"),
60                 uptime          = _("Uptime")
61         }
62
63         -- our collectd menu
64         local collectd_menu = {
65                 output  = { "csv", "network", "rrdtool", "unixsock" },
66                 system  = { "cpu", "df", "disk", "email", "exec", "irq", "load", "memory", "nut", "processes", "uptime" },
67                 network = { "conntrack", "dns", "interface", "iptables", "netlink", "olsrd", "ping", "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"), _("Collectd"), 10).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"), 80)
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                         png:close()
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 = true
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