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.

189 lines
4.5 KiB

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