summaryrefslogtreecommitdiff
path: root/net/mwan3/files/usr/sbin/mwan3track
blob: c4d25e99d26d33baf7c580efe61d6da50495ebb4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/bin/sh

[ -z "$9" ] && echo "Error: should not be started manually" && exit 0

if [ -e /var/run/mwan3track-$1.pid ] ; then
	kill $(cat /var/run/mwan3track-$1.pid) &> /dev/null
	rm /var/run/mwan3track-$1.pid &> /dev/null
fi

echo "$$" > /var/run/mwan3track-$1.pid

score=$(($7+$8))
track_ips=$(echo $* | cut -d ' ' -f 9-99)
host_up_count=0
lost=0

while true; do

	for track_ip in $track_ips; do
		ping -I $2 -c $4 -W $5 -s 4 -q $track_ip &> /dev/null
		if [ $? -eq 0 ]; then
			let host_up_count++
		else
			let lost++
		fi
	done
		
	if [ $host_up_count -lt $3 ]; then
		let score--

		if [ $score -lt $8 ]; then score=0 ; fi
		if [ $score -eq $8 ]; then

			logger -t mwan3track -p notice "Interface $1 ($2) is offline"
			env -i ACTION=ifdown INTERFACE=$1 DEVICE=$2 /sbin/hotplug-call iface
			score=0

		fi

	else

		if [ $score -lt $(($7+$8)) ] && [ $lost -gt 0 ]; then

			logger -t mwan3track -p info "Lost $(($lost*$4)) ping(s) on interface $1 ($2)"

		fi

		let score++
		lost=0

		if [ $score -gt $8 ]; then score=$(($7+$8)); fi
		if [ $score -eq $8 ]; then

			logger -t mwan3track -p notice "Interface $1 ($2) is online"
			env -i ACTION=ifup INTERFACE=$1 DEVICE=$2 /sbin/hotplug-call iface
			rm /var/run/mwan3track-$1.pid
			exit 0
		fi
	fi

	host_up_count=0
	sleep $6
done

exit 1