mwan3track 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #!/bin/sh
  2. . /lib/functions.sh
  3. LOG="logger -t $(basename "$0")[$$] -p"
  4. INTERFACE=""
  5. DEVICE=""
  6. IFDOWN_EVENT=0
  7. clean_up() {
  8. $LOG notice "Stopping mwan3track for interface \"${INTERFACE}\""
  9. if [ "$(pgrep -f "mwan3track ${INTERFACE}")" = "" ]; then
  10. rm -rf "/var/run/mwan3track/${INTERFACE}" &> /dev/null
  11. fi
  12. if [ -z "$(ls -A "/var/run/mwan3track")" ]; then
  13. rm -rf "/var/run/mwan3track"
  14. fi
  15. exit 0
  16. }
  17. if_down() {
  18. $LOG info "Detect ifdown event on interface ${INTERFACE} (${DEVICE})"
  19. IFDOWN_EVENT=1
  20. }
  21. main() {
  22. local reliability count timeout interval failure_interval
  23. local recovery_interval down up size
  24. [ -z "$3" ] && echo "Error: should not be started manually" && exit 0
  25. INTERFACE=$1
  26. DEVICE=$2
  27. mkdir -p /var/run/mwan3track/$1
  28. trap clean_up SIGINT SIGTERM
  29. trap if_down SIGUSR1
  30. config_load mwan3
  31. config_get reliability $1 reliability 1
  32. config_get count $1 count 1
  33. config_get timeout $1 timeout 4
  34. config_get interval $1 interval 10
  35. config_get down $1 down 5
  36. config_get up $1 up 5
  37. config_get size $1 size 56
  38. config_get failure_interval $1 failure_interval $interval
  39. config_get recovery_interval $1 recovery_interval $interval
  40. local score=$(($down+$up))
  41. local track_ips=$(echo $* | cut -d ' ' -f 3-99)
  42. local host_up_count=0
  43. local lost=0
  44. local sleep_time=0
  45. local turn=0
  46. echo "offline" > /var/run/mwan3track/$1/STATUS
  47. while true; do
  48. sleep_time=$interval
  49. for track_ip in $track_ips; do
  50. ping -I $2 -c $count -W $timeout -s $size -q $track_ip &> /dev/null
  51. if [ $? -eq 0 ]; then
  52. let host_up_count++
  53. echo "up" > /var/run/mwan3track/$1/TRACK_${track_ip}
  54. else
  55. let lost++
  56. echo "down" > /var/run/mwan3track/$1/TRACK_${track_ip}
  57. fi
  58. done
  59. if [ $host_up_count -lt $reliability ]; then
  60. let score--
  61. if [ $score -lt $up ]; then
  62. score=0
  63. else
  64. sleep_time=$failure_interval
  65. fi
  66. if [ $score -eq $up ]; then
  67. echo "offline" > /var/run/mwan3track/$1/STATUS
  68. $LOG notice "Interface $1 ($2) is offline"
  69. env -i ACTION=ifdown INTERFACE=$1 DEVICE=$2 /sbin/hotplug-call iface
  70. score=0
  71. fi
  72. else
  73. if [ $score -lt $(($down+$up)) ] && [ $lost -gt 0 ]; then
  74. $LOG info "Lost $(($lost*$count)) ping(s) on interface $1 ($2)"
  75. fi
  76. let score++
  77. lost=0
  78. if [ $score -gt $up ]; then
  79. echo "online" > /var/run/mwan3track/$1/STATUS
  80. score=$(($down+$up))
  81. elif [ $score -le $up ]; then
  82. sleep_time=$recovery_interval
  83. fi
  84. if [ $score -eq $up ]; then
  85. $LOG notice "Interface $1 ($2) is online"
  86. env -i ACTION=ifup INTERFACE=$1 DEVICE=$2 /sbin/hotplug-call iface
  87. rm -rf "/var/run/mwan3track/${1}" &> /dev/null
  88. exit 0
  89. fi
  90. fi
  91. let turn++
  92. mkdir -p "/var/run/mwan3track/${1}"
  93. echo "${lost}" > /var/run/mwan3track/$1/LOST
  94. echo "${score}" > /var/run/mwan3track/$1/SCORE
  95. echo "${turn}" > /var/run/mwan3track/$1/TURN
  96. host_up_count=0
  97. sleep "${sleep_time}" &
  98. wait
  99. if [ "${IFDOWN_EVENT}" -eq 1 ]; then
  100. score=0
  101. echo "offline" > /var/run/mwan3track/$1/STATUS
  102. IFDOWN_EVENT=0
  103. fi
  104. done
  105. }
  106. main "$@"