Merge pull request #17303 from CarlosDerSeher/feature_bt_agent
[feed/packages.git] / net / mwan3 / files / usr / libexec / rpcd / mwan3
1 #!/bin/sh
2
3 . /lib/functions.sh
4 . /lib/functions/network.sh
5 . /usr/share/libubox/jshn.sh
6 . /lib/mwan3/common.sh
7
8 report_connected_v4() {
9 local address
10
11 if [ -n "$($IPT4 -S mwan3_connected 2> /dev/null)" ]; then
12 for address in $($IPS -o save list mwan3_connected_v4 | grep add | cut -d " " -f 3); do
13 json_add_string "" "${address}"
14 done
15 fi
16 }
17
18 report_connected_v6() {
19 [ $NO_IPV6 -ne 0 ] && return
20 local address
21
22 if [ -n "$($IPT6 -S mwan3_connected 2> /dev/null)" ]; then
23 for address in $($IPS -o save list mwan3_connected_v6 | grep add | cut -d " " -f 3); do
24 json_add_string "" "${address}"
25 done
26 fi
27 }
28
29 report_policies() {
30 local ipt="$1"
31 local policy="$2"
32
33 local percent total_weight weight iface
34
35 total_weight=$($ipt -S $policy | grep -v '.*--comment "out .*" .*$' | cut -s -d'"' -f2 | head -1 | awk '{print $3}')
36
37 for iface in $($ipt -S $policy | grep -v '.*--comment "out .*" .*$' | cut -s -d'"' -f2 | awk '{print $1}'); do
38 weight=$($ipt -S $policy | grep -v '.*--comment "out .*" .*$' | cut -s -d'"' -f2 | awk '$1 == "'$iface'"' | awk '{print $2}')
39 percent=$(($weight*100/$total_weight))
40 json_add_object
41 json_add_string interface "$iface"
42 json_add_int percent "$percent"
43 json_close_object
44 done
45 }
46
47 report_policies_v4() {
48 local policy
49
50 for policy in $($IPT4 -S | awk '{print $2}' | grep mwan3_policy_ | sort -u); do
51 json_add_array "${policy##*mwan3_policy_}"
52 report_policies "$IPT4" "$policy"
53 json_close_array
54 done
55 }
56
57 report_policies_v6() {
58 [ $NO_IPV6 -ne 0 ] && return
59 local policy
60
61 for policy in $($IPT6 -S | awk '{print $2}' | grep mwan3_policy_ | sort -u); do
62 json_add_array "${policy##*mwan3_policy_}"
63 report_policies "$IPT6" "$policy"
64 json_close_array
65 done
66 }
67
68 get_age() {
69 local time_p time_u
70 iface="$1"
71 time_p="$(cat "$MWAN3TRACK_STATUS_DIR/${iface}/TIME")"
72 [ -z "${time_p}" ] || {
73 time_n="$(get_uptime)"
74 echo $((time_n-time_p))
75 }
76 }
77
78 get_offline_time() {
79 local time_n time_d iface
80 iface="$1"
81 time_d="$(cat "$MWAN3TRACK_STATUS_DIR/${iface}/OFFLINE")"
82 [ -z "${time_d}" ] || [ "${time_d}" = "0" ] || {
83 time_n="$(get_uptime)"
84 echo $((time_n-time_d))
85 }
86 }
87
88 get_mwan3_status() {
89 local iface="${1}"
90 local iface_select="${2}"
91 local running="0"
92 local age=0
93 local online=0
94 local offline=0
95 local enabled time_p time_n time_u time_d status track_status up uptime
96
97 if [ "${iface}" != "${iface_select}" ] && [ "${iface_select}" != "" ]; then
98 return
99 fi
100
101 track_status="$(mwan3_get_mwan3track_status "$1")"
102 [ "$track_status" = "active" ] && running="1"
103 age=$(get_age "$iface")
104 online=$(get_online_time "$iface")
105 offline=$(get_offline_time "$iface")
106
107 config_get enabled "$iface" enabled 0
108
109 if [ -d "${MWAN3_STATUS_DIR}" ]; then
110 network_get_uptime uptime "$iface"
111 network_is_up "$iface" && up="1"
112 if [ -f "$MWAN3TRACK_STATUS_DIR/${iface}/STATUS" ]; then
113 status="$(cat "$MWAN3TRACK_STATUS_DIR/${iface}/STATUS")"
114 else
115 status="notracking"
116 fi
117 else
118 uptime=0
119 up=0
120 status="unknown"
121 fi
122
123 json_add_object "${iface}"
124 json_add_int age "$age"
125 json_add_int online "${online}"
126 json_add_int offline "${offline}"
127 json_add_int uptime "${uptime}"
128 json_add_int "score" "$(cat "$MWAN3TRACK_STATUS_DIR/${iface}/SCORE")"
129 json_add_int "lost" "$(cat "$MWAN3TRACK_STATUS_DIR/${iface}/LOST")"
130 json_add_int "turn" "$(cat "$MWAN3TRACK_STATUS_DIR/${iface}/TURN")"
131 json_add_string "status" "${status}"
132 json_add_boolean "enabled" "${enabled}"
133 json_add_boolean "running" "${running}"
134 json_add_boolean "up" "${up}"
135 json_add_array "track_ip"
136 for file in $MWAN3TRACK_STATUS_DIR/${iface}/TRACK_*; do
137 [ -z "${file#*/TRACK_OUTPUT}" ] && continue
138 [ -z "${file#*/TRACK_\*}" ] && continue
139 track="${file#*/TRACK_}"
140 json_add_object
141 json_add_string ip "${track}"
142 json_add_string status "$(cat "${file}")"
143 json_add_int latency "$(cat "$MWAN3TRACK_STATUS_DIR/${iface}/LATENCY_${track}")"
144 json_add_int packetloss "$(cat "$MWAN3TRACK_STATUS_DIR/${iface}/LOSS_${track}")"
145 json_close_object
146 done
147 json_close_array
148 json_close_object
149 }
150
151 main () {
152
153 case "$1" in
154 list)
155 json_init
156 json_add_object "status"
157 json_add_string "section" "x"
158 json_add_string "interface" "x"
159 json_add_string "policies" "x"
160 json_close_object
161 json_dump
162 ;;
163 call)
164 case "$2" in
165 status)
166 local section iface
167 read input;
168 json_load "$input"
169 json_get_var section section
170 json_get_var iface interface
171
172 config_load mwan3
173 json_init
174 case "$section" in
175 interfaces)
176 json_add_object interfaces
177 config_foreach get_mwan3_status interface "${iface}"
178 json_close_object
179 ;;
180 connected)
181 json_add_object connected
182 json_add_array ipv4
183 report_connected_v4
184 json_close_array
185 json_add_array ipv6
186 report_connected_v6
187 json_close_array
188 json_close_object
189 ;;
190 policies)
191 json_add_object policies
192 json_add_object ipv4
193 report_policies_v4
194 json_close_object
195 json_add_object ipv6
196 report_policies_v6
197 json_close_object
198 json_close_object
199 ;;
200 *)
201 # interfaces
202 json_add_object interfaces
203 config_foreach get_mwan3_status interface
204 json_close_object
205 # connected
206 json_add_object connected
207 json_add_array ipv4
208 report_connected_v4
209 json_close_array
210 json_add_array ipv6
211 report_connected_v6
212 json_close_array
213 json_close_object
214 # policies
215 json_add_object policies
216 json_add_object ipv4
217 report_policies_v4
218 json_close_object
219 json_add_object ipv6
220 report_policies_v6
221 json_close_object
222 json_close_object
223 ;;
224 esac
225 json_dump
226 ;;
227 esac
228 ;;
229 esac
230 }
231
232 main "$@"