interface.lua 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. -- ------ extra functions ------ --
  2. function interfaceCheck() -- find issues with too many interfaces, reliability and metric
  3. uci.cursor():foreach("mwan3", "interface",
  4. function (section)
  5. local interfaceName = section[".name"]
  6. interfaceNumber = interfaceNumber+1 -- count number of mwan interfaces configured
  7. -- create list of metrics for none and duplicate checking
  8. local metricValue = ut.trim(sys.exec("uci -p /var/state get network." .. interfaceName .. ".metric"))
  9. if metricValue == "" then
  10. errorFound = 1
  11. errorNoMetricList = errorNoMetricList .. interfaceName .. " "
  12. else
  13. metricList = metricList .. interfaceName .. " " .. metricValue .. "\n"
  14. end
  15. -- check if any interfaces have a higher reliability requirement than tracking IPs configured
  16. local trackingNumber = tonumber(ut.trim(sys.exec("echo $(uci -p /var/state get mwan3." .. interfaceName .. ".track_ip) | wc -w")))
  17. if trackingNumber > 0 then
  18. local reliabilityNumber = tonumber(ut.trim(sys.exec("uci -p /var/state get mwan3." .. interfaceName .. ".reliability")))
  19. if reliabilityNumber and reliabilityNumber > trackingNumber then
  20. errorFound = 1
  21. errorReliabilityList = errorReliabilityList .. interfaceName .. " "
  22. end
  23. end
  24. -- check if any interfaces are not properly configured in /etc/config/network or have no default route in main routing table
  25. if ut.trim(sys.exec("uci -p /var/state get network." .. interfaceName)) == "interface" then
  26. local interfaceDevice = ut.trim(sys.exec("uci -p /var/state get network." .. interfaceName .. ".ifname"))
  27. if interfaceDevice == "uci: Entry not found" or interfaceDevice == "" then
  28. errorFound = 1
  29. errorNetConfigList = errorNetConfigList .. interfaceName .. " "
  30. errorRouteList = errorRouteList .. interfaceName .. " "
  31. else
  32. local routeCheck = ut.trim(sys.exec("route -n | awk '{if ($8 == \"" .. interfaceDevice .. "\" && $1 == \"0.0.0.0\" && $3 == \"0.0.0.0\") print $1}'"))
  33. if routeCheck == "" then
  34. errorFound = 1
  35. errorRouteList = errorRouteList .. interfaceName .. " "
  36. end
  37. end
  38. else
  39. errorFound = 1
  40. errorNetConfigList = errorNetConfigList .. interfaceName .. " "
  41. errorRouteList = errorRouteList .. interfaceName .. " "
  42. end
  43. end
  44. )
  45. -- check if any interfaces have duplicate metrics
  46. local metricDuplicateNumbers = sys.exec("echo '" .. metricList .. "' | awk '{print $2}' | uniq -d")
  47. if metricDuplicateNumbers ~= "" then
  48. errorFound = 1
  49. local metricDuplicates = ""
  50. for line in metricDuplicateNumbers:gmatch("[^\r\n]+") do
  51. metricDuplicates = sys.exec("echo '" .. metricList .. "' | grep '" .. line .. "' | awk '{print $1}'")
  52. errorDuplicateMetricList = errorDuplicateMetricList .. metricDuplicates
  53. end
  54. errorDuplicateMetricList = sys.exec("echo '" .. errorDuplicateMetricList .. "' | tr '\n' ' '")
  55. end
  56. end
  57. function interfaceWarnings() -- display status and warning messages at the top of the page
  58. local warnings = ""
  59. if interfaceNumber <= 250 then
  60. warnings = "<strong>" .. translatef("There are currently %d of 250 supported interfaces configured", interfaceNumber) .. "</strong>"
  61. else
  62. warnings = "<font color=\"ff0000\"><strong>" .. translatef("WARNING: %d interfaces are configured exceeding the maximum of 250!", interfaceNumber) .. "</strong></font>"
  63. end
  64. if errorReliabilityList ~= " " then
  65. warnings = warnings .. "<br /><br /><font color=\"ff0000\"><strong>" .. translate("WARNING: Some interfaces have a higher reliability requirement than there are tracking IP addresses!") .. "</strong></font>"
  66. end
  67. if errorRouteList ~= " " then
  68. warnings = warnings .. "<br /><br /><font color=\"ff0000\"><strong>" .. translate("WARNING: Some interfaces have no default route in the main routing table!") .. "</strong></font>"
  69. end
  70. if errorNetConfigList ~= " " then
  71. warnings = warnings .. "<br /><br /><font color=\"ff0000\"><strong>" .. translate("WARNING: Some interfaces are configured incorrectly or not at all in /etc/config/network!") .. "</strong></font>"
  72. end
  73. if errorNoMetricList ~= " " then
  74. warnings = warnings .. "<br /><br /><font color=\"ff0000\"><strong>" .. translate("WARNING: Some interfaces have no metric configured in /etc/config/network!") .. "</strong></font>"
  75. end
  76. if errorDuplicateMetricList ~= " " then
  77. warnings = warnings .. "<br /><br /><font color=\"ff0000\"><strong>" .. translate("WARNING: Some interfaces have duplicate metrics configured in /etc/config/network!") .. "</strong></font>"
  78. end
  79. return warnings
  80. end
  81. -- ------ interface configuration ------ --
  82. dsp = require "luci.dispatcher"
  83. sys = require "luci.sys"
  84. ut = require "luci.util"
  85. interfaceNumber = 0
  86. metricList = ""
  87. errorFound = 0
  88. errorDuplicateMetricList = " "
  89. errorNetConfigList = " "
  90. errorNoMetricList = " "
  91. errorReliabilityList = " "
  92. errorRouteList = " "
  93. interfaceCheck()
  94. m5 = Map("mwan3", translate("MWAN Interface Configuration"),
  95. interfaceWarnings())
  96. m5:append(Template("mwan/config_css"))
  97. mwan_interface = m5:section(TypedSection, "interface", translate("Interfaces"),
  98. translate("MWAN supports up to 250 physical and/or logical interfaces<br />" ..
  99. "MWAN requires that all interfaces have a unique metric configured in /etc/config/network<br />" ..
  100. "Names must match the interface name found in /etc/config/network (see advanced tab)<br />" ..
  101. "Names may contain characters A-Z, a-z, 0-9, _ and no spaces<br />" ..
  102. "Interfaces may not share the same name as configured members, policies or rules"))
  103. mwan_interface.addremove = true
  104. mwan_interface.dynamic = false
  105. mwan_interface.sectionhead = translate("Interface")
  106. mwan_interface.sortable = false
  107. mwan_interface.template = "cbi/tblsection"
  108. mwan_interface.extedit = dsp.build_url("admin", "network", "mwan", "configuration", "interface", "%s")
  109. function mwan_interface.create(self, section)
  110. TypedSection.create(self, section)
  111. m5.uci:save("mwan3")
  112. luci.http.redirect(dsp.build_url("admin", "network", "mwan", "configuration", "interface", section))
  113. end
  114. enabled = mwan_interface:option(DummyValue, "enabled", translate("Enabled"))
  115. enabled.rawhtml = true
  116. function enabled.cfgvalue(self, s)
  117. if self.map:get(s, "enabled") == "1" then
  118. return "Yes"
  119. else
  120. return "No"
  121. end
  122. end
  123. track_ip = mwan_interface:option(DummyValue, "track_ip", translate("Tracking IP"))
  124. track_ip.rawhtml = true
  125. function track_ip.cfgvalue(self, s)
  126. tracked = self.map:get(s, "track_ip")
  127. if tracked then
  128. local ipList = ""
  129. for k,v in pairs(tracked) do
  130. ipList = ipList .. v .. "<br />"
  131. end
  132. return ipList
  133. else
  134. return "&#8212;"
  135. end
  136. end
  137. track_method = mwan_interface:option(DummyValue, "track_method", translate("Tracking method"))
  138. track_method.rawhtml = true
  139. function track_method.cfgvalue(self, s)
  140. if tracked then
  141. return self.map:get(s, "track_method") or "&#8212;"
  142. else
  143. return "&#8212;"
  144. end
  145. end
  146. reliability = mwan_interface:option(DummyValue, "reliability", translate("Tracking reliability"))
  147. reliability.rawhtml = true
  148. function reliability.cfgvalue(self, s)
  149. if tracked then
  150. return self.map:get(s, "reliability") or "&#8212;"
  151. else
  152. return "&#8212;"
  153. end
  154. end
  155. count = mwan_interface:option(DummyValue, "count", translate("Ping count"))
  156. count.rawhtml = true
  157. function count.cfgvalue(self, s)
  158. if tracked then
  159. return self.map:get(s, "count") or "&#8212;"
  160. else
  161. return "&#8212;"
  162. end
  163. end
  164. timeout = mwan_interface:option(DummyValue, "timeout", translate("Ping timeout"))
  165. timeout.rawhtml = true
  166. function timeout.cfgvalue(self, s)
  167. if tracked then
  168. local timeoutValue = self.map:get(s, "timeout")
  169. if timeoutValue then
  170. return timeoutValue .. "s"
  171. else
  172. return "&#8212;"
  173. end
  174. else
  175. return "&#8212;"
  176. end
  177. end
  178. interval = mwan_interface:option(DummyValue, "interval", translate("Ping interval"))
  179. interval.rawhtml = true
  180. function interval.cfgvalue(self, s)
  181. if tracked then
  182. local intervalValue = self.map:get(s, "interval")
  183. if intervalValue then
  184. return intervalValue .. "s"
  185. else
  186. return "&#8212;"
  187. end
  188. else
  189. return "&#8212;"
  190. end
  191. end
  192. down = mwan_interface:option(DummyValue, "down", translate("Interface down"))
  193. down.rawhtml = true
  194. function down.cfgvalue(self, s)
  195. if tracked then
  196. return self.map:get(s, "down") or "&#8212;"
  197. else
  198. return "&#8212;"
  199. end
  200. end
  201. up = mwan_interface:option(DummyValue, "up", translate("Interface up"))
  202. up.rawhtml = true
  203. function up.cfgvalue(self, s)
  204. if tracked then
  205. return self.map:get(s, "up") or "&#8212;"
  206. else
  207. return "&#8212;"
  208. end
  209. end
  210. metric = mwan_interface:option(DummyValue, "metric", translate("Metric"))
  211. metric.rawhtml = true
  212. function metric.cfgvalue(self, s)
  213. local metricValue = sys.exec("uci -p /var/state get network." .. s .. ".metric")
  214. if metricValue ~= "" then
  215. return metricValue
  216. else
  217. return "&#8212;"
  218. end
  219. end
  220. errors = mwan_interface:option(DummyValue, "errors", translate("Errors"))
  221. errors.rawhtml = true
  222. function errors.cfgvalue(self, s)
  223. if errorFound == 1 then
  224. local mouseOver, lineBreak = "", ""
  225. if string.find(errorReliabilityList, " " .. s .. " ") then
  226. mouseOver = "Higher reliability requirement than there are tracking IP addresses"
  227. lineBreak = "&#10;&#10;"
  228. end
  229. if string.find(errorRouteList, " " .. s .. " ") then
  230. mouseOver = mouseOver .. lineBreak .. "No default route in the main routing table"
  231. lineBreak = "&#10;&#10;"
  232. end
  233. if string.find(errorNetConfigList, " " .. s .. " ") then
  234. mouseOver = mouseOver .. lineBreak .. "Configured incorrectly or not at all in /etc/config/network"
  235. lineBreak = "&#10;&#10;"
  236. end
  237. if string.find(errorNoMetricList, " " .. s .. " ") then
  238. mouseOver = mouseOver .. lineBreak .. "No metric configured in /etc/config/network"
  239. lineBreak = "&#10;&#10;"
  240. end
  241. if string.find(errorDuplicateMetricList, " " .. s .. " ") then
  242. mouseOver = mouseOver .. lineBreak .. "Duplicate metric configured in /etc/config/network"
  243. end
  244. if mouseOver == "" then
  245. return ""
  246. else
  247. return "<span title=\"" .. mouseOver .. "\"><img src=\"/luci-static/resources/cbi/reset.gif\" alt=\"error\"></img></span>"
  248. end
  249. else
  250. return ""
  251. end
  252. end
  253. return m5