1
0

multiwan.lua 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. module("luci.controller.multiwan", package.seeall)
  2. function index()
  3. local fs = require "nixio.fs"
  4. if not fs.access("/etc/config/multiwan") then
  5. return
  6. end
  7. local page
  8. page = entry({"admin", "network", "multiwan"}, cbi("multiwan/multiwan"), _("Multi-WAN"))
  9. page.dependent = true
  10. entry({"admin", "network", "multiwan", "status"}, call("multiwan_status"))
  11. page = entry({"mini", "network", "multiwan"}, cbi("multiwan/multiwanmini", {autoapply=true}), _("Multi-WAN"))
  12. page.dependent = true
  13. end
  14. function multiwan_status()
  15. local nfs = require "nixio.fs"
  16. local cachefile = "/tmp/.mwan/cache"
  17. local rv = { }
  18. cachefile = nfs.readfile(cachefile)
  19. if cachefile then
  20. local ntm = require "luci.model.network".init()
  21. _, _, wan_if_map = string.find(cachefile, "wan_if_map=\"([^\"]*)\"")
  22. _, _, wan_fail_map = string.find(cachefile, "wan_fail_map=\"([^\"]*)\"")
  23. _, _, wan_recovery_map = string.find(cachefile, "wan_recovery_map=\"([^\"]*)\"")
  24. rv.wans = { }
  25. wansid = {}
  26. for wanname, wanifname in string.gfind(wan_if_map, "([^%[]+)%[([^%]]+)%]") do
  27. local wanlink = ntm:get_interface(wanifname)
  28. wanlink = wanlink and wanlink:get_network()
  29. wanlink = wanlink and wanlink:adminlink() or "#"
  30. wansid[wanname] = #rv.wans + 1
  31. rv.wans[wansid[wanname]] = { name = wanname, link = wanlink, ifname = wanifname, status = "ok", count = 0 }
  32. end
  33. for wanname, failcount in string.gfind(wan_fail_map, "([^%[]+)%[([^%]]+)%]") do
  34. if failcount == "x" then
  35. rv.wans[wansid[wanname]].status = "ko"
  36. else
  37. rv.wans[wansid[wanname]].status = "failing"
  38. rv.wans[wansid[wanname]].count = failcount
  39. end
  40. end
  41. for wanname, recoverycount in string.gfind(wan_recovery_map, "([^%[]+)%[([^%]]+)%]") do
  42. rv.wans[wansid[wanname]].status = "recovering"
  43. rv.wans[wansid[wanname]].count = recoverycount
  44. end
  45. end
  46. luci.http.prepare_content("application/json")
  47. luci.http.write_json(rv)
  48. end