statistics: Add support for entropy stats
[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 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
17                         entry( path, ... )
18                 end
19         end
20
21         local labels = {
22                 s_output        = _("Output plugins"),
23                 s_system        = _("System plugins"),
24                 s_network       = _("Network plugins"),
25
26                 conntrack       = _("Conntrack"),
27                 cpu                     = _("Processor"),
28                 csv                     = _("CSV Output"),
29                 df                      = _("Disk Space Usage"),
30                 disk            = _("Disk Usage"),
31                 dns                     = _("DNS"),
32                 email           = _("Email"),
33                 entropy         = _("Entropy"),
34                 exec            = _("Exec"),
35                 interface       = _("Interfaces"),
36                 iptables        = _("Firewall"),
37                 irq                     = _("Interrupts"),
38                 iwinfo          = _("Wireless"),
39                 load            = _("System Load"),
40                 memory          = _("Memory"),
41                 netlink         = _("Netlink"),
42                 network         = _("Network"),
43                 nut                     = _("UPS"),
44                 olsrd           = _("OLSRd"),
45                 ping            = _("Ping"),
46                 processes       = _("Processes"),
47                 rrdtool         = _("RRDTool"),
48                 splash_leases = _("Splash Leases"),
49                 tcpconns        = _("TCP Connections"),
50                 unixsock        = _("UnixSock"),
51                 uptime          = _("Uptime")
52         }
53
54         -- our collectd menu
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" }
59         }
60
61         -- create toplevel menu nodes
62         local st = entry({"admin", "statistics"}, template("admin_statistics/index"), _("Statistics"), 80)
63         st.index = true
64
65         entry({"admin", "statistics", "collectd"}, cbi("luci_statistics/collectd"), _("Collectd"), 10).subindex = true
66
67
68         -- populate collectd plugin menu
69         local index = 1
70         for section, plugins in luci.util.kspairs( collectd_menu ) do
71                 local e = entry(
72                         { "admin", "statistics", "collectd", section },
73                         firstchild(), labels["s_"..section], index * 10
74                 )
75
76                 e.index = true
77
78                 for j, plugin in luci.util.vspairs( plugins ) do
79                         _entry(
80                                 { "admin", "statistics", "collectd", section, plugin },
81                                 cbi("luci_statistics/" .. plugin ),
82                                 labels[plugin], j * 10
83                         )
84                 end
85
86                 index = index + 1
87         end
88
89         -- output views
90         local page = entry( { "admin", "statistics", "graph" }, template("admin_statistics/index"), _("Graphs"), 80)
91               page.setuser  = "nobody"
92               page.setgroup = "nogroup"
93
94         local vars = luci.http.formvalue(nil, true)
95         local span = vars.timespan or nil
96         local host = vars.host or nil
97
98         -- get rrd data tree
99         local tree = luci.statistics.datatree.Instance(host)
100
101         local _, plugin, idx
102         for _, plugin, idx in luci.util.vspairs( tree:plugins() ) do
103
104                 -- get plugin instances
105                 local instances = tree:plugin_instances( plugin )
106
107                 -- plugin menu entry
108                 entry(
109                         { "admin", "statistics", "graph", plugin },
110                         call("statistics_render"), labels[plugin], idx
111                 ).query = { timespan = span , host = host }
112
113                 -- if more then one instance is found then generate submenu
114                 if #instances > 1 then
115                         local _, inst, idx2
116                         for _, inst, idx2 in luci.util.vspairs(instances) do
117                                 -- instance menu entry
118                                 entry(
119                                         { "admin", "statistics", "graph", plugin, inst },
120                                         call("statistics_render"), inst, idx2
121                                 ).query = { timespan = span , host = host }
122                         end
123                 end
124         end
125 end
126
127 function statistics_render()
128
129         require("luci.statistics.rrdtool")
130         require("luci.template")
131         require("luci.model.uci")
132
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()
143
144         local is_index = false
145         local i, p, inst, idx
146
147         -- deliver image
148         if vars.img then
149                 local l12 = require "luci.ltn12"
150                 local png = io.open(graph.opts.imgpath .. "/" .. vars.img:gsub("%.+", "."), "r")
151                 if png then
152                         luci.http.prepare_content("image/png")
153                         l12.pump.all(l12.source.file(png), luci.http.write)
154                 end
155                 return
156         end
157
158         local plugin, instances
159         local images = { }
160
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] }
166         end
167     end
168
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 )
173                 is_index = true
174
175         -- index instance requested
176         elseif instances[1] == "-" then
177                 instances[1] = ""
178                 is_index = true
179         end
180
181
182         -- render graphs
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
187                 end
188         end
189
190         luci.template.render( "public_statistics/graph", {
191                 images           = images,
192                 plugin           = plugin,
193                 timespans        = spans,
194                 current_timespan = span,
195                 hosts            = hosts,
196                 current_host     = host,
197                 is_index         = is_index
198         } )
199 end