nft-qos.lua 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. -- Copyright 2018 Rosy Song <rosysong@rosinson.com>
  2. -- Licensed to the public under the Apache License 2.0.
  3. module("luci.controller.nft-qos", package.seeall)
  4. function index()
  5. if not nixio.fs.access("/etc/config/nft-qos") then
  6. return
  7. end
  8. entry({"admin", "status", "realtime", "rate"},
  9. template("nft-qos/rate"), _("Rate"), 5).leaf = true
  10. entry({"admin", "status", "realtime", "rate_status"},
  11. call("action_rate")).leaf = true
  12. entry({"admin", "services", "nft-qos"}, cbi("nft-qos/nft-qos"),
  13. _("Qos over Nftables"), 60)
  14. end
  15. function _action_rate(rv, n)
  16. local c = io.popen("nft list chain inet nft-qos-monitor " .. n .. " 2>/dev/null")
  17. if c then
  18. for l in c:lines() do
  19. local _, i, p, b = l:match('^%s+ip ([^%s]+) ([^%s]+) counter packets (%d+) bytes (%d+)')
  20. if i and p and b then
  21. -- handle expression
  22. local r = {
  23. rule = {
  24. family = "inet",
  25. table = "nft-qos-monitor",
  26. chain = n,
  27. handle = 0,
  28. expr = {
  29. { match = { right = i } },
  30. { counter = { packets = p, bytes = b } }
  31. }
  32. }
  33. }
  34. rv[#rv + 1] = r
  35. end
  36. end
  37. c:close()
  38. end
  39. end
  40. function action_rate()
  41. luci.http.prepare_content("application/json")
  42. local data = { nftables = {} }
  43. _action_rate(data.nftables, "upload")
  44. _action_rate(data.nftables, "download")
  45. luci.http.write_json(data)
  46. end