net/mwan3: add clean_up trap function for SIGINT SIGTERM
[feed/packages.git] / net / mwan3 / files / usr / sbin / mwan3track
1 #!/bin/sh
2
3 LOG="/usr/bin/logger -t $(basename "$0")[$$] -p"
4 INTERFACE=""
5
6 clean_up() {
7 $LOG notice "Stopping mwan3track for interface \"${INTERFACE}\""
8 rm "/var/run/mwan3track-${INTERFACE}.pid" &> /dev/null
9 exit 0
10 }
11
12 main() {
13
14 [ -z "$12" ] && echo "Error: should not be started manually" && exit 0
15
16 INTERFACE=$1
17 echo "$$" > /var/run/mwan3track-$1.pid
18 trap clean_up SIGINT SIGTERM
19
20 local score=$(($7+$8))
21 local track_ips=$(echo $* | cut -d ' ' -f 12-99)
22 local host_up_count=0
23 local lost=0
24 local sleep_time=0
25
26 while true; do
27
28 sleep_time=$6
29
30 for track_ip in $track_ips; do
31 ping -I $2 -c $4 -W $5 -s $9 -q $track_ip &> /dev/null
32 if [ $? -eq 0 ]; then
33 let host_up_count++
34 else
35 let lost++
36 fi
37 done
38
39 if [ $host_up_count -lt $3 ]; then
40 let score--
41
42 if [ $score -lt $8 ]; then
43 score=0
44 else
45 sleep_time=$10
46 fi
47
48 if [ $score -eq $8 ]; then
49 $LOG notice "Interface $1 ($2) is offline"
50 env -i ACTION=ifdown INTERFACE=$1 DEVICE=$2 /sbin/hotplug-call iface
51 score=0
52 fi
53 else
54 if [ $score -lt $(($7+$8)) ] && [ $lost -gt 0 ]; then
55 $LOG info "Lost $(($lost*$4)) ping(s) on interface $1 ($2)"
56 fi
57
58 let score++
59 lost=0
60
61 if [ $score -gt $8 ]; then
62 score=$(($7+$8))
63 elif [ $score -le $8 ]; then
64 sleep_time=$11
65 fi
66
67 if [ $score -eq $8 ]; then
68 $LOG notice "Interface $1 ($2) is online"
69 env -i ACTION=ifup INTERFACE=$1 DEVICE=$2 /sbin/hotplug-call iface
70 rm /var/run/mwan3track-$1.pid
71 exit 0
72 fi
73 fi
74
75 host_up_count=0
76 sleep $sleep_time
77 done
78 }
79
80 main "$@"