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.

176 lines
4.1 KiB

  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. exit 0
  10. }
  11. if_down() {
  12. $LOG info "Detect ifdown event on interface ${INTERFACE} (${DEVICE})"
  13. IFDOWN_EVENT=1
  14. }
  15. validate_track_method() {
  16. case "$1" in
  17. ping)
  18. # Assume that ping is installed
  19. ;;
  20. arping)
  21. which arping 1>/dev/null 2>&1 || {
  22. $LOG warn "Missing arping. Please install iputils-arping package."
  23. return 1
  24. }
  25. ;;
  26. httping)
  27. which httping 1>/dev/null 2>&1 || {
  28. $LOG warn "Missing httping. Please install httping package."
  29. return 1
  30. }
  31. ;;
  32. *)
  33. $LOG warn "Unsupported tracking method: $track_method"
  34. return 2
  35. ;;
  36. esac
  37. }
  38. main() {
  39. local reliability count timeout interval failure_interval
  40. local recovery_interval down up size
  41. local keep_failure_interval
  42. [ -z "$5" ] && echo "Error: should not be started manually" && exit 0
  43. INTERFACE=$1
  44. DEVICE=$2
  45. STATUS=$3
  46. SRC_IP=$4
  47. mkdir -p /var/run/mwan3track/$1
  48. trap clean_up SIGINT SIGTERM
  49. trap if_down SIGUSR1
  50. config_load mwan3
  51. config_get track_method $1 track_method ping
  52. validate_track_method $track_method || {
  53. $LOG warn "Using ping to track interface $INTERFACE avaliability"
  54. track_method=ping
  55. }
  56. config_get reliability $1 reliability 1
  57. config_get count $1 count 1
  58. config_get timeout $1 timeout 4
  59. config_get interval $1 interval 10
  60. config_get down $1 down 5
  61. config_get up $1 up 5
  62. config_get size $1 size 56
  63. config_get failure_interval $1 failure_interval $interval
  64. config_get_bool keep_failure_interval $1 keep_failure_interval 0
  65. config_get recovery_interval $1 recovery_interval $interval
  66. local score=$(($down+$up))
  67. local track_ips=$(echo $* | cut -d ' ' -f 5-99)
  68. local host_up_count=0
  69. local lost=0
  70. local sleep_time=0
  71. local turn=0
  72. if [ "$STATUS" = "unknown" ]; then
  73. echo "unknown" > /var/run/mwan3track/$1/STATUS
  74. score=0
  75. else
  76. echo "online" > /var/run/mwan3track/$1/STATUS
  77. fi
  78. while true; do
  79. sleep_time=$interval
  80. for track_ip in $track_ips; do
  81. if [ $host_up_count -lt $reliability ]; then
  82. case "$track_method" in
  83. ping)
  84. ping -I $DEVICE -c $count -W $timeout -s $size -q $track_ip &> /dev/null ;;
  85. arping)
  86. arping -I $DEVICE -c $count -w $timeout -q $track_ip &> /dev/null ;;
  87. httping)
  88. httping -y $SRC_IP -c $count -t $timeout -q $track_ip &> /dev/null ;;
  89. esac
  90. if [ $? -eq 0 ]; then
  91. let host_up_count++
  92. echo "up" > /var/run/mwan3track/$1/TRACK_${track_ip}
  93. else
  94. let lost++
  95. echo "down" > /var/run/mwan3track/$1/TRACK_${track_ip}
  96. fi
  97. else
  98. echo "skipped" > /var/run/mwan3track/$1/TRACK_${track_ip}
  99. fi
  100. done
  101. if [ $host_up_count -lt $reliability ]; then
  102. let score--
  103. if [ $score -lt $up ]; then
  104. score=0
  105. [ ${keep_failure_interval} -eq 1 ] && {
  106. sleep_time=$failure_interval
  107. }
  108. else
  109. sleep_time=$failure_interval
  110. fi
  111. if [ $score -eq $up ]; then
  112. echo "offline" > /var/run/mwan3track/$1/STATUS
  113. $LOG notice "Interface $1 ($2) is offline"
  114. env -i ACTION=ifdown INTERFACE=$1 DEVICE=$2 /sbin/hotplug-call iface
  115. score=0
  116. fi
  117. else
  118. if [ $score -lt $(($down+$up)) ] && [ $lost -gt 0 ]; then
  119. $LOG info "Lost $(($lost*$count)) ping(s) on interface $1 ($2)"
  120. fi
  121. let score++
  122. lost=0
  123. if [ $score -gt $up ]; then
  124. echo "online" > /var/run/mwan3track/$1/STATUS
  125. score=$(($down+$up))
  126. elif [ $score -le $up ]; then
  127. sleep_time=$recovery_interval
  128. fi
  129. if [ $score -eq $up ]; then
  130. $LOG notice "Interface $1 ($2) is online"
  131. echo "online" > /var/run/mwan3track/$1/STATUS
  132. env -i ACTION=ifup INTERFACE=$1 DEVICE=$2 /sbin/hotplug-call iface
  133. exit 0
  134. fi
  135. fi
  136. let turn++
  137. mkdir -p "/var/run/mwan3track/${1}"
  138. echo "${lost}" > /var/run/mwan3track/$1/LOST
  139. echo "${score}" > /var/run/mwan3track/$1/SCORE
  140. echo "${turn}" > /var/run/mwan3track/$1/TURN
  141. echo "$(date +'%s')" > /var/run/mwan3track/$1/TIME
  142. host_up_count=0
  143. sleep "${sleep_time}" &
  144. wait
  145. if [ "${IFDOWN_EVENT}" -eq 1 ]; then
  146. score=0
  147. echo "offline" > /var/run/mwan3track/$1/STATUS
  148. IFDOWN_EVENT=0
  149. fi
  150. done
  151. }
  152. main "$@"