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.

197 lines
4.9 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. env -i ACTION="connected" INTERFACE="$1" DEVICE="$2" /sbin/hotplug-call iface
  91. fi
  92. while true; do
  93. sleep_time=$interval
  94. for track_ip in $track_ips; do
  95. if [ $host_up_count -lt $reliability ]; then
  96. case "$track_method" in
  97. ping)
  98. ping -I $DEVICE -c $count -W $timeout -s $size -q $track_ip &> /dev/null ;;
  99. arping)
  100. arping -I $DEVICE -c $count -w $timeout -q $track_ip &> /dev/null ;;
  101. httping)
  102. httping -y $SRC_IP -c $count -t $timeout -q $track_ip &> /dev/null ;;
  103. esac
  104. if [ $? -eq 0 ]; then
  105. let host_up_count++
  106. echo "up" > /var/run/mwan3track/$1/TRACK_${track_ip}
  107. if [ $score -le $up ]; then
  108. $LOG info "Check ($track_method) success for target \"$track_ip\" on interface $1 ($2)"
  109. fi
  110. else
  111. let lost++
  112. echo "down" > /var/run/mwan3track/$1/TRACK_${track_ip}
  113. if [ $score -gt $up ]; then
  114. $LOG info "Check ($track_method) failed for target \"$track_ip\" on interface $1 ($2)"
  115. fi
  116. fi
  117. else
  118. echo "skipped" > /var/run/mwan3track/$1/TRACK_${track_ip}
  119. fi
  120. done
  121. if [ $host_up_count -lt $reliability ]; then
  122. let score--
  123. if [ $score -lt $up ]; then
  124. score=0
  125. [ ${keep_failure_interval} -eq 1 ] && {
  126. sleep_time=$failure_interval
  127. }
  128. else
  129. sleep_time=$failure_interval
  130. fi
  131. if [ $score -eq $up ]; then
  132. echo "offline" > /var/run/mwan3track/$1/STATUS
  133. $LOG notice "Interface $1 ($2) is offline"
  134. env -i ACTION=ifdown INTERFACE=$1 DEVICE=$2 /sbin/hotplug-call iface
  135. env -i ACTION="disconnected" INTERFACE="$1" DEVICE="$2" /sbin/hotplug-call iface
  136. score=0
  137. fi
  138. else
  139. if [ $score -lt $(($down+$up)) ] && [ $lost -gt 0 ]; then
  140. $LOG info "Lost $(($lost*$count)) ping(s) on interface $1 ($2)"
  141. fi
  142. let score++
  143. lost=0
  144. if [ $score -gt $up ]; then
  145. echo "online" > /var/run/mwan3track/$1/STATUS
  146. score=$(($down+$up))
  147. elif [ $score -le $up ]; then
  148. sleep_time=$recovery_interval
  149. fi
  150. if [ $score -eq $up ]; then
  151. $LOG notice "Interface $1 ($2) is online"
  152. echo "online" > /var/run/mwan3track/$1/STATUS
  153. env -i ACTION=ifup INTERFACE=$1 DEVICE=$2 /sbin/hotplug-call iface
  154. exit 0
  155. fi
  156. fi
  157. let turn++
  158. mkdir -p "/var/run/mwan3track/${1}"
  159. echo "${lost}" > /var/run/mwan3track/$1/LOST
  160. echo "${score}" > /var/run/mwan3track/$1/SCORE
  161. echo "${turn}" > /var/run/mwan3track/$1/TURN
  162. echo "$(get_uptime)" > /var/run/mwan3track/$1/TIME
  163. host_up_count=0
  164. sleep "${sleep_time}" &
  165. wait
  166. if [ "${IFDOWN_EVENT}" -eq 1 ]; then
  167. score=0
  168. echo "offline" > /var/run/mwan3track/$1/STATUS
  169. IFDOWN_EVENT=0
  170. fi
  171. done
  172. }
  173. main "$@"