You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

266 lines
9.5 KiB

  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>There are currently " .. interfaceNumber .. " of 250 supported interfaces configured</strong>"
  61. else
  62. warnings = "<font color=\"ff0000\"><strong>WARNING: " .. interfaceNumber .. " interfaces are configured exceeding the maximum of 250!</strong></font>"
  63. end
  64. if errorReliabilityList ~= " " then
  65. warnings = warnings .. "<br /><br /><font color=\"ff0000\"><strong>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>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>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>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>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. translate(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 = "Interface"
  106. mwan_interface.sortable = true
  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. reliability = mwan_interface:option(DummyValue, "reliability", translate("Tracking reliability"))
  138. reliability.rawhtml = true
  139. function reliability.cfgvalue(self, s)
  140. if tracked then
  141. return self.map:get(s, "reliability") or "&#8212;"
  142. else
  143. return "&#8212;"
  144. end
  145. end
  146. count = mwan_interface:option(DummyValue, "count", translate("Ping count"))
  147. count.rawhtml = true
  148. function count.cfgvalue(self, s)
  149. if tracked then
  150. return self.map:get(s, "count") or "&#8212;"
  151. else
  152. return "&#8212;"
  153. end
  154. end
  155. timeout = mwan_interface:option(DummyValue, "timeout", translate("Ping timeout"))
  156. timeout.rawhtml = true
  157. function timeout.cfgvalue(self, s)
  158. if tracked then
  159. local timeoutValue = self.map:get(s, "timeout")
  160. if timeoutValue then
  161. return timeoutValue .. "s"
  162. else
  163. return "&#8212;"
  164. end
  165. else
  166. return "&#8212;"
  167. end
  168. end
  169. interval = mwan_interface:option(DummyValue, "interval", translate("Ping interval"))
  170. interval.rawhtml = true
  171. function interval.cfgvalue(self, s)
  172. if tracked then
  173. local intervalValue = self.map:get(s, "interval")
  174. if intervalValue then
  175. return intervalValue .. "s"
  176. else
  177. return "&#8212;"
  178. end
  179. else
  180. return "&#8212;"
  181. end
  182. end
  183. down = mwan_interface:option(DummyValue, "down", translate("Interface down"))
  184. down.rawhtml = true
  185. function down.cfgvalue(self, s)
  186. if tracked then
  187. return self.map:get(s, "down") or "&#8212;"
  188. else
  189. return "&#8212;"
  190. end
  191. end
  192. up = mwan_interface:option(DummyValue, "up", translate("Interface up"))
  193. up.rawhtml = true
  194. function up.cfgvalue(self, s)
  195. if tracked then
  196. return self.map:get(s, "up") or "&#8212;"
  197. else
  198. return "&#8212;"
  199. end
  200. end
  201. metric = mwan_interface:option(DummyValue, "metric", translate("Metric"))
  202. metric.rawhtml = true
  203. function metric.cfgvalue(self, s)
  204. local metricValue = sys.exec("uci -p /var/state get network." .. s .. ".metric")
  205. if metricValue ~= "" then
  206. return metricValue
  207. else
  208. return "&#8212;"
  209. end
  210. end
  211. errors = mwan_interface:option(DummyValue, "errors", translate("Errors"))
  212. errors.rawhtml = true
  213. function errors.cfgvalue(self, s)
  214. if errorFound == 1 then
  215. local mouseOver, lineBreak = "", ""
  216. if string.find(errorReliabilityList, " " .. s .. " ") then
  217. mouseOver = "Higher reliability requirement than there are tracking IP addresses"
  218. lineBreak = "&#10;&#10;"
  219. end
  220. if string.find(errorRouteList, " " .. s .. " ") then
  221. mouseOver = mouseOver .. lineBreak .. "No default route in the main routing table"
  222. lineBreak = "&#10;&#10;"
  223. end
  224. if string.find(errorNetConfigList, " " .. s .. " ") then
  225. mouseOver = mouseOver .. lineBreak .. "Configured incorrectly or not at all in /etc/config/network"
  226. lineBreak = "&#10;&#10;"
  227. end
  228. if string.find(errorNoMetricList, " " .. s .. " ") then
  229. mouseOver = mouseOver .. lineBreak .. "No metric configured in /etc/config/network"
  230. lineBreak = "&#10;&#10;"
  231. end
  232. if string.find(errorDuplicateMetricList, " " .. s .. " ") then
  233. mouseOver = mouseOver .. lineBreak .. "Duplicate metric configured in /etc/config/network"
  234. end
  235. if mouseOver == "" then
  236. return ""
  237. else
  238. return "<span title=\"" .. mouseOver .. "\"><img src=\"/luci-static/resources/cbi/reset.gif\" alt=\"error\"></img></span>"
  239. end
  240. else
  241. return ""
  242. end
  243. end
  244. return m5