mwan3: add policies view to ubus interface 7418/head
authorFlorian Eckert <fe@dev.tdt.de>
Fri, 9 Nov 2018 06:53:33 +0000 (07:53 +0100)
committerFlorian Eckert <fe@dev.tdt.de>
Tue, 13 Nov 2018 15:04:26 +0000 (16:04 +0100)
Add to ubus the missing output information "policies" which could already
be observed with the command "mwan3 status".

Signed-off-by: Florian Eckert <fe@dev.tdt.de>
net/mwan3/files/usr/libexec/rpcd/mwan3

index dc4eeb6df6b2a51e1b27a93a68f69f2b1cf82825..d31ee63bcef0eea5755334de8ef1d81add908422 100755 (executable)
@@ -31,6 +31,44 @@ report_connected_v6() {
        fi
 }
 
+report_policies() {
+       local ipt="$1"
+       local policy="$2"
+
+       local percent total_weight weight iface
+
+       total_weight=$($ipt -S $policy | grep -v '.*--comment "out .*" .*$' | cut -s -d'"' -f2 | head -1 | awk '{print $3}')
+
+       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))
+               json_add_object
+               json_add_string interface "$iface"
+               json_add_int percent "$percent"
+               json_close_object
+       done
+}
+
+report_policies_v4() {
+       local policy
+
+       for policy in $($IPT4 -S | awk '{print $2}' | grep mwan3_policy_ | sort -u); do
+               json_add_array "${policy##*mwan3_policy_}"
+               report_policies "$IPT4" "$policy"
+               json_close_array
+       done
+}
+
+report_policies_v6() {
+       local policy
+
+       for policy in $($IPT6 -S | awk '{print $2}' | grep mwan3_policy_ | sort -u); do
+               json_add_array "${policy##*mwan3_policy_}"
+               report_policies "$IPT6" "$policy"
+               json_close_array
+       done
+}
+
 get_mwan3_status() {
        local iface="${1}"
        local iface_select="${2}"
@@ -100,6 +138,7 @@ main () {
                        json_add_object "status"
                        json_add_string "section" "x"
                        json_add_string "interface" "x"
+                       json_add_string "policies" "x"
                        json_close_object
                        json_dump
                        ;;
@@ -130,6 +169,16 @@ main () {
                                                json_close_array
                                                json_close_object
                                                ;;
+                                       policies)
+                                               json_add_object policies
+                                               json_add_object ipv4
+                                               report_policies_v4
+                                               json_close_object
+                                               json_add_object ipv6
+                                               report_policies_v6
+                                               json_close_object
+                                               json_close_object
+                                               ;;
                                        *)
                                                # interfaces
                                                json_add_object interfaces
@@ -144,6 +193,15 @@ main () {
                                                report_connected_v6
                                                json_close_array
                                                json_close_object
+                                               # policies
+                                               json_add_object policies
+                                               json_add_object ipv4
+                                               report_policies_v4
+                                               json_close_object
+                                               json_add_object ipv6
+                                               report_policies_v6
+                                               json_close_object
+                                               json_close_object
                                                ;;
                                esac
                                json_dump