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