Add mwan3 to v1.4 branch of package feed
[librecmc/package-feed.git] / net / mwan3 / files / usr / libexec / rpcd / mwan3
1 #!/bin/sh
2
3 . /lib/functions.sh
4 . /usr/share/libubox/jshn.sh
5
6 MWAN3_STATUS_DIR="/var/run/mwan3track"
7 MWAN3_PID_FILE="/var/run/mwan3track"
8
9 IPS="ipset"
10 IPT4="iptables -t mangle -w"
11 IPT6="ip6tables -t mangle -w"
12
13 report_connected_v4() {
14         local address
15
16         if [ -n "$($IPT4 -S mwan3_connected 2> /dev/null)" ]; then
17                 for address in $($IPS list mwan3_connected_v4 | tail -n +8); do
18                         json_add_string "" "${address}"
19                 done
20         fi
21 }
22
23 report_connected_v6() {
24         local address
25
26         if [ -n "$($IPT6 -S mwan3_connected 2> /dev/null)" ]; then
27                 for address in $($IPS list mwan3_connected_v6 | tail -n +8); do
28                         json_add_string "" "${address}"
29                 done
30         fi
31 }
32
33 get_mwan3_status() {
34         local iface="${1}"
35         local iface_select="${2}"
36         local running="0"
37         local pid
38
39         if [ "${iface}" = "${iface_select}" ] || [ "${iface_select}" = "" ]; then
40                 pid="$(pgrep -f "mwan3track $iface_selected")"
41                 if [ "${pid}" != "" ]; then
42                         running="1"
43                 fi
44
45                 json_add_object "${iface}"
46                 json_add_string "score" "$(cat "$MWAN3_STATUS_DIR/${iface}/SCORE")"
47                 json_add_string "lost" "$(cat "$MWAN3_STATUS_DIR/${iface}/LOST")"
48                 json_add_string "turn" "$(cat "$MWAN3_STATUS_DIR/${iface}/TURN")"
49                 json_add_string "status" "$(cat "$MWAN3_STATUS_DIR/${iface}/STATUS")"
50                 json_add_boolean "running" "${running}"
51                 json_add_array "track_ip"
52                 for file in $MWAN3_STATUS_DIR/${iface}/*; do
53                         track="${file#*/TRACK_}"
54                         if [ "${track}" != "${file}" ]; then
55                                 json_add_object
56                                 json_add_string ip "${track}"
57                                 json_add_string status "$(cat "${file}")"
58                                 json_close_object
59                         fi
60                 done
61                 json_close_array
62                 json_close_object
63         fi
64 }
65
66 case "$1" in
67         list)
68                 json_init
69                 json_add_object "status"
70                 json_add_string "section" "x"
71                 json_add_string "interface" "x"
72                 json_close_object
73                 json_dump
74                 ;;
75         call)
76                 case "$2" in
77                 status)
78                         local section iface
79                         read input;
80                         json_load "$input"
81                         json_get_var section section
82                         json_get_var iface interface
83
84                         config_load mwan3
85                         json_init
86                         case "$section" in
87                                 interfaces)
88                                         json_add_object interfaces
89                                         config_foreach get_mwan3_status interface "${iface}"
90                                         json_close_object
91                                         ;;
92                                 connected)
93                                         json_add_object connected
94                                         json_add_array ipv4
95                                         report_connected_v4
96                                         json_close_array
97                                         json_add_array ipv6
98                                         report_connected_v6
99                                         json_close_array
100                                         json_close_object
101                                         ;;
102                                 *)
103                                         # interfaces
104                                         json_add_object interfaces
105                                         config_foreach get_mwan3_status interface
106                                         json_close_object
107                                         # connected
108                                         json_add_object connected
109                                         json_add_array ipv4
110                                         report_connected_v4
111                                         json_close_array
112                                         json_add_array ipv6
113                                         report_connected_v6
114                                         json_close_array
115                                         json_close_object
116                                         ;;
117                         esac
118                         json_dump
119                         ;;
120                 esac
121                 ;;
122 esac