42d7d2404b8b37e6b644682571f29576c29f59f9
[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         luci.http.prepare_content("image/png")
25
26         local png = io.popen("vnstati -i %s -%s -o -" %{
27                 utl.shellquote(iface),
28                 utl.shellquote(style)
29         })
30
31         luci.http.write(png:read("*a"))
32         png:close()
33
34         return
35
36 --
37 -- update database
38 --
39 else
40         sys.call("vnstat -u >/dev/null 2>/dev/null")
41 end
42
43
44 --
45 -- find databases
46 --
47 local dbdir, line
48
49 for line in io.lines("/etc/vnstat.conf") do
50         dbdir = line:match("^%s*DatabaseDir%s+[\"'](%S-)[\"']")
51         if dbdir then break end
52 end
53
54 dbdir = dbdir or "/var/lib/vnstat"
55
56 -%>
57
58 <%+header%>
59
60 <h2 name="content"><%:VnStat Graphs%></h2>
61
62 <form action="" method="get">
63
64 <select name="style">
65         <option value="s"<%=(style == "s") and ' selected="selected"' or ''%>><%:Summary display%></option>
66         <option value="t"<%=(style == "t") and ' selected="selected"' or ''%>><%:Top 10 display%></option>
67         <option value="h"<%=(style == "h") and ' selected="selected"' or ''%>><%:Hourly traffic%></option>
68         <option value="d"<%=(style == "d") and ' selected="selected"' or ''%>><%:Daily traffic%></option>
69         <option value="m"<%=(style == "m") and ' selected="selected"' or ''%>><%:Monthly traffic%></option>
70 </select>
71
72 <input type="submit" value="<%:Update ยป%>" />
73
74 </form>
75
76 <br /><hr /><br />
77
78 <div style="text-align:center">
79 <%
80    empty = true
81    ifdir = fs.dir(dbdir)
82
83    if ifdir then
84      for iface in ifdir do
85        if iface:sub(1,1) ~= "." then
86          empty = false
87 %>
88         <img src="<%=REQUEST_URI%>?iface=<%=iface%>&amp;style=<%=param('style')%>" alt="" />
89         <br /><br />
90 <%
91        end
92      end
93    end
94 %>
95
96 <% if empty then %>
97 <p><em><%:No database has been set up yet. Go to the VnStat configuration and enable monitoring for one or more interfaces.%></em></p>
98 <% end %>
99
100 </div>
101
102 <%+footer%>