You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
1.5 KiB

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