net/mwan3: reduce duplicate code
authorFlorian Eckert <redacted>
Mon, 15 Oct 2018 14:24:25 +0000 (16:24 +0200)
committerFlorian Eckert <redacted>
Tue, 23 Oct 2018 12:44:29 +0000 (14:44 +0200)
The generation for reporting the policies uses the same code add a
common function to reduce duplication.

Signed-off-by: Florian Eckert <redacted>
net/mwan3/files/lib/mwan3/mwan3.sh

index 7c4195394f73c19c02ae5032aaf48ebfbdef9a87..e7a865b43a343f525bea61e6d89dd4b9f8a26b05 100644 (file)
@@ -1047,53 +1047,43 @@ mwan3_report_iface_status()
        echo " interface $1 is $result and tracking is $tracking"
 }
 
-mwan3_report_policies_v4()
+mwan3_report_policies()
 {
-       local percent policy share total_weight weight iface
+       local ipt="$1"
+       local policy="$2"
 
-       for policy in $($IPT4 -S | awk '{print $2}' | grep mwan3_policy_ | sort -u); do
-               echo "$policy:" | sed 's/mwan3_policy_//'
+       local percent total_weight weight iface
 
-               [ -n "$total_weight" ] || total_weight=$($IPT4 -S $policy | grep -v '.*--comment "out .*" .*$' | cut -s -d'"' -f2 | head -1 | awk '{print $3}')
+       total_weight=$($ipt -S $policy | grep -v '.*--comment "out .*" .*$' | cut -s -d'"' -f2 | head -1 | awk '{print $3}')
 
-               if [ ! -z "${total_weight##*[!0-9]*}" ]; then
-                       for iface in $($IPT4 -S $policy | grep -v '.*--comment "out .*" .*$' | cut -s -d'"' -f2 | awk '{print $1}'); do
-                               weight=$($IPT4 -S $policy | grep -v '.*--comment "out .*" .*$' | cut -s -d'"' -f2 | awk '$1 == "'$iface'"' | awk '{print $2}')
-                               percent=$(($weight*100/$total_weight))
-                               echo " $iface ($percent%)"
-                       done
-               else
-                       echo " $($IPT4 -S $policy | grep -v '.*--comment "out .*" .*$' | sed '/.*--comment \([^ ]*\) .*$/!d;s//\1/;q')"
-               fi
+       if [ ! -z "${total_weight##*[!0-9]*}" ]; then
+               for iface in $($ipt -S $policy | grep -v '.*--comment "out .*" .*$' | cut -s -d'"' -f2 | awk '{print $1}'); do
+                       weight=$($ipt -S $policy | grep -v '.*--comment "out .*" .*$' | cut -s -d'"' -f2 | awk '$1 == "'$iface'"' | awk '{print $2}')
+                       percent=$(($weight*100/$total_weight))
+                       echo " $iface ($percent%)"
+               done
+       else
+               echo " $($ipt -S $policy | grep -v '.*--comment "out .*" .*$' | sed '/.*--comment \([^ ]*\) .*$/!d;s//\1/;q')"
+       fi
+}
 
-               unset total_weight
+mwan3_report_policies_v4()
+{
+       local policy
 
-               echo -e
+       for policy in $($IPT4 -S | awk '{print $2}' | grep mwan3_policy_ | sort -u); do
+               echo "$policy:" | sed 's/mwan3_policy_//'
+               mwan3_report_policies "$IPT4" "$policy"
        done
 }
 
 mwan3_report_policies_v6()
 {
-       local percent policy share total_weight weight iface
+       local policy
 
        for policy in $($IPT6 -S | awk '{print $2}' | grep mwan3_policy_ | sort -u); do
                echo "$policy:" | sed 's/mwan3_policy_//'
-
-               [ -n "$total_weight" ] || total_weight=$($IPT6 -S $policy | grep -v '.*--comment "out .*" .*$' | cut -s -d'"' -f2 | head -1 | awk '{print $3}')
-
-               if [ ! -z "${total_weight##*[!0-9]*}" ]; then
-                       for iface in $($IPT6 -S $policy | grep -v '.*--comment "out .*" .*$' | cut -s -d'"' -f2 | awk '{print $1}'); do
-                               weight=$($IPT6 -S $policy | grep -v '.*--comment "out .*" .*$' | cut -s -d'"' -f2 | awk '$1 == "'$iface'"' | awk '{print $2}')
-                               percent=$(($weight*100/$total_weight))
-                               echo " $iface ($percent%)"
-                       done
-               else
-                       echo " $($IPT6 -S $policy | grep -v '.*--comment "out .*" .*$' | sed '/.*--comment \([^ ]*\) .*$/!d;s//\1/;q')"
-               fi
-
-               unset total_weight
-
-               echo -e
+               mwan3_report_policies "$IPT6" "$policy"
        done
 }
 
git clone https://git.99rst.org/PROJECT