2b8d9ff9c902a0e84e8cd9da49260974aba4466f
[project/luci.git] / applications / luci-app-vnstat / luasrc / view / vnstat.htm
1 <%#
2  Copyright 2010 Jo-Philipp Wich <jow@openwrt.org>
3  Licensed to the public under the Apache License 2.0.
4 -%>
5
6 <%-
7
8 local fs  = require "nixio.fs"
9 local sys = require "luci.sys"
10 local utl = require "luci.util"
11
12 local param = luci.http.formvalue
13
14 local iface = param("iface")
15 local style = param("style")
16
17 style = (style and #style > 0) and style or "s"
18
19
20 --
21 -- render image
22 --
23 if iface then
24         style = style:gsub("[^%w]", "")
25         iface = iface:gsub("[^%w%.%-%_]", "")
26
27         luci.http.prepare_content("image/png")
28
29         local png = io.popen("vnstati -i '%s' '-%s' -o -" % { iface, style })
30         luci.http.write(png:read("*a"))
31         png:close()
32
33         return
34
35 --
36 -- update database
37 --
38 else
39         sys.call("vnstat -u >/dev/null 2>/dev/null")
40 end
41
42
43 --
44 -- find databases
45 --
46 local dbdir, line
47
48 for line in io.lines("/etc/vnstat.conf") do
49         dbdir = line:match("^%s*DatabaseDir%s+[\"'](%S-)[\"']")
50         if dbdir then break end
51 end
52
53 dbdir = dbdir or "/var/lib/vnstat"
54
55 -%>
56
57 <%+header%>
58
59 <h2 name="content"><%:VnStat Graphs%></h2>
60
61 <form action="" method="get">
62
63 <select name="style">
64         <option value="s"<%=(style == "s") and ' selected="selected"' or ''%>><%:Summary display%></option>
65         <option value="t"<%=(style == "t") and ' selected="selected"' or ''%>><%:Top 10 display%></option>
66         <option value="h"<%=(style == "h") and ' selected="selected"' or ''%>><%:Hourly traffic%></option>
67         <option value="d"<%=(style == "d") and ' selected="selected"' or ''%>><%:Daily traffic%></option>
68         <option value="m"<%=(style == "m") and ' selected="selected"' or ''%>><%:Monthly traffic%></option>
69 </select>
70
71 <input type="submit" value="<%:Update ยป%>" />
72
73 </form>
74
75 <br /><hr /><br />
76
77 <div style="text-align:center">
78 <%
79    empty = true
80    ifdir = fs.dir(dbdir)
81
82    if ifdir then
83      for iface in ifdir do
84        if iface:sub(1,1) ~= "." then
85          empty = false
86 %>
87         <img src="<%=REQUEST_URI%>?iface=<%=iface%>&amp;style=<%=param('style')%>" alt="" />
88         <br /><br />
89 <%
90        end
91      end
92    end 
93 %>
94
95 <% if empty then %>
96 <p><em><%:No database has been set up yet. Go to the VnStat configuration and enable monitoring for one or more interfaces.%></em></p>
97 <% end %>
98
99 </div>
100
101 <%+footer%>