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.

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