From ce123d3e83ed1861fb7837e8aa3ab2cc28efd38a Mon Sep 17 00:00:00 2001
From: Florian Eckert <fe@dev.tdt.de>
Date: Fri, 9 Nov 2018 07:53:33 +0100
Subject: [PATCH] mwan3: add policies view to ubus interface

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 | 58 ++++++++++++++++++++++++++
 1 file changed, 58 insertions(+)

diff --git a/net/mwan3/files/usr/libexec/rpcd/mwan3 b/net/mwan3/files/usr/libexec/rpcd/mwan3
index dc4eeb6df..d31ee63bc 100755
--- a/net/mwan3/files/usr/libexec/rpcd/mwan3
+++ b/net/mwan3/files/usr/libexec/rpcd/mwan3
@@ -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