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.

284 lines
8.4 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 httping_ssl
  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. config_get_bool httping_ssl $1 httping_ssl 0
  69. validate_track_method $track_method $SRC_IP || {
  70. track_method=ping
  71. if validate_track_method $track_method; then
  72. $LOG warn "Using ping to track interface $INTERFACE avaliability"
  73. else
  74. $LOG err "No track method avaliable"
  75. exit 1
  76. fi
  77. }
  78. config_get reliability $1 reliability 1
  79. config_get count $1 count 1
  80. config_get timeout $1 timeout 4
  81. config_get interval $1 interval 10
  82. config_get down $1 down 5
  83. config_get up $1 up 5
  84. config_get size $1 size 56
  85. config_get max_ttl $1 max_ttl 60
  86. config_get failure_interval $1 failure_interval $interval
  87. config_get_bool keep_failure_interval $1 keep_failure_interval 0
  88. config_get recovery_interval $1 recovery_interval $interval
  89. config_get_bool check_quality $1 check_quality 0
  90. config_get failure_latency $1 failure_latency 1000
  91. config_get recovery_latency $1 recovery_latency 500
  92. config_get failure_loss $1 failure_loss 40
  93. config_get recovery_loss $1 recovery_loss 10
  94. local score=$(($down+$up))
  95. local track_ips=$(echo $* | cut -d ' ' -f 5-99)
  96. local host_up_count=0
  97. local lost=0
  98. local sleep_time=0
  99. local turn=0
  100. local result
  101. local ping_result
  102. local loss=0
  103. local latency=0
  104. if [ "$STATUS" = "offline" ]; then
  105. echo "offline" > /var/run/mwan3track/$1/STATUS
  106. echo "0" > /var/run/mwan3track/$1/ONLINE
  107. echo "$(get_uptime)" > /var/run/mwan3track/$1/OFFLINE
  108. score=0
  109. else
  110. echo "online" > /var/run/mwan3track/$1/STATUS
  111. echo "0" > /var/run/mwan3track/$1/OFFLINE
  112. echo "$(get_uptime)" > /var/run/mwan3track/$1/ONLINE
  113. env -i ACTION="connected" INTERFACE="$1" DEVICE="$2" /sbin/hotplug-call iface
  114. fi
  115. while true; do
  116. sleep_time=$interval
  117. for track_ip in $track_ips; do
  118. if [ $host_up_count -lt $reliability ]; then
  119. case "$track_method" in
  120. ping)
  121. # pinging IPv6 hosts with an interface is troublesome
  122. # https://bugs.openwrt.org/index.php?do=details&task_id=2897
  123. # so get the IP address of the interface and use that instead
  124. if echo $track_ip | grep -q ':'; then
  125.  ADDR=$(ip -6 addr ls dev "$DEVICE" | sed -ne 's/ *inet6 \([^ \/]*\).* scope global.*/\1/p')
  126. fi
  127. if [ $check_quality -eq 0 ]; then
  128. ping -I ${ADDR:-$DEVICE} -c $count -W $timeout -s $size -t $max_ttl -q $track_ip &> /dev/null
  129. result=$?
  130. else
  131. ping_result="$(ping -I ${ADDR:-$DEVICE} -c $count -W $timeout -s $size -t $max_ttl -q $track_ip | tail -2)"
  132. loss="$(echo "$ping_result" | grep "packet loss" | cut -d "," -f3 | awk '{print $1}' | sed -e 's/%//')"
  133. if [ "$loss" -eq 100 ]; then
  134. latency=999999
  135. else
  136. latency="$(echo "$ping_result" | grep -E 'rtt|round-trip' | cut -d "=" -f2 | cut -d "/" -f2 | cut -d "." -f1)"
  137. fi
  138. fi
  139. ;;
  140. arping)
  141. arping -I $DEVICE -c $count -w $timeout -q $track_ip &> /dev/null
  142. result=$?
  143. ;;
  144. httping)
  145. if [ "$httping_ssl" -eq 1 ]; then
  146. httping -y $SRC_IP -c $count -t $timeout -q "https://$track_ip" &> /dev/null
  147. else
  148. httping -y $SRC_IP -c $count -t $timeout -q "http://$track_ip" &> /dev/null
  149. fi
  150. result=$?
  151. ;;
  152. nping-tcp)
  153. result=$(nping -e $DEVICE -c $count $track_ip --tcp | grep Lost | awk '{print $12}')
  154. ;;
  155. nping-udp)
  156. result=$(nping -e $DEVICE -c $count $track_ip --udp | grep Lost | awk '{print $12}')
  157. ;;
  158. nping-icmp)
  159. result=$(nping -e $DEVICE -c $count $track_ip --icmp | grep Lost | awk '{print $12}')
  160. ;;
  161. nping-arp)
  162. result=$(nping -e $DEVICE -c $count $track_ip --arp | grep Lost | awk '{print $12}')
  163. ;;
  164. esac
  165. if [ $check_quality -eq 0 ]; then
  166. if [ $result -eq 0 ]; then
  167. let host_up_count++
  168. echo "up" > /var/run/mwan3track/$1/TRACK_${track_ip}
  169. if [ $score -le $up ]; then
  170. $LOG info "Check ($track_method) success for target \"$track_ip\" on interface $1 ($2)"
  171. fi
  172. else
  173. let lost++
  174. echo "down" > /var/run/mwan3track/$1/TRACK_${track_ip}
  175. if [ $score -gt $up ]; then
  176. $LOG info "Check ($track_method) failed for target \"$track_ip\" on interface $1 ($2)"
  177. fi
  178. fi
  179. else
  180. if [ "$loss" -ge "$failure_loss" -o "$latency" -ge "$failure_latency" ]; then
  181. let lost++
  182. echo "down" > /var/run/mwan3track/$1/TRACK_${track_ip}
  183. echo "$latency" > /var/run/mwan3track/$1/LATENCY_${track_ip}
  184. echo "$loss" > /var/run/mwan3track/$1/LOSS_${track_ip}
  185. if [ $score -gt $up ]; then
  186. $LOG info "Check (${track_method}: latency=${latency}ms loss=${loss}%) failed for target \"$track_ip\" on interface $1 ($2)"
  187. fi
  188. elif [ "$loss" -le "$recovery_loss" -a "$latency" -le "$recovery_latency" ]; then
  189. let host_up_count++
  190. echo "up" > /var/run/mwan3track/$1/TRACK_${track_ip}
  191. echo "$latency" > /var/run/mwan3track/$1/LATENCY_${track_ip}
  192. echo "$loss" > /var/run/mwan3track/$1/LOSS_${track_ip}
  193. if [ $score -le $up ]; then
  194. $LOG info "Check (${track_method}: latency=${latency}ms loss=${loss}%) success for target \"$track_ip\" on interface $1 ($2)"
  195. fi
  196. else
  197. echo "skipped" > /var/run/mwan3track/$1/TRACK_${track_ip}
  198. fi
  199. fi
  200. else
  201. echo "skipped" > /var/run/mwan3track/$1/TRACK_${track_ip}
  202. fi
  203. done
  204. if [ $host_up_count -lt $reliability ]; then
  205. let score--
  206. if [ $score -lt $up ]; then
  207. score=0
  208. [ ${keep_failure_interval} -eq 1 ] && {
  209. sleep_time=$failure_interval
  210. }
  211. else
  212. sleep_time=$failure_interval
  213. fi
  214. if [ $score -eq $up ]; then
  215. echo "offline" > /var/run/mwan3track/$1/STATUS
  216. env -i ACTION=ifdown INTERFACE=$1 DEVICE=$2 /sbin/hotplug-call iface
  217. score=0
  218. fi
  219. else
  220. if [ $score -lt $(($down+$up)) ] && [ $lost -gt 0 ]; then
  221. $LOG info "Lost $(($lost*$count)) ping(s) on interface $1 ($2)"
  222. fi
  223. let score++
  224. lost=0
  225. if [ $score -gt $up ]; then
  226. echo "online" > /var/run/mwan3track/$1/STATUS
  227. score=$(($down+$up))
  228. elif [ $score -le $up ]; then
  229. sleep_time=$recovery_interval
  230. fi
  231. if [ $score -eq $up ]; then
  232. $LOG notice "Interface $1 ($2) is online"
  233. echo "online" > /var/run/mwan3track/$1/STATUS
  234. env -i ACTION=ifup INTERFACE=$1 DEVICE=$2 /sbin/hotplug-call iface
  235. exit 0
  236. fi
  237. fi
  238. let turn++
  239. mkdir -p "/var/run/mwan3track/${1}"
  240. echo "${lost}" > /var/run/mwan3track/$1/LOST
  241. echo "${score}" > /var/run/mwan3track/$1/SCORE
  242. echo "${turn}" > /var/run/mwan3track/$1/TURN
  243. echo "$(get_uptime)" > /var/run/mwan3track/$1/TIME
  244. host_up_count=0
  245. sleep "${sleep_time}" &
  246. wait
  247. if [ "${IFDOWN_EVENT}" -eq 1 ]; then
  248. echo "offline" > /var/run/mwan3track/$1/STATUS
  249. echo "$(get_uptime)" > /var/run/mwan3track/$1/OFFLINE
  250. echo "0" > /var/run/mwan3track/$1/ONLINE
  251. $LOG notice "Interface $1 ($2) is offline"
  252. env -i ACTION="disconnected" INTERFACE="$1" DEVICE="$2" /sbin/hotplug-call iface
  253. score=0
  254. IFDOWN_EVENT=0
  255. fi
  256. done
  257. }
  258. main "$@"