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