vnstat.htm 2.1 KB

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