mwan3track 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #!/bin/sh
  2. [ -z "$9" ] && echo "Error: should not be started manually" && exit 0
  3. if [ -e /var/run/mwan3track-$1.pid ] ; then
  4. kill $(cat /var/run/mwan3track-$1.pid) &> /dev/null
  5. rm /var/run/mwan3track-$1.pid &> /dev/null
  6. fi
  7. echo "$$" > /var/run/mwan3track-$1.pid
  8. score=$(($7+$8))
  9. track_ips=$(echo $* | cut -d ' ' -f 9-99)
  10. host_up_count=0
  11. lost=0
  12. while true; do
  13. for track_ip in $track_ips; do
  14. ping -I $2 -c $4 -W $5 -q $track_ip &> /dev/null
  15. if [ $? -eq 0 ]; then
  16. let host_up_count++
  17. else
  18. let lost++
  19. fi
  20. done
  21. if [ $host_up_count -lt $3 ]; then
  22. let score--
  23. if [ $score -lt $8 ]; then score=0 ; fi
  24. if [ $score -eq $8 ]; then
  25. logger -t mwan3track -p notice "Interface $1 ($2) is offline"
  26. env -i ACTION=ifdown INTERFACE=$1 DEVICE=$2 /sbin/hotplug-call iface
  27. score=0
  28. fi
  29. else
  30. if [ $score -lt $(($7+$8)) ] && [ $lost -gt 0 ]; then
  31. logger -t mwan3track -p info "Lost $(($lost*$4)) ping(s) on interface $1 ($2)"
  32. fi
  33. let score++
  34. lost=0
  35. if [ $score -gt $8 ]; then score=$(($7+$8)); fi
  36. if [ $score -eq $8 ]; then
  37. logger -t mwan3track -p notice "Interface $1 ($2) is online"
  38. env -i ACTION=ifup INTERFACE=$1 DEVICE=$2 /sbin/hotplug-call iface
  39. rm /var/run/mwan3track-$1.pid
  40. exit 0
  41. fi
  42. fi
  43. host_up_count=0
  44. sleep $6 &
  45. wait
  46. done
  47. exit 1