5befdce970422280eed875f4ecdb6fe1b752f7ec
[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_packet_size" : {},
130                         "tptest_running": {} }'
131         ;;
132         call)
133                 case "$2" in
134                         ping|ping6)
135                                 cmd=$2
136                                 host=""
137                                 seq=""
138                                 count=""
139                                 timeout=""
140                                 read input
141                                 json_load "$input"
142                                 json_get_var host host
143                                 json_get_var seq seq 0
144                                 json_get_var count count 1
145                                 json_get_var timeout timeout 1
146                                 validate_ping_traceroute $host $seq $count $timeout && {
147                                         diagnostics -U -s $seq -C $cmd -h $host -c $count -W $timeout >/dev/null 2>&1 &
148                                         json_init
149                                         json_add_string "state" "running"
150                                         json_add_string "message" "success"
151                                         json_dump
152                                 } || {
153                                         json_init
154                                         json_add_string "state" "error"
155                                         json_add_string "message" "invalid arguments"
156                                         json_dump
157                                 }
158                                 ;;
159                         traceroute|traceroute6)
160                                 cmd=$2
161                                 host=""
162                                 seq=""
163                                 count=""
164                                 timeout=""
165                                 read input
166                                 json_load "$input"
167                                 json_get_var host host
168                                 json_get_var seq seq 0
169                                 json_get_var count count 1
170                                 json_get_var timeout timeout 1
171                                 validate_ping_traceroute $host $seq $count $timeout && {
172                                         diagnostics -U -s $seq -C $cmd -h $host -q $count -w $timeout >/dev/null 2>&1 &
173                                         json_init
174                                         json_add_string "state" "running"
175                                         json_add_string "message" "success"
176                                         json_dump
177                                 } || {
178                                         json_init
179                                         json_add_string "state" "error"
180                                         json_add_string "message" "invalid arguments"
181                                         json_dump
182                                 }
183                                 ;;
184                         tptest_start)
185                                 host=""
186                                 seq=""
187                                 testmode=""
188                                 packetsize_up=""
189                                 packetsize_down=""
190                                 port=""
191                                 auto=""
192                                 read input
193                                 json_load "$input"
194                                 json_get_var host host ""
195                                 json_get_var seq seq 1
196                                 json_get_var testmode testmode "up_down"
197                                 json_get_var packetsize_up packetsize_up 50000
198                                 json_get_var packetsize_down packetsize_down 50000
199                                 json_get_var port port 0
200                                 json_get_var auto auto 1
201                                 validate_arguments_tptest "$host" "$testmode" "$packetsize_up" "$packetsize_down" "$port" "$auto" && {
202                                         if [ $auto -eq 1 ]; then
203                                                 diagnostics -U -s $seq -C tptest -a -t $testmode -h $host -p $port >/dev/null 2>&1 &
204                                         else
205                                                 diagnostics -U -s $seq -C tptest -u $packetsize_up -d $packetsize_down -t $testmode -h $host -p $port >/dev/null 2>&1 &
206                                         fi
207                                         json_init
208                                         json_add_string "state" "running"
209                                         json_add_string "message" "success"
210                                         json_dump
211                                 } || {
212                                         json_init
213                                         json_add_string "state" "error"
214                                         json_add_string "message" "invalid arguments"
215                                         json_dump
216                                 }
217                         ;;
218                         tptest_running)
219                                 json_init
220                                 seqs="$(ps -w | awk '/diagnostic[s].*-C tptest/ { print $10 }' | sort -u)"
221                                 if [ "$seqs" == "" ]; then
222                                         json_add_string "message" "Not running"
223                                         json_add_boolean "running" 0
224                                         json_dump
225                                         exit
226                                 else
227                                         json_add_string "message" "Running"
228                                         json_add_boolean "running" 1
229                                 fi
230                                 json_add_array "sequence numbers"
231                                 for s in $seqs; do
232                                         json_add_int "" $s
233                                 done
234                                 json_close_array
235                                 json_dump
236                         ;;
237                         tptest_packet_size)
238                                 get_iface
239
240                                 for i in $ifaces; do
241                                         if [ $iface == "$i" ]; then
242                                                 get_packet_sizes $iface
243                                                 json_init
244                                                 json_add_string "packetsize_up" "$packetsize_up"
245                                                 json_add_string "packetsize_down" "$packetsize_down"
246                                                 json_dump
247                                                 return
248                                         fi
249                                 done
250                         ;;
251                         *) return 1 ;;
252                 esac
253         ;;
254 esac