applications: add luci-vnstat, frontend for the VnStat traffic monitor
[project/luci.git] / applications / luci-vnstat / luasrc / view / vnstat.htm
1 <%#
2 LuCI - Lua Configuration Interface
3 Copyright 2010 Jo-Philipp Wich <xm@subsignal.org>
4
5 Licensed under the Apache License, Version 2.0 (the "License");
6 you may not use this file except in compliance with the License.
7 You may obtain a copy of the License at
8
9 http://www.apache.org/licenses/LICENSE-2.0
10
11 -%>
12
13 <%-
14
15 local fs = require "nixio.fs"
16 local sys = require "luci.sys"
17 local utl = require "luci.util"
18
19 local param = luci.http.formvalue
20
21 local iface = param("iface")
22 local style = param("style")
23
24 style = (style and #style > 0) and style or "s"
25
26
27 --
28 -- render image
29 --
30 if iface then
31 luci.http.prepare_content("image/png")
32
33 local png = io.popen("vnstati -i %q -%q -o -" % { iface, style })
34 luci.http.write(png:read("*a"))
35 png:close()
36
37 return
38
39 --
40 -- update database
41 --
42 else
43 sys.call("vnstat -u >/dev/null 2>/dev/null")
44 end
45
46
47 --
48 -- find databases
49 --
50 local dbdir, line
51
52 for line in io.lines("/etc/vnstat.conf") do
53 dbdir = line:match("^%s*DatabaseDir%s+[\"'](%S-)[\"']")
54 if dbdir then break end
55 end
56
57 dbdir = dbdir or "/var/lib/vnstat"
58
59 -%>
60
61 <%+header%>
62
63 <h2><a id="content" name="content"><%:VnStat Graphs%></a></h2>
64
65 <form action="" method="get">
66
67 <select name="style">
68 <option value="s"<%=(style == "s") and ' selected="selected"' or ''%>><%:Summary display%></option>
69 <option value="t"<%=(style == "t") and ' selected="selected"' or ''%>><%:Top 10 display%></option>
70 <option value="h"<%=(style == "h") and ' selected="selected"' or ''%>><%:Hourly traffic%></option>
71 <option value="d"<%=(style == "d") and ' selected="selected"' or ''%>><%:Daily traffic%></option>
72 <option value="m"<%=(style == "m") and ' selected="selected"' or ''%>><%:Monthly traffic%></option>
73 </select>
74
75 <input type="submit" value="<%:Update ยป%>" />
76
77 </form>
78
79 <br /><hr /><br />
80
81 <div style="text-align:center">
82 <% for iface in fs.dir(dbdir) do if iface:sub(1,1) ~= "." then %>
83 <img src="<%=REQUEST_URI%>?iface=<%=iface%>&amp;style=<%=param('style')%>" alt="" />
84 <br /><br />
85 <% end end %>
86 </div>
87
88 <%+footer%>