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