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.

247 lines
6.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 check_quality failure_latency
  50. local recovery_latency failure_loss recovery_loss
  51. [ -z "$5" ] && echo "Error: should not be started manually" && exit 0
  52. INTERFACE=$1
  53. DEVICE=$2
  54. STATUS=$3
  55. SRC_IP=$4
  56. mkdir -p /var/run/mwan3track/$1
  57. trap clean_up TERM
  58. trap if_down USR1
  59. config_load mwan3
  60. config_get track_method $1 track_method ping
  61. validate_track_method $track_method $SRC_IP || {
  62. track_method=ping
  63. if validate_track_method $track_method; then
  64. $LOG warn "Using ping to track interface $INTERFACE avaliability"
  65. else
  66. $LOG err "No track method avaliable"
  67. exit 1
  68. fi
  69. }
  70. config_get reliability $1 reliability 1
  71. config_get count $1 count 1
  72. config_get timeout $1 timeout 4
  73. config_get interval $1 interval 10
  74. config_get down $1 down 5
  75. config_get up $1 up 5
  76. config_get size $1 size 56
  77. config_get failure_interval $1 failure_interval $interval
  78. config_get_bool keep_failure_interval $1 keep_failure_interval 0
  79. config_get recovery_interval $1 recovery_interval $interval
  80. config_get_bool check_quality $1 check_quality 0
  81. config_get failure_latency $1 failure_latency 1000
  82. config_get recovery_latency $1 recovery_latency 500
  83. config_get failure_loss $1 failure_loss 40
  84. config_get recovery_loss $1 recovery_loss 10
  85. local score=$(($down+$up))
  86. local track_ips=$(echo $* | cut -d ' ' -f 5-99)
  87. local host_up_count=0
  88. local lost=0
  89. local sleep_time=0
  90. local turn=0
  91. local result
  92. local ping_result
  93. local loss=0
  94. local latency=0
  95. if [ "$STATUS" = "unknown" ]; then
  96. echo "unknown" > /var/run/mwan3track/$1/STATUS
  97. score=0
  98. else
  99. echo "online" > /var/run/mwan3track/$1/STATUS
  100. env -i ACTION="connected" INTERFACE="$1" DEVICE="$2" /sbin/hotplug-call iface
  101. fi
  102. while true; do
  103. sleep_time=$interval
  104. for track_ip in $track_ips; do
  105. if [ $host_up_count -lt $reliability ]; then
  106. case "$track_method" in
  107. ping)
  108. if [ $check_quality -eq 0 ]; then
  109. ping -I $DEVICE -c $count -W $timeout -s $size -q $track_ip &> /dev/null
  110. result=$?
  111. else
  112. ping_result="$(ping -I $DEVICE -c $count -W $timeout -s $size -q $track_ip | tail -2)"
  113. loss="$(echo "$ping_result" | grep "packet loss" | cut -d "," -f3 | awk '{print $1}' | sed -e 's/%//')"
  114. if [ "$loss" -eq 100 ]; then
  115. latency=999999
  116. else
  117. latency="$(echo "$ping_result" | grep -E 'rtt|round-trip' | cut -d "=" -f2 | cut -d "/" -f2 | cut -d "." -f1)"
  118. fi
  119. fi
  120. ;;
  121. arping)
  122. arping -I $DEVICE -c $count -w $timeout -q $track_ip &> /dev/null
  123. result=$?
  124. ;;
  125. httping)
  126. httping -y $SRC_IP -c $count -t $timeout -q $track_ip &> /dev/null
  127. result=$?
  128. ;;
  129. esac
  130. if [ $check_quality -eq 0 ]; then
  131. if [ $result -eq 0 ]; then
  132. let host_up_count++
  133. echo "up" > /var/run/mwan3track/$1/TRACK_${track_ip}
  134. if [ $score -le $up ]; then
  135. $LOG info "Check ($track_method) success for target \"$track_ip\" on interface $1 ($2)"
  136. fi
  137. else
  138. let lost++
  139. echo "down" > /var/run/mwan3track/$1/TRACK_${track_ip}
  140. if [ $score -gt $up ]; then
  141. $LOG info "Check ($track_method) failed for target \"$track_ip\" on interface $1 ($2)"
  142. fi
  143. fi
  144. else
  145. if [ "$loss" -ge "$failure_loss" -o "$latency" -ge "$failure_latency" ]; then
  146. let lost++
  147. echo "down" > /var/run/mwan3track/$1/TRACK_${track_ip}
  148. echo "$latency" > /var/run/mwan3track/$1/LATENCY_${track_ip}
  149. echo "$loss" > /var/run/mwan3track/$1/LOSS_${track_ip}
  150. if [ $score -gt $up ]; then
  151. $LOG info "Check (${track_method}: latency=${latency}ms loss=${loss}%) failed for target \"$track_ip\" on interface $1 ($2)"
  152. fi
  153. elif [ "$loss" -le "$recovery_loss" -a "$latency" -le "$recovery_latency" ]; then
  154. let host_up_count++
  155. echo "up" > /var/run/mwan3track/$1/TRACK_${track_ip}
  156. echo "$latency" > /var/run/mwan3track/$1/LATENCY_${track_ip}
  157. echo "$loss" > /var/run/mwan3track/$1/LOSS_${track_ip}
  158. if [ $score -le $up ]; then
  159. $LOG info "Check (${track_method}: latency=${latency}ms loss=${loss}%) success for target \"$track_ip\" on interface $1 ($2)"
  160. fi
  161. else
  162. echo "skipped" > /var/run/mwan3track/$1/TRACK_${track_ip}
  163. fi
  164. fi
  165. else
  166. echo "skipped" > /var/run/mwan3track/$1/TRACK_${track_ip}
  167. fi
  168. done
  169. if [ $host_up_count -lt $reliability ]; then
  170. let score--
  171. if [ $score -lt $up ]; then
  172. score=0
  173. [ ${keep_failure_interval} -eq 1 ] && {
  174. sleep_time=$failure_interval
  175. }
  176. else
  177. sleep_time=$failure_interval
  178. fi
  179. if [ $score -eq $up ]; then
  180. echo "offline" > /var/run/mwan3track/$1/STATUS
  181. $LOG notice "Interface $1 ($2) is offline"
  182. env -i ACTION=ifdown INTERFACE=$1 DEVICE=$2 /sbin/hotplug-call iface
  183. env -i ACTION="disconnected" INTERFACE="$1" DEVICE="$2" /sbin/hotplug-call iface
  184. score=0
  185. fi
  186. else
  187. if [ $score -lt $(($down+$up)) ] && [ $lost -gt 0 ]; then
  188. $LOG info "Lost $(($lost*$count)) ping(s) on interface $1 ($2)"
  189. fi
  190. let score++
  191. lost=0
  192. if [ $score -gt $up ]; then
  193. echo "online" > /var/run/mwan3track/$1/STATUS
  194. score=$(($down+$up))
  195. elif [ $score -le $up ]; then
  196. sleep_time=$recovery_interval
  197. fi
  198. if [ $score -eq $up ]; then
  199. $LOG notice "Interface $1 ($2) is online"
  200. echo "online" > /var/run/mwan3track/$1/STATUS
  201. env -i ACTION=ifup INTERFACE=$1 DEVICE=$2 /sbin/hotplug-call iface
  202. exit 0
  203. fi
  204. fi
  205. let turn++
  206. mkdir -p "/var/run/mwan3track/${1}"
  207. echo "${lost}" > /var/run/mwan3track/$1/LOST
  208. echo "${score}" > /var/run/mwan3track/$1/SCORE
  209. echo "${turn}" > /var/run/mwan3track/$1/TURN
  210. echo "$(get_uptime)" > /var/run/mwan3track/$1/TIME
  211. host_up_count=0
  212. sleep "${sleep_time}" &
  213. wait
  214. if [ "${IFDOWN_EVENT}" -eq 1 ]; then
  215. score=0
  216. echo "offline" > /var/run/mwan3track/$1/STATUS
  217. IFDOWN_EVENT=0
  218. fi
  219. done
  220. }
  221. main "$@"