luci-app-mwan3: refactoring warning compilation on the interface config pages
authorFlorian Eckert <fe@dev.tdt.de>
Wed, 17 Jan 2018 13:37:14 +0000 (14:37 +0100)
committerFlorian Eckert <fe@dev.tdt.de>
Thu, 18 Jan 2018 14:09:20 +0000 (15:09 +0100)
Signed-off-by: Florian Eckert <fe@dev.tdt.de>
applications/luci-app-mwan3/luasrc/model/cbi/mwan/interface.lua
applications/luci-app-mwan3/luasrc/model/cbi/mwan/interfaceconfig.lua

index 4f5dc75360dc981bdc0d38db5fea6e9f88cec627..95af9bbbfd09d424e249b9c9f6b07e2a0ace69c6 100644 (file)
--- ------ extra functions ------ --
-
-function interfaceCheck() -- find issues with too many interfaces, reliability and metric
-       uci.cursor():foreach("mwan3", "interface",
-               function (section)
-                       local interfaceName = section[".name"]
-                       interfaceNumber = interfaceNumber+1 -- count number of mwan interfaces configured
-                       -- create list of metrics for none and duplicate checking
-                       local metricValue = ut.trim(sys.exec("uci -p /var/state get network." .. interfaceName .. ".metric"))
-                       if metricValue == "" then
-                               errorFound = 1
-                               errorNoMetricList = errorNoMetricList .. interfaceName .. " "
-                       else
-                               metricList = metricList .. interfaceName .. " " .. metricValue .. "\n"
-                       end
-                       -- check if any interfaces have a higher reliability requirement than tracking IPs configured
-                       local trackingNumber = tonumber(ut.trim(sys.exec("echo $(uci -p /var/state get mwan3." .. interfaceName .. ".track_ip) | wc -w")))
-                       if trackingNumber > 0 then
-                               local reliabilityNumber = tonumber(ut.trim(sys.exec("uci -p /var/state get mwan3." .. interfaceName .. ".reliability")))
-                               if reliabilityNumber and reliabilityNumber > trackingNumber then
-                                       errorFound = 1
-                                       errorReliabilityList = errorReliabilityList .. interfaceName .. " "
-                               end
-                       end
-                       -- check if any interfaces are not properly configured in /etc/config/network or have no default route in main routing table
-                       if ut.trim(sys.exec("uci -p /var/state get network." .. interfaceName)) == "interface" then
-                               local interfaceDevice = ut.trim(sys.exec("uci -p /var/state get network." .. interfaceName .. ".ifname"))
-                               if interfaceDevice == "uci: Entry not found" or interfaceDevice == "" then
-                                       errorFound = 1
-                                       errorNetConfigList = errorNetConfigList .. interfaceName .. " "
-                                       errorRouteList = errorRouteList .. interfaceName .. " "
-                               else
-                                       local routeCheck = ut.trim(sys.exec("route -n | awk '{if ($8 == \"" .. interfaceDevice .. "\" && $1 == \"0.0.0.0\" && $3 == \"0.0.0.0\") print $1}'"))
-                                       if routeCheck == "" then
-                                               errorFound = 1
-                                               errorRouteList = errorRouteList .. interfaceName .. " "
-                                       end
-                               end
-                       else
-                               errorFound = 1
-                               errorNetConfigList = errorNetConfigList .. interfaceName .. " "
-                               errorRouteList = errorRouteList .. interfaceName .. " "
-                       end
-               end
-       )
-       -- check if any interfaces have duplicate metrics
-       local metricDuplicateNumbers = sys.exec("echo '" .. metricList .. "' | awk '{print $2}' | uniq -d")
-       if metricDuplicateNumbers ~= "" then
-               errorFound = 1
-               local metricDuplicates = ""
-               for line in metricDuplicateNumbers:gmatch("[^\r\n]+") do
-                       metricDuplicates = sys.exec("echo '" .. metricList .. "' | grep '" .. line .. "' | awk '{print $1}'")
-                       errorDuplicateMetricList = errorDuplicateMetricList .. metricDuplicates
-               end
-               errorDuplicateMetricList = sys.exec("echo '" .. errorDuplicateMetricList .. "' | tr '\n' ' '")
-       end
-end
+dsp = require "luci.dispatcher"
+sys = require "luci.sys"
+ut = require "luci.util"
 
-function interfaceWarnings() -- display status and warning messages at the top of the page
+function interfaceWarnings(overview, count)
        local warnings = ""
-       if interfaceNumber <= 250 then
-               warnings = "<strong>" .. translatef("There are currently %d of 250 supported interfaces configured", interfaceNumber) .. "</strong>"
+       if count <= 250 then
+               warnings = string.format("<strong>%s</strong></br>",
+                       translatef("There are currently %d of 250 supported interfaces configured", count)
+                       )
        else
-               warnings = "<font color=\"ff0000\"><strong>" .. translatef("WARNING: %d interfaces are configured exceeding the maximum of 250!", interfaceNumber) .. "</strong></font>"
-       end
-       if errorReliabilityList ~= " " then
-               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>"
-       end
-       if errorRouteList ~= " " then
-               warnings = warnings .. "<br /><br /><font color=\"ff0000\"><strong>" .. translate("WARNING: Some interfaces have no default route in the main routing table!") .. "</strong></font>"
-       end
-       if errorNetConfigList ~= " " then
-               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>"
-       end
-       if errorNoMetricList ~= " " then
-               warnings = warnings .. "<br /><br /><font color=\"ff0000\"><strong>" .. translate("WARNING: Some interfaces have no metric configured in /etc/config/network!") .. "</strong></font>"
+               warnings = string.format("<strong>%s</strong></br>",
+                       translatef("WARNING: %d interfaces are configured exceeding the maximum of 250!", count)
+                       )
        end
-       if errorDuplicateMetricList ~= " " then
-               warnings = warnings .. "<br /><br /><font color=\"ff0000\"><strong>" .. translate("WARNING: Some interfaces have duplicate metrics configured in /etc/config/network!") .. "</strong></font>"
+
+       for i, k in pairs(overview) do
+               if overview[i]["network"] == false then
+                       warnings = warnings .. string.format("<strong>%s</strong></br>",
+                                       translatef("WARNING: Interface %s are not found in /etc/config/network", i)
+                                       )
+               end
+
+               if overview[i]["default_route"] == false then
+                       warnings = warnings .. string.format("<strong>%s</strong></br>",
+                               translatef("WARNING: Interface %s has no default route in the main routing table", i)
+                               )
+               end
+
+               if overview[i]["reliability"] == false then
+                       warnings = warnings .. string.format("<strong>%s</strong></br>",
+                               translatef("WARNING: Interface %s has a higher reliability " ..
+                               "requirement than tracking hosts (%d)", i, overview[i]["tracking"])
+                               )
+               end
+
+               if overview[i]["duplicate_metric"] == true then
+                       warnings = warnings .. string.format("<strong>%s</strong></br>",
+                               translatef("WARNING: Interface %s has a duplicate metric %s configured", i, overview[i]["metric"])
+                               )
+               end
        end
+
        return warnings
 end
 
--- ------ interface configuration ------ --
+function configCheck()
+       local overview = {}
+       local count = 0
+       local duplicate_metric = {}
+       uci.cursor():foreach("mwan3", "interface",
+               function (section)
+                       local uci = uci.cursor(nil, "/var/state")
+                       local iface = section[".name"]
+                       overview[iface] = {}
+                       count = count + 1
+                       local network = uci:get("network", iface)
+                       overview[iface]["network"] = false
+                       if network ~= nil then
+                               overview[iface]["network"] = true
+
+                               local device = uci:get("network", iface, "ifname")
+                               if device ~= nil then
+                                       overview[iface]["device"] = device
+                               end
 
-dsp = require "luci.dispatcher"
-sys = require "luci.sys"
-ut = require "luci.util"
+                               local metric = uci:get("network", iface, "metric")
+                               if metric ~= nil then
+                                       overview[iface]["metric"] = metric
+                                       overview[iface]["duplicate_metric"] = false
+                                       for _, m in ipairs(duplicate_metric) do
+                                               if m == metric then
+                                                       overview[iface]["duplicate_metric"] = true
+                                               end
+                                       end
+                                       table.insert(duplicate_metric, metric)
+                               end
 
-interfaceNumber = 0
-metricList = ""
-errorFound = 0
-errorDuplicateMetricList = " "
-errorNetConfigList = " "
-errorNoMetricList = " "
-errorReliabilityList = " "
-errorRouteList = " "
-interfaceCheck()
+                               local dump = require("luci.util").ubus("network.interface.%s" % iface, "status", {})
+                               overview[iface]["default_route"] = false
+                               if dump then
+                                       local _, route
+                                       for _, route in ipairs(dump.route) do
+                                               if dump.route[_].target == "0.0.0.0" then
+                                                       overview[iface]["default_route"] = true
+                                               end
+                                       end
+                               end
+                       end
 
+                       local trackingNumber = uci:get("mwan3", iface, "track_ip")
+                       overview[iface]["tracking"] = 0
+                       if #trackingNumber > 0 then
+                               overview[iface]["tracking"] = #trackingNumber
+                               overview[iface]["reliability"] = false
+                               local reliabilityNumber = tonumber(uci:get("mwan3", iface, "reliability"))
+                               if reliabilityNumber and reliabilityNumber <= #trackingNumber then
+                                       overview[iface]["reliability"] = true
+                               end
+                       end
+               end
+       )
+       return overview, count
+end
 
 m5 = Map("mwan3", translate("MWAN - Interfaces"),
-       interfaceWarnings())
+       interfaceWarnings(configCheck()))
        m5:append(Template("mwan/config_css"))
 
 
@@ -238,39 +243,4 @@ metric = mwan_interface:option(DummyValue, "metric", translate("Metric"))
                end
        end
 
-errors = mwan_interface:option(DummyValue, "errors", translate("Errors"))
-       errors.rawhtml = true
-       function errors.cfgvalue(self, s)
-               if errorFound == 1 then
-                       local mouseOver, lineBreak = "", ""
-                       if string.find(errorReliabilityList, " " .. s .. " ") then
-                               mouseOver = "Higher reliability requirement than there are tracking IP addresses"
-                               lineBreak = "&#10;&#10;"
-                       end
-                       if string.find(errorRouteList, " " .. s .. " ") then
-                               mouseOver = mouseOver .. lineBreak .. "No default route in the main routing table"
-                               lineBreak = "&#10;&#10;"
-                       end
-                       if string.find(errorNetConfigList, " " .. s .. " ") then
-                               mouseOver = mouseOver .. lineBreak .. "Configured incorrectly or not at all in /etc/config/network"
-                               lineBreak = "&#10;&#10;"
-                       end
-                       if string.find(errorNoMetricList, " " .. s .. " ") then
-                               mouseOver = mouseOver .. lineBreak .. "No metric configured in /etc/config/network"
-                               lineBreak = "&#10;&#10;"
-                       end
-                       if string.find(errorDuplicateMetricList, " " .. s .. " ") then
-                               mouseOver = mouseOver .. lineBreak .. "Duplicate metric configured in /etc/config/network"
-                       end
-                       if mouseOver == "" then
-                               return ""
-                       else
-                               return "<span title=\"" .. mouseOver .. "\"><img src=\"/luci-static/resources/cbi/reset.gif\" alt=\"error\"></img></span>"
-                       end
-               else
-                       return ""
-               end
-       end
-
-
 return m5
index 86e959771e9c695d653b15e1bde16745eae7d4ba..07ccd2d59f302467b010e1b4a30d1d4a61c44234 100644 (file)
@@ -1,94 +1,10 @@
--- ------ extra functions ------ --
-
-function interfaceCheck()
-       metricValue = ut.trim(sys.exec("uci -p /var/state get network." .. arg[1] .. ".metric"))
-       if metricValue == "" then -- no metric
-               errorNoMetric = 1
-       else -- if metric exists create list of interface metrics to compare against for duplicates
-               uci.cursor():foreach("mwan3", "interface",
-                       function (section)
-                               local metricValue = ut.trim(sys.exec("uci -p /var/state get network." .. section[".name"] .. ".metric"))
-                               metricList = metricList .. section[".name"] .. " " .. metricValue .. "\n"
-                       end
-               )
-               -- compare metric against list
-               local metricDuplicateNumbers, metricDuplicates = sys.exec("echo '" .. metricList .. "' | awk '{print $2}' | uniq -d"), ""
-               for line in metricDuplicateNumbers:gmatch("[^\r\n]+") do
-                       metricDuplicates = sys.exec("echo '" .. metricList .. "' | grep '" .. line .. "' | awk '{print $1}'")
-                       errorDuplicateMetricList = errorDuplicateMetricList .. metricDuplicates
-               end
-               if sys.exec("echo '" .. errorDuplicateMetricList .. "' | grep -w " .. arg[1]) ~= "" then
-                       errorDuplicateMetric = 1
-               end
-       end
-       -- check if this interface has a higher reliability requirement than track IPs configured
-       local trackingNumber = tonumber(ut.trim(sys.exec("echo $(uci -p /var/state get mwan3." .. arg[1] .. ".track_ip) | wc -w")))
-       if trackingNumber > 0 then
-               local reliabilityNumber = tonumber(ut.trim(sys.exec("uci -p /var/state get mwan3." .. arg[1] .. ".reliability")))
-               if reliabilityNumber and reliabilityNumber > trackingNumber then
-                       errorReliability = 1
-               end
-       end
-       -- check if any interfaces are not properly configured in /etc/config/network or have no default route in main routing table
-       if ut.trim(sys.exec("uci -p /var/state get network." .. arg[1])) == "interface" then
-               local interfaceDevice = ut.trim(sys.exec("uci -p /var/state get network." .. arg[1] .. ".ifname"))
-               if interfaceDevice == "uci: Entry not found" or interfaceDevice == "" then
-                       errorNetConfig = 1
-                       errorRoute = 1
-               else
-                       local routeCheck = ut.trim(sys.exec("route -n | awk '{if ($8 == \"" .. interfaceDevice .. "\" && $1 == \"0.0.0.0\" && $3 == \"0.0.0.0\") print $1}'"))
-                       if routeCheck == "" then
-                               errorRoute = 1
-                       end
-               end
-       else
-               errorNetConfig = 1
-               errorRoute = 1
-       end
-end
-
-function interfaceWarnings() -- display warning messages at the top of the page
-       local warns, lineBreak = "", ""
-       if errorReliability == 1 then
-               warns = "<font color=\"ff0000\"><strong>" .. translate("WARNING: This interface has a higher reliability requirement than there are tracking IP addresses!") .. "</strong></font>"
-               lineBreak = "<br /><br />"
-       end
-       if errorRoute == 1 then
-               warns = warns .. lineBreak .. "<font color=\"ff0000\"><strong>" .. translate("WARNING: This interface has no default route in the main routing table!") .. "</strong></font>"
-               lineBreak = "<br /><br />"
-       end
-       if errorNetConfig == 1 then
-               warns = warns .. lineBreak .. "<font color=\"ff0000\"><strong>" .. translate("WARNING: This interface is configured incorrectly or not at all in /etc/config/network!") .. "</strong></font>"
-               lineBreak = "<br /><br />"
-       end
-       if errorNoMetric == 1 then
-               warns = warns .. lineBreak .. "<font color=\"ff0000\"><strong>" .. translate("WARNING: This interface has no metric configured in /etc/config/network!") .. "</strong></font>"
-       elseif errorDuplicateMetric == 1 then
-               warns = warns .. lineBreak .. "<font color=\"ff0000\"><strong>" .. translate("WARNING: This and other interfaces have duplicate metrics configured in /etc/config/network!") .. "</strong></font>"
-       end
-       return warns
-end
-
--- ------ interface configuration ------ --
-
 dsp = require "luci.dispatcher"
 sys = require "luci.sys"
 ut = require "luci.util"
 arg[1] = arg[1] or ""
 
-metricValue = ""
-metricList = ""
-errorDuplicateMetricList = ""
-errorNoMetric = 0
-errorDuplicateMetric = 0
-errorRoute = 0
-errorNetConfig = 0
-errorReliability = 0
-interfaceCheck()
 
-
-m5 = Map("mwan3", translatef("MWAN Interface Configuration - %s", arg[1]),
-       interfaceWarnings())
+m5 = Map("mwan3", translatef("MWAN Interface Configuration - %s", arg[1]))
        m5.redirect = dsp.build_url("admin", "network", "mwan", "interface")
 
 
@@ -255,12 +171,13 @@ metric = mwan_interface:option(DummyValue, "metric", translate("Metric"),
        translate("This displays the metric assigned to this interface in /etc/config/network"))
        metric.rawhtml = true
        function metric.cfgvalue(self, s)
-               if errorNoMetric == 0 then
-                       return metricValue
+               local uci = uci.cursor(nil, "/var/state")
+               local metric = uci:get("network", arg[1], "metric")
+               if metric then
+                       return metric
                else
                        return "&#8212;"
                end
        end
 
-
 return m5