diagnostics: add tptest_stop functionality
[juci.git] / plugins / juci-diagnostics / ubus / juci.diagnostics
1 #!/bin/sh
2
3 . /usr/share/libubox/jshn.sh
4 . /lib/functions.sh
5
6 is_num() {
7         [ "$1" == "" ] && return 1
8         [ "$(echo $1 | sed 's/[0-9]//g')" == "" ] && return 0 || return 1
9 }
10
11 validate_ping_traceroute() {
12         host="$1"
13         seq="$2"
14         count="$3"
15         timeout="$4"
16         [ "$host" == "" ] && return 1
17         is_num $seq || return 1
18         is_num $count || return 1
19         is_num $timeout || return 1
20         return 0
21 }
22 validate_arguments_tptest() {
23         host="$1"
24         testmode="$2"
25         packetsize_up="$3"
26         packetsize_down="$4"
27         port="$5"
28         auto="$6"
29         is_num $port || return 1
30         [ "$host" == "" ] && return 1
31         case $testmode in
32                 up|down|up_down);;
33                 *) return 1 ;;
34         esac
35         if [ "$auto" == "0" ]; then
36                 case $testmode in
37                         *up*)
38                                 is_num $packetsize_up || return 1
39                                 ;;
40                 esac
41                 case $testmode in
42                         *down*)
43                                 is_num $packetsize_down || return 1
44                                 ;;
45                 esac
46         elif [ "$auto" == "1" ]; then
47                 return 0;
48         else
49                 return 1
50         fi
51 }
52
53 run_cmd() {
54         CMND=$1
55         input=$2
56
57         read input;
58
59         json_load "$input"
60         json_get_var host host
61
62         json_init
63         json_add_string stdout "$($CMND $host)"
64         json_dump
65 }
66
67 ifaces="ethernet vdsl adsl"
68
69 get_packet_sizes () {
70         option="$1"
71         packetsizes=$(uci -q get speedtest.@packetsize[0].$option)
72
73         if [ "$packetsizes" ]; then
74                 packetsize_down=${packetsizes%/*}
75                 packetsize_up=${packetsizes#*/}
76         else
77                 case "$option" in
78                         ethernet)
79                                 packetsize_up=5
80                                 packetsize_down=100
81                                 ;;
82                         vdsl)
83                                 packetsize_up=3
84                                 packetsize_down=50
85                                 ;;
86                         adsl)
87                                 packetsize_up=1
88                                 packetsize_down=25
89                                 ;;
90                         *)
91                                 packetsize_up=1
92                                 packetsize_down=25
93                 esac
94         fi
95 }
96
97 get_iface () {
98         iface=$(ip route | awk '/default via/{print $5}')
99         case "$iface" in
100                 eth*)
101                         iface="ethernet"
102                 ;;
103                 atm*)
104                         iface="adsl"
105                 ;;
106                 ptm*)
107                         iface="vdsl"
108                 ;;
109                 wwan*)
110                         iface="vdsl"
111                 ;;
112                 br-*)
113                         iface="ethernet"
114                 ;;
115                 *)
116                         iface="ethernet"
117         esac
118 }
119
120 input=""
121
122 case "$1" in
123         list)
124                 echo '{ "ping" : {"host":"str", "seq":32, "count":32, "timeout":32},
125                         "ping6" : {"host":"str", "seq":32, "count":32, "timeout":32},
126                         "traceroute" : {"host":"str", "seq":32, "querycount":32, "timeout":32},
127                         "traceroute6" : {"host":"str", "seq":32, "querycount":32, "timeout":32},
128                         "tptest_start" : {"seq":32, "auto": true, "testmode":"str","packetsize_up":32,"packetsize_down":32,"port":32},
129                         "tptest_stop" : {},
130                         "tptest_packet_size" : {},
131                         "tptest_running": {} }'
132         ;;
133         call)
134                 case "$2" in
135                         ping|ping6)
136                                 cmd=$2
137                                 host=""
138                                 seq=""
139                                 count=""
140                                 timeout=""
141                                 read input
142                                 json_load "$input"
143                                 json_get_var host host
144                                 json_get_var seq seq 0
145                                 json_get_var count count 1
146                                 json_get_var timeout timeout 1
147                                 validate_ping_traceroute $host $seq $count $timeout && {
148                                         diagnostics -U -s $seq -C $cmd -h $host -c $count -W $timeout >/dev/null 2>&1 &
149                                         json_init
150                                         json_add_string "state" "running"
151                                         json_add_string "message" "success"
152                                         json_dump
153                                 } || {
154                                         json_init
155                                         json_add_string "state" "error"
156                                         json_add_string "message" "invalid arguments"
157                                         json_dump
158                                 }
159                                 ;;
160                         traceroute|traceroute6)
161                                 cmd=$2
162                                 host=""
163                                 seq=""
164                                 count=""
165                                 timeout=""
166                                 read input
167                                 json_load "$input"
168                                 json_get_var host host
169                                 json_get_var seq seq 0
170                                 json_get_var count count 1
171                                 json_get_var timeout timeout 1
172                                 validate_ping_traceroute $host $seq $count $timeout && {
173                                         diagnostics -U -s $seq -C $cmd -h $host -q $count -w $timeout >/dev/null 2>&1 &
174                                         json_init
175                                         json_add_string "state" "running"
176                                         json_add_string "message" "success"
177                                         json_dump
178                                 } || {
179                                         json_init
180                                         json_add_string "state" "error"
181                                         json_add_string "message" "invalid arguments"
182                                         json_dump
183                                 }
184                                 ;;
185                         tptest_start)
186                                 host=""
187                                 seq=""
188                                 testmode=""
189                                 packetsize_up=""
190                                 packetsize_down=""
191                                 port=""
192                                 auto=""
193                                 read input
194                                 json_load "$input"
195                                 json_get_var host host ""
196                                 json_get_var seq seq 1
197                                 json_get_var testmode testmode "up_down"
198                                 json_get_var packetsize_up packetsize_up 50000
199                                 json_get_var packetsize_down packetsize_down 50000
200                                 json_get_var port port 0
201                                 json_get_var auto auto 1
202                                 validate_arguments_tptest "$host" "$testmode" "$packetsize_up" "$packetsize_down" "$port" "$auto" && {
203                                         if [ $auto -eq 1 ]; then
204                                                 diagnostics -U -s $seq -C tptest -a -t $testmode -h $host -p $port >/dev/null 2>&1 &
205                                         else
206                                                 diagnostics -U -s $seq -C tptest -u $packetsize_up -d $packetsize_down -t $testmode -h $host -p $port >/dev/null 2>&1 &
207                                         fi
208                                         json_init
209                                         json_add_string "state" "running"
210                                         json_add_string "message" "success"
211                                         json_dump
212                                 } || {
213                                         json_init
214                                         json_add_string "state" "error"
215                                         json_add_string "message" "invalid arguments"
216                                         json_dump
217                                 }
218                         ;;
219                         tptest_stop)
220                                 killall -9 diagnostics >/dev/null 2>&1
221                                 killall -9 tptest >/dev/null 2>&1
222                                 json_init                                                                                                                      
223                                 json_add_string "state" "aborted"                                                                                              
224                                 json_add_string "message" "success"                                                                                            
225                                 json_dump 
226                         ;;
227                         tptest_running)
228                                 json_init
229                                 seqs="$(ps -w | awk '/diagnostic[s].*-C tptest/ { print $10 }' | sort -u)"
230                                 if [ "$seqs" == "" ]; then
231                                         json_add_string "message" "Not running"
232                                         json_add_boolean "running" 0
233                                         json_dump
234                                         exit
235                                 else
236                                         json_add_string "message" "Running"
237                                         json_add_boolean "running" 1
238                                 fi
239                                 json_add_array "sequence numbers"
240                                 for s in $seqs; do
241                                         json_add_int "" $s
242                                 done
243                                 json_close_array
244                                 json_dump
245                         ;;
246                         tptest_packet_size)
247                                 get_iface
248
249                                 for i in $ifaces; do
250                                         if [ $iface == "$i" ]; then
251                                                 get_packet_sizes $iface
252                                                 json_init
253                                                 json_add_string "packetsize_up" "$packetsize_up"
254                                                 json_add_string "packetsize_down" "$packetsize_down"
255                                                 json_dump
256                                                 return
257                                         fi
258                                 done
259                         ;;
260                         *) return 1 ;;
261                 esac
262         ;;
263 esac