luci-app-statistics: add missing ACL rules
[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 -- create toplevel menu nodes
14 local st = entry({"admin", "statistics"}, template("admin_statistics/index"), _("Statistics"), 80)
15 st.index = true
16
17 entry({"admin", "statistics", "collectd"}, view("statistics/collectd"), _("Setup"), 20).subindex = true
18
19 -- output views
20 local page = entry( { "admin", "statistics", "graph" }, template("admin_statistics/index"), _("Graphs"), 10)
21 page.setuser = "nobody"
22 page.setgroup = "nogroup"
23
24 local vars = luci.http.formvalue(nil, true)
25 local span = vars.timespan or nil
26 local host = vars.host or nil
27
28 -- get rrd data tree
29 local tree = luci.statistics.datatree.Instance(host)
30
31 local _, plugin, idx
32 for _, plugin, idx in luci.util.vspairs( tree:plugins() ) do
33
34 -- get plugin instances
35 local instances = tree:plugin_instances( plugin )
36
37 -- load plugin menu entry from the description
38 local plugin_name = "luci.statistics.rrdtool.definitions." .. plugin
39 local stat, def = pcall( require, plugin_name )
40 if stat and def and type(def.item) == "function" then
41 entry(
42 { "admin", "statistics", "graph", plugin },
43 call("statistics_render"), def.item(), idx
44 ).query = { timespan = span , host = host }
45 end
46
47 -- if more then one instance is found then generate submenu
48 if #instances > 1 then
49 local _, inst, idx2
50 for _, inst, idx2 in luci.util.vspairs(instances) do
51 -- instance menu entry
52 entry(
53 { "admin", "statistics", "graph", plugin, inst },
54 call("statistics_render"), inst, idx2
55 ).query = { timespan = span , host = host }
56 end
57 end
58 end
59 end
60
61 function statistics_render()
62
63 require("luci.statistics.rrdtool")
64 require("luci.template")
65 require("luci.model.uci")
66
67 local vars = luci.http.formvalue()
68 local req = luci.dispatcher.context.request
69 local path = luci.dispatcher.context.path
70 local uci = luci.model.uci.cursor()
71 local spans = luci.util.split( uci:get( "luci_statistics", "collectd_rrdtool", "RRATimespans" ), "%s+", nil, true )
72 local span = vars.timespan or uci:get( "luci_statistics", "rrdtool", "default_timespan" ) or spans[1]
73 local host = vars.host or uci:get( "luci_statistics", "collectd", "Hostname" ) or luci.sys.hostname()
74 local opts = { host = vars.host }
75 local graph = luci.statistics.rrdtool.Graph( luci.util.parse_units( span ), opts )
76 local hosts = graph.tree:host_instances()
77
78 local is_index = false
79 local i, p, inst, idx
80
81 -- deliver image
82 if vars.img then
83 local l12 = require "luci.ltn12"
84 local png = io.open(graph.opts.imgpath .. "/" .. vars.img:gsub("%.+", "."), "r")
85 if png then
86 luci.http.prepare_content("image/png")
87 l12.pump.all(l12.source.file(png), luci.http.write)
88 end
89 return
90 end
91
92 local plugin, instances
93 local images = { }
94
95 -- find requested plugin and instance
96 for i, p in ipairs( luci.dispatcher.context.path ) do
97 if luci.dispatcher.context.path[i] == "graph" then
98 plugin = luci.dispatcher.context.path[i+1]
99 instances = { luci.dispatcher.context.path[i+2] }
100 end
101 end
102
103 -- no instance requested, find all instances
104 if #instances == 0 then
105 --instances = { graph.tree:plugin_instances( plugin )[1] }
106 instances = graph.tree:plugin_instances( plugin )
107 is_index = (#instances > 1)
108
109 -- index instance requested
110 elseif instances[1] == "-" then
111 instances[1] = ""
112 is_index = true
113 end
114
115 -- render graphs
116 for i, inst in luci.util.vspairs( instances ) do
117 for i, img in luci.util.vspairs( graph:render( plugin, inst, is_index ) ) do
118 table.insert( images, graph:strippngpath( img ) )
119 images[images[#images]] = inst
120 end
121 end
122
123 luci.template.render( "public_statistics/graph", {
124 images = images,
125 plugin = plugin,
126 timespans = spans,
127 current_timespan = span,
128 hosts = hosts,
129 current_host = host,
130 is_index = is_index
131 } )
132 end