Merge pull request #2209 from jinchizhong/luci-app-statistics-add-curl
[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 -- plugin menu entry
119 entry(
120 { "admin", "statistics", "graph", plugin },
121 call("statistics_render"), labels[plugin], idx
122 ).query = { timespan = span , host = host }
123
124 -- if more then one instance is found then generate submenu
125 if #instances > 1 then
126 local _, inst, idx2
127 for _, inst, idx2 in luci.util.vspairs(instances) do
128 -- instance menu entry
129 entry(
130 { "admin", "statistics", "graph", plugin, inst },
131 call("statistics_render"), inst, idx2
132 ).query = { timespan = span , host = host }
133 end
134 end
135 end
136 end
137
138 function statistics_render()
139
140 require("luci.statistics.rrdtool")
141 require("luci.template")
142 require("luci.model.uci")
143
144 local vars = luci.http.formvalue()
145 local req = luci.dispatcher.context.request
146 local path = luci.dispatcher.context.path
147 local uci = luci.model.uci.cursor()
148 local spans = luci.util.split( uci:get( "luci_statistics", "collectd_rrdtool", "RRATimespans" ), "%s+", nil, true )
149 local span = vars.timespan or uci:get( "luci_statistics", "rrdtool", "default_timespan" ) or spans[1]
150 local host = vars.host or uci:get( "luci_statistics", "collectd", "Hostname" ) or luci.sys.hostname()
151 local opts = { host = vars.host }
152 local graph = luci.statistics.rrdtool.Graph( luci.util.parse_units( span ), opts )
153 local hosts = graph.tree:host_instances()
154
155 local is_index = false
156 local i, p, inst, idx
157
158 -- deliver image
159 if vars.img then
160 local l12 = require "luci.ltn12"
161 local png = io.open(graph.opts.imgpath .. "/" .. vars.img:gsub("%.+", "."), "r")
162 if png then
163 luci.http.prepare_content("image/png")
164 l12.pump.all(l12.source.file(png), luci.http.write)
165 end
166 return
167 end
168
169 local plugin, instances
170 local images = { }
171
172 -- find requested plugin and instance
173 for i, p in ipairs( luci.dispatcher.context.path ) do
174 if luci.dispatcher.context.path[i] == "graph" then
175 plugin = luci.dispatcher.context.path[i+1]
176 instances = { luci.dispatcher.context.path[i+2] }
177 end
178 end
179
180 -- no instance requested, find all instances
181 if #instances == 0 then
182 --instances = { graph.tree:plugin_instances( plugin )[1] }
183 instances = graph.tree:plugin_instances( plugin )
184 is_index = (#instances > 1)
185
186 -- index instance requested
187 elseif instances[1] == "-" then
188 instances[1] = ""
189 is_index = true
190 end
191
192
193 -- render graphs
194 for i, inst in luci.util.vspairs( instances ) do
195 for i, img in luci.util.vspairs( graph:render( plugin, inst, is_index ) ) do
196 table.insert( images, graph:strippngpath( img ) )
197 images[images[#images]] = inst
198 end
199 end
200
201 luci.template.render( "public_statistics/graph", {
202 images = images,
203 plugin = plugin,
204 timespans = spans,
205 current_timespan = span,
206 hosts = hosts,
207 current_host = host,
208 is_index = is_index
209 } )
210 end