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.

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