luci-app-mwan3: remove some config parameter from rule overview table
[project/luci.git] / applications / luci-app-mwan3 / luasrc / model / cbi / mwan / interface.lua
1 dsp = require "luci.dispatcher"
2 sys = require "luci.sys"
3 ut = require "luci.util"
4
5 function interfaceWarnings(overview, count)
6         local warnings = ""
7         if count <= 250 then
8                 warnings = string.format("<strong>%s</strong></br>",
9                         translatef("There are currently %d of 250 supported interfaces configured", count)
10                         )
11         else
12                 warnings = string.format("<strong>%s</strong></br>",
13                         translatef("WARNING: %d interfaces are configured exceeding the maximum of 250!", count)
14                         )
15         end
16
17         for i, k in pairs(overview) do
18                 if overview[i]["network"] == false then
19                         warnings = warnings .. string.format("<strong>%s</strong></br>",
20                                         translatef("WARNING: Interface %s are not found in /etc/config/network", i)
21                                         )
22                 end
23
24                 if overview[i]["default_route"] == false then
25                         warnings = warnings .. string.format("<strong>%s</strong></br>",
26                                 translatef("WARNING: Interface %s has no default route in the main routing table", i)
27                                 )
28                 end
29
30                 if overview[i]["reliability"] == false then
31                         warnings = warnings .. string.format("<strong>%s</strong></br>",
32                                 translatef("WARNING: Interface %s has a higher reliability " ..
33                                 "requirement than tracking hosts (%d)", i, overview[i]["tracking"])
34                                 )
35                 end
36
37                 if overview[i]["duplicate_metric"] == true then
38                         warnings = warnings .. string.format("<strong>%s</strong></br>",
39                                 translatef("WARNING: Interface %s has a duplicate metric %s configured", i, overview[i]["metric"])
40                                 )
41                 end
42         end
43
44         return warnings
45 end
46
47 function configCheck()
48         local overview = {}
49         local count = 0
50         local duplicate_metric = {}
51         uci.cursor():foreach("mwan3", "interface",
52                 function (section)
53                         local uci = uci.cursor(nil, "/var/state")
54                         local iface = section[".name"]
55                         overview[iface] = {}
56                         count = count + 1
57                         local network = uci:get("network", iface)
58                         overview[iface]["network"] = false
59                         if network ~= nil then
60                                 overview[iface]["network"] = true
61
62                                 local device = uci:get("network", iface, "ifname")
63                                 if device ~= nil then
64                                         overview[iface]["device"] = device
65                                 end
66
67                                 local metric = uci:get("network", iface, "metric")
68                                 if metric ~= nil then
69                                         overview[iface]["metric"] = metric
70                                         overview[iface]["duplicate_metric"] = false
71                                         for _, m in ipairs(duplicate_metric) do
72                                                 if m == metric then
73                                                         overview[iface]["duplicate_metric"] = true
74                                                 end
75                                         end
76                                         table.insert(duplicate_metric, metric)
77                                 end
78
79                                 local dump = require("luci.util").ubus("network.interface.%s" % iface, "status", {})
80                                 overview[iface]["default_route"] = false
81                                 if dump then
82                                         local _, route
83                                         for _, route in ipairs(dump.route) do
84                                                 if dump.route[_].target == "0.0.0.0" then
85                                                         overview[iface]["default_route"] = true
86                                                 end
87                                         end
88                                 end
89                         end
90
91                         local trackingNumber = uci:get("mwan3", iface, "track_ip")
92                         overview[iface]["tracking"] = 0
93                         if #trackingNumber > 0 then
94                                 overview[iface]["tracking"] = #trackingNumber
95                                 overview[iface]["reliability"] = false
96                                 local reliabilityNumber = tonumber(uci:get("mwan3", iface, "reliability"))
97                                 if reliabilityNumber and reliabilityNumber <= #trackingNumber then
98                                         overview[iface]["reliability"] = true
99                                 end
100                         end
101                 end
102         )
103         return overview, count
104 end
105
106 m5 = Map("mwan3", translate("MWAN - Interfaces"),
107         interfaceWarnings(configCheck()))
108
109
110 mwan_interface = m5:section(TypedSection, "interface", nil,
111         translate("MWAN supports up to 250 physical and/or logical interfaces<br />" ..
112         "MWAN requires that all interfaces have a unique metric configured in /etc/config/network<br />" ..
113         "Names must match the interface name found in /etc/config/network (see advanced tab)<br />" ..
114         "Names may contain characters A-Z, a-z, 0-9, _ and no spaces<br />" ..
115         "Interfaces may not share the same name as configured members, policies or rules"))
116         mwan_interface.addremove = true
117         mwan_interface.dynamic = false
118         mwan_interface.sectionhead = translate("Interface")
119         mwan_interface.sortable = false
120         mwan_interface.template = "cbi/tblsection"
121         mwan_interface.extedit = dsp.build_url("admin", "network", "mwan", "interface", "%s")
122         function mwan_interface.create(self, section)
123                 TypedSection.create(self, section)
124                 m5.uci:save("mwan3")
125                 luci.http.redirect(dsp.build_url("admin", "network", "mwan", "interface", section))
126         end
127
128
129 enabled = mwan_interface:option(DummyValue, "enabled", translate("Enabled"))
130         enabled.rawhtml = true
131         function enabled.cfgvalue(self, s)
132                 if self.map:get(s, "enabled") == "1" then
133                         return translate("Yes")
134                 else
135                         return translate("No")
136                 end
137         end
138
139 track_method = mwan_interface:option(DummyValue, "track_method", translate("Tracking method"))
140         track_method.rawhtml = true
141         function track_method.cfgvalue(self, s)
142                 local tracked = self.map:get(s, "track_ip")
143                 if tracked then
144                         return self.map:get(s, "track_method") or "&#8212;"
145                 else
146                         return "&#8212;"
147                 end
148         end
149
150 reliability = mwan_interface:option(DummyValue, "reliability", translate("Tracking reliability"))
151         reliability.rawhtml = true
152         function reliability.cfgvalue(self, s)
153                 local tracked = self.map:get(s, "track_ip")
154                 if tracked then
155                         return self.map:get(s, "reliability") or "&#8212;"
156                 else
157                         return "&#8212;"
158                 end
159         end
160
161 interval = mwan_interface:option(DummyValue, "interval", translate("Ping interval"))
162         interval.rawhtml = true
163         function interval.cfgvalue(self, s)
164                 local tracked = self.map:get(s, "track_ip")
165                 if tracked then
166                         local intervalValue = self.map:get(s, "interval")
167                         if intervalValue then
168                                 return intervalValue .. "s"
169                         else
170                                 return "&#8212;"
171                         end
172                 else
173                         return "&#8212;"
174                 end
175         end
176
177 down = mwan_interface:option(DummyValue, "down", translate("Interface down"))
178         down.rawhtml = true
179         function down.cfgvalue(self, s)
180                 local tracked = self.map:get(s, "track_ip")
181                 if tracked then
182                         return self.map:get(s, "down") or "&#8212;"
183                 else
184                         return "&#8212;"
185                 end
186         end
187
188 up = mwan_interface:option(DummyValue, "up", translate("Interface up"))
189         up.rawhtml = true
190         function up.cfgvalue(self, s)
191                 local tracked = self.map:get(s, "track_ip")
192                 if tracked then
193                         return self.map:get(s, "up") or "&#8212;"
194                 else
195                         return "&#8212;"
196                 end
197         end
198
199 metric = mwan_interface:option(DummyValue, "metric", translate("Metric"))
200         metric.rawhtml = true
201         function metric.cfgvalue(self, s)
202                 local uci = uci.cursor(nil, "/var/state")
203                 local metric = uci:get("network", s, "metric")
204                 if metric then
205                         return metric
206                 else
207                         return "&#8212;"
208                 end
209         end
210
211 return m5