4c5db17d83109625cc040e635d0204f4a944cd06
[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 existence <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
27 -- our collectd menu
28 local collectd_menu = {
29 output = { },
30 general = { },
31 network = { }
32 }
33
34 local plugin_dir = "/usr/lib/lua/luci/statistics/plugins/"
35 for filename in nixio.fs.dir(plugin_dir) do
36 local plugin_fun = loadfile(plugin_dir .. filename)
37 setfenv(plugin_fun, { _ = luci.i18n.translate })
38 local plugin = plugin_fun()
39 local name = filename:gsub("%.lua", "")
40 table.insert(collectd_menu[plugin.category], name)
41 labels[name] = plugin.label
42 end
43
44 -- create toplevel menu nodes
45 local st = entry({"admin", "statistics"}, template("admin_statistics/index"), _("Statistics"), 80)
46 st.index = true
47
48 entry({"admin", "statistics", "collectd"}, cbi("luci_statistics/collectd"), _("Setup"), 20).subindex = true
49
50
51 -- populate collectd plugin menu
52 local index = 1
53 for section, plugins in luci.util.kspairs( collectd_menu ) do
54 local e = entry(
55 { "admin", "statistics", "collectd", section },
56 firstchild(), labels["s_"..section], index * 10
57 )
58
59 e.index = true
60
61 for j, plugin in luci.util.vspairs( plugins ) do
62 _entry(
63 { "admin", "statistics", "collectd", section, plugin },
64 cbi("luci_statistics/" .. plugin ),
65 labels[plugin] or plugin, j * 10
66 )
67 end
68
69 index = index + 1
70 end
71
72 -- output views
73 local page = entry( { "admin", "statistics", "graph" }, template("admin_statistics/index"), _("Graphs"), 10)
74 page.setuser = "nobody"
75 page.setgroup = "nogroup"
76
77 local vars = luci.http.formvalue(nil, true)
78 local span = vars.timespan or nil
79 local host = vars.host or nil
80
81 -- get rrd data tree
82 local tree = luci.statistics.datatree.Instance(host)
83
84 local _, plugin, idx
85 for _, plugin, idx in luci.util.vspairs( tree:plugins() ) do
86
87 -- get plugin instances
88 local instances = tree:plugin_instances( plugin )
89
90 -- load plugin menu entry from the description
91 local plugin_name = "luci.statistics.rrdtool.definitions." .. plugin
92 local stat, def = pcall( require, plugin_name )
93 if stat and def and type(def.item) == "function" then
94 entry(
95 { "admin", "statistics", "graph", plugin },
96 call("statistics_render"), def.item(), idx
97 ).query = { timespan = span , host = host }
98 end
99
100 -- if more then one instance is found then generate submenu
101 if #instances > 1 then
102 local _, inst, idx2
103 for _, inst, idx2 in luci.util.vspairs(instances) do
104 -- instance menu entry
105 entry(
106 { "admin", "statistics", "graph", plugin, inst },
107 call("statistics_render"), inst, idx2
108 ).query = { timespan = span , host = host }
109 end
110 end
111 end
112 end
113
114 function statistics_render()
115
116 require("luci.statistics.rrdtool")
117 require("luci.template")
118 require("luci.model.uci")
119
120 local vars = luci.http.formvalue()
121 local req = luci.dispatcher.context.request
122 local path = luci.dispatcher.context.path
123 local uci = luci.model.uci.cursor()
124 local spans = luci.util.split( uci:get( "luci_statistics", "collectd_rrdtool", "RRATimespans" ), "%s+", nil, true )
125 local span = vars.timespan or uci:get( "luci_statistics", "rrdtool", "default_timespan" ) or spans[1]
126 local host = vars.host or uci:get( "luci_statistics", "collectd", "Hostname" ) or luci.sys.hostname()
127 local opts = { host = vars.host }
128 local graph = luci.statistics.rrdtool.Graph( luci.util.parse_units( span ), opts )
129 local hosts = graph.tree:host_instances()
130
131 local is_index = false
132 local i, p, inst, idx
133
134 -- deliver image
135 if vars.img then
136 local l12 = require "luci.ltn12"
137 local png = io.open(graph.opts.imgpath .. "/" .. vars.img:gsub("%.+", "."), "r")
138 if png then
139 luci.http.prepare_content("image/png")
140 l12.pump.all(l12.source.file(png), luci.http.write)
141 end
142 return
143 end
144
145 local plugin, instances
146 local images = { }
147
148 -- find requested plugin and instance
149 for i, p in ipairs( luci.dispatcher.context.path ) do
150 if luci.dispatcher.context.path[i] == "graph" then
151 plugin = luci.dispatcher.context.path[i+1]
152 instances = { luci.dispatcher.context.path[i+2] }
153 end
154 end
155
156 -- no instance requested, find all instances
157 if #instances == 0 then
158 --instances = { graph.tree:plugin_instances( plugin )[1] }
159 instances = graph.tree:plugin_instances( plugin )
160 is_index = (#instances > 1)
161
162 -- index instance requested
163 elseif instances[1] == "-" then
164 instances[1] = ""
165 is_index = true
166 end
167
168 -- render graphs
169 for i, inst in luci.util.vspairs( instances ) do
170 for i, img in luci.util.vspairs( graph:render( plugin, inst, is_index ) ) do
171 table.insert( images, graph:strippngpath( img ) )
172 images[images[#images]] = inst
173 end
174 end
175
176 luci.template.render( "public_statistics/graph", {
177 images = images,
178 plugin = plugin,
179 timespans = spans,
180 current_timespan = span,
181 hosts = hosts,
182 current_host = host,
183 is_index = is_index
184 } )
185 end