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