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