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.
5 module("luci.controller.luci_statistics.luci_statistics", package.seeall)
11 require("luci.statistics.datatree")
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
22 s_output = _("Output plugins"),
23 s_system = _("System plugins"),
24 s_network = _("Network plugins"),
26 conntrack = _("Conntrack"),
28 csv = _("CSV Output"),
29 df = _("Disk Space Usage"),
30 disk = _("Disk Usage"),
33 entropy = _("Entropy"),
35 interface = _("Interfaces"),
36 iptables = _("Firewall"),
37 irq = _("Interrupts"),
38 iwinfo = _("Wireless"),
39 load = _("System Load"),
41 netlink = _("Netlink"),
42 network = _("Network"),
46 processes = _("Processes"),
47 rrdtool = _("RRDTool"),
48 splash_leases = _("Splash Leases"),
49 tcpconns = _("TCP Connections"),
50 unixsock = _("UnixSock"),
55 local collectd_menu = {
56 output = { "csv", "network", "rrdtool", "unixsock" },
57 system = { "cpu", "df", "disk", "email", "entropy", "exec", "irq", "load", "memory", "nut", "processes", "uptime" },
58 network = { "conntrack", "dns", "interface", "iptables", "netlink", "olsrd", "ping", "splash_leases", "tcpconns", "iwinfo" }
61 -- create toplevel menu nodes
62 local st = entry({"admin", "statistics"}, template("admin_statistics/index"), _("Statistics"), 80)
65 entry({"admin", "statistics", "collectd"}, cbi("luci_statistics/collectd"), _("Collectd"), 10).subindex = true
68 -- populate collectd plugin menu
70 for section, plugins in luci.util.kspairs( collectd_menu ) do
72 { "admin", "statistics", "collectd", section },
73 firstchild(), labels["s_"..section], index * 10
78 for j, plugin in luci.util.vspairs( plugins ) do
80 { "admin", "statistics", "collectd", section, plugin },
81 cbi("luci_statistics/" .. plugin ),
82 labels[plugin], j * 10
90 local page = entry( { "admin", "statistics", "graph" }, template("admin_statistics/index"), _("Graphs"), 80)
91 page.setuser = "nobody"
92 page.setgroup = "nogroup"
94 local vars = luci.http.formvalue(nil, true)
95 local span = vars.timespan or nil
96 local host = vars.host or nil
99 local tree = luci.statistics.datatree.Instance(host)
102 for _, plugin, idx in luci.util.vspairs( tree:plugins() ) do
104 -- get plugin instances
105 local instances = tree:plugin_instances( plugin )
109 { "admin", "statistics", "graph", plugin },
110 call("statistics_render"), labels[plugin], idx
111 ).query = { timespan = span , host = host }
113 -- if more then one instance is found then generate submenu
114 if #instances > 1 then
116 for _, inst, idx2 in luci.util.vspairs(instances) do
117 -- instance menu entry
119 { "admin", "statistics", "graph", plugin, inst },
120 call("statistics_render"), inst, idx2
121 ).query = { timespan = span , host = host }
127 function statistics_render()
129 require("luci.statistics.rrdtool")
130 require("luci.template")
131 require("luci.model.uci")
133 local vars = luci.http.formvalue()
134 local req = luci.dispatcher.context.request
135 local path = luci.dispatcher.context.path
136 local uci = luci.model.uci.cursor()
137 local spans = luci.util.split( uci:get( "luci_statistics", "collectd_rrdtool", "RRATimespans" ), "%s+", nil, true )
138 local span = vars.timespan or uci:get( "luci_statistics", "rrdtool", "default_timespan" ) or spans[1]
139 local host = vars.host or uci:get( "luci_statistics", "collectd", "Hostname" ) or luci.sys.hostname()
140 local opts = { host = vars.host }
141 local graph = luci.statistics.rrdtool.Graph( luci.util.parse_units( span ), opts )
142 local hosts = graph.tree:host_instances()
144 local is_index = false
145 local i, p, inst, idx
149 local l12 = require "luci.ltn12"
150 local png = io.open(graph.opts.imgpath .. "/" .. vars.img:gsub("%.+", "."), "r")
152 luci.http.prepare_content("image/png")
153 l12.pump.all(l12.source.file(png), luci.http.write)
158 local plugin, instances
161 -- find requested plugin and instance
162 for i, p in ipairs( luci.dispatcher.context.path ) do
163 if luci.dispatcher.context.path[i] == "graph" then
164 plugin = luci.dispatcher.context.path[i+1]
165 instances = { luci.dispatcher.context.path[i+2] }
169 -- no instance requested, find all instances
170 if #instances == 0 then
171 --instances = { graph.tree:plugin_instances( plugin )[1] }
172 instances = graph.tree:plugin_instances( plugin )
175 -- index instance requested
176 elseif instances[1] == "-" then
183 for i, inst in luci.util.vspairs( instances ) do
184 for i, img in luci.util.vspairs( graph:render( plugin, inst, is_index ) ) do
185 table.insert( images, graph:strippngpath( img ) )
186 images[images[#images]] = inst
190 luci.template.render( "public_statistics/graph", {
194 current_timespan = span,