Add support for collectd-mod-uptime to luci-statistics
[project/luci.git] / applications / luci-statistics / luasrc / controller / luci_statistics / luci_statistics.lua
1 --[[
2
3 Luci statistics - statistics controller module
4 (c) 2008 Freifunk Leipzig / Jo-Philipp Wich <xm@leipzig.freifunk.net>
5 (c) 2012 Jo-Philipp Wich <xm@subsignal.org>
6
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
10
11         http://www.apache.org/licenses/LICENSE-2.0
12
13 ]]--
14
15 module("luci.controller.luci_statistics.luci_statistics", package.seeall)
16
17 function index()
18
19         require("nixio.fs")
20         require("luci.util")
21         require("luci.statistics.datatree")
22
23         -- override entry(): check for existance <plugin>.so where <plugin> is derived from the called path
24         function _entry( path, ... )
25                 local file = path[5] or path[4]
26                 if nixio.fs.access( "/usr/lib/collectd/" .. file .. ".so" ) then
27                         entry( path, ... )
28                 end
29         end
30
31         local labels = {
32                 s_output        = _("Output plugins"),
33                 s_system        = _("System plugins"),
34                 s_network       = _("Network plugins"),
35
36                 conntrack       = _("Conntrack"),
37                 cpu                     = _("Processor"),
38                 csv                     = _("CSV Output"),
39                 df                      = _("Disk Space Usage"),
40                 disk            = _("Disk Usage"),
41                 dns                     = _("DNS"),
42                 email           = _("Email"),
43                 exec            = _("Exec"),
44                 interface       = _("Interfaces"),
45                 iptables        = _("Firewall"),
46                 irq                     = _("Interrupts"),
47                 iwinfo          = _("Wireless"),
48                 load            = _("System Load"),
49                 memory          = _("Memory"),
50                 netlink         = _("Netlink"),
51                 network         = _("Network"),
52                 nut                     = _("UPS"),
53                 olsrd           = _("OLSRd"),
54                 ping            = _("Ping"),
55                 processes       = _("Processes"),
56                 rrdtool         = _("RRDTool"),
57                 tcpconns        = _("TCP Connections"),
58                 unixsock        = _("UnixSock"),
59                 uptime          = _("Uptime")
60         }
61
62         -- our collectd menu
63         local collectd_menu = {
64                 output  = { "csv", "network", "rrdtool", "unixsock" },
65                 system  = { "cpu", "df", "disk", "email", "exec", "irq", "load", "memory", "nut", "processes", "uptime" },
66                 network = { "conntrack", "dns", "interface", "iptables", "netlink", "olsrd", "ping", "tcpconns", "iwinfo" }
67         }
68
69         -- create toplevel menu nodes
70         local st = entry({"admin", "statistics"}, template("admin_statistics/index"), _("Statistics"), 80)
71         st.index = true
72
73         entry({"admin", "statistics", "collectd"}, cbi("luci_statistics/collectd"), _("Collectd"), 10).subindex = true
74
75
76         -- populate collectd plugin menu
77         local index = 1
78         for section, plugins in luci.util.kspairs( collectd_menu ) do
79                 local e = entry(
80                         { "admin", "statistics", "collectd", section },
81                         firstchild(), labels["s_"..section], index * 10
82                 )
83
84                 e.index = true
85
86                 for j, plugin in luci.util.vspairs( plugins ) do
87                         _entry(
88                                 { "admin", "statistics", "collectd", section, plugin },
89                                 cbi("luci_statistics/" .. plugin ),
90                                 labels[plugin], j * 10
91                         )
92                 end
93
94                 index = index + 1
95         end
96
97         -- output views
98         local page = entry( { "admin", "statistics", "graph" }, template("admin_statistics/index"), _("Graphs"), 80)
99               page.setuser  = "nobody"
100               page.setgroup = "nogroup"
101
102         local vars = luci.http.formvalue(nil, true)
103         local span = vars.timespan or nil
104         local host = vars.host or nil
105
106         -- get rrd data tree
107         local tree = luci.statistics.datatree.Instance(host)
108
109         local _, plugin, idx
110         for _, plugin, idx in luci.util.vspairs( tree:plugins() ) do
111
112                 -- get plugin instances
113                 local instances = tree:plugin_instances( plugin )
114
115                 -- plugin menu entry
116                 entry(
117                         { "admin", "statistics", "graph", plugin },
118                         call("statistics_render"), labels[plugin], idx
119                 ).query = { timespan = span , host = host }
120
121                 -- if more then one instance is found then generate submenu
122                 if #instances > 1 then
123                         local _, inst, idx2
124                         for _, inst, idx2 in luci.util.vspairs(instances) do
125                                 -- instance menu entry
126                                 entry(
127                                         { "admin", "statistics", "graph", plugin, inst },
128                                         call("statistics_render"), inst, idx2
129                                 ).query = { timespan = span , host = host }
130                         end
131                 end
132         end
133 end
134
135 function statistics_render()
136
137         require("luci.statistics.rrdtool")
138         require("luci.template")
139         require("luci.model.uci")
140
141         local vars  = luci.http.formvalue()
142         local req   = luci.dispatcher.context.request
143         local path  = luci.dispatcher.context.path
144         local uci   = luci.model.uci.cursor()
145         local spans = luci.util.split( uci:get( "luci_statistics", "collectd_rrdtool", "RRATimespans" ), "%s+", nil, true )
146         local span  = vars.timespan or uci:get( "luci_statistics", "rrdtool", "default_timespan" ) or spans[1]
147         local host  = vars.host     or uci:get( "luci_statistics", "collectd", "Hostname" ) or luci.sys.hostname()
148         local opts = { host = vars.host }
149         local graph = luci.statistics.rrdtool.Graph( luci.util.parse_units( span ), opts )
150         local hosts = graph.tree:host_instances()
151
152         local is_index = false
153         local i, p, inst, idx
154
155         -- deliver image
156         if vars.img then
157                 local l12 = require "luci.ltn12"
158                 local png = io.open(graph.opts.imgpath .. "/" .. vars.img:gsub("%.+", "."), "r")
159                 if png then
160                         luci.http.prepare_content("image/png")
161                         l12.pump.all(l12.source.file(png), luci.http.write)
162                         png:close()
163                 end
164                 return
165         end
166
167         local plugin, instances
168         local images = { }
169
170         -- find requested plugin and instance
171     for i, p in ipairs( luci.dispatcher.context.path ) do
172         if luci.dispatcher.context.path[i] == "graph" then
173             plugin    = luci.dispatcher.context.path[i+1]
174             instances = { luci.dispatcher.context.path[i+2] }
175         end
176     end
177
178         -- no instance requested, find all instances
179         if #instances == 0 then
180                 --instances = { graph.tree:plugin_instances( plugin )[1] }
181                 instances = graph.tree:plugin_instances( plugin )
182                 is_index = true
183
184         -- index instance requested
185         elseif instances[1] == "-" then
186                 instances[1] = ""
187                 is_index = true
188         end
189
190
191         -- render graphs
192         for i, inst in luci.util.vspairs( instances ) do
193                 for i, img in luci.util.vspairs( graph:render( plugin, inst, is_index ) ) do
194                         table.insert( images, graph:strippngpath( img ) )
195                         images[images[#images]] = inst
196                 end
197         end
198
199         luci.template.render( "public_statistics/graph", {
200                 images           = images,
201                 plugin           = plugin,
202                 timespans        = spans,
203                 current_timespan = span,
204                 hosts            = hosts,
205                 current_host     = host,
206                 is_index         = is_index
207         } )
208 end