vnstat.htm 2.0 KB

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