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.

320 lines
9.1 KiB

  1. #!/bin/sh
  2. . /lib/functions.sh
  3. . /lib/mwan3/common.sh
  4. INTERFACE=""
  5. DEVICE=""
  6. PING="/bin/ping"
  7. IFDOWN_EVENT=0
  8. IFUP_EVENT=0
  9. clean_up() {
  10. LOG notice "Stopping mwan3track for interface \"${INTERFACE}\""
  11. exit 0
  12. }
  13. if_down() {
  14. LOG info "Detect ifdown event on interface ${INTERFACE} (${DEVICE})"
  15. IFDOWN_EVENT=1
  16. }
  17. if_up() {
  18. LOG info "Detect ifup event on interface ${INTERFACE} (${DEVICE})"
  19. IFUP_EVENT=1
  20. }
  21. validate_track_method() {
  22. case "$1" in
  23. ping)
  24. [ -x "$PING" ] || {
  25. LOG warn "Missing ping. Please enable ping util and recompile busybox."
  26. return 1
  27. }
  28. ;;
  29. arping)
  30. command -v arping 1>/dev/null 2>&1 || {
  31. LOG warn "Missing arping. Please install iputils-arping package."
  32. return 1
  33. }
  34. ;;
  35. httping)
  36. command -v httping 1>/dev/null 2>&1 || {
  37. LOG warn "Missing httping. Please install httping package."
  38. return 1
  39. }
  40. [ -n "$2" -a "$2" != "0.0.0.0" -a "$2" != "::" ] || {
  41. LOG warn "Cannot determine source IP for the interface which is required by httping."
  42. return 1
  43. }
  44. ;;
  45. nping-*)
  46. command -v nping 1>/dev/null 2>&1 || {
  47. LOG warn "Missing nping. Please install nping package."
  48. return 1
  49. }
  50. ;;
  51. *)
  52. LOG warn "Unsupported tracking method: $track_method"
  53. return 2
  54. ;;
  55. esac
  56. }
  57. disconnected() {
  58. echo "offline" > /var/run/mwan3track/$INTERFACE/STATUS
  59. echo "$(get_uptime)" > /var/run/mwan3track/$INTERFACE/OFFLINE
  60. echo "0" > /var/run/mwan3track/$INTERFACE/ONLINE
  61. score=0
  62. [ "$1" == 1 ] && return
  63. LOG notice "Interface $INTERFACE ($DEVICE) is offline"
  64. env -i ACTION="disconnected" INTERFACE="$INTERFACE" DEVICE="$DEVICE" /sbin/hotplug-call iface
  65. }
  66. connected() {
  67. echo "online" > /var/run/mwan3track/$INTERFACE/STATUS
  68. echo "0" > /var/run/mwan3track/$INTERFACE/OFFLINE
  69. echo "$(get_uptime)" > /var/run/mwan3track/$INTERFACE/ONLINE
  70. host_up_count=0
  71. lost=0
  72. turn=0
  73. loss=0
  74. [ "$1" == 1 ] && return
  75. LOG notice "Interface $INTERFACE ($DEVICE) is online"
  76. env -i ACTION="connected" INTERFACE="$INTERFACE" DEVICE="$DEVICE" /sbin/hotplug-call iface
  77. }
  78. firstconnect() {
  79. if [ "$STATUS" = "offline" ]; then
  80. disconnected 1
  81. else
  82. connected 1
  83. fi
  84. }
  85. update_status() {
  86. local status track_ip
  87. track_ip=$1
  88. status=$2
  89. echo "$1" > /var/run/mwan3track/$INTERFACE/TRACK_${track_ip}
  90. [ -z "$3" ] && return
  91. echo "$3" > /var/run/mwan3track/$INTERFACE/LATENCY_${track_ip}
  92. echo "$4" > /var/run/mwan3track/$INTERFACE/LOSS_${track_ip}
  93. }
  94. main() {
  95. local reliability count timeout interval failure_interval
  96. local recovery_interval down up size
  97. local keep_failure_interval check_quality failure_latency
  98. local recovery_latency failure_loss recovery_loss
  99. local max_ttl httping_ssl
  100. [ -z "$5" ] && echo "Error: should not be started manually" && exit 0
  101. INTERFACE=$1
  102. DEVICE=$2
  103. STATUS=$3
  104. SRC_IP=$4
  105. mkdir -p /var/run/mwan3track/$INTERFACE
  106. trap clean_up TERM
  107. trap if_down USR1
  108. trap if_up USR2
  109. config_load mwan3
  110. config_get track_method $INTERFACE track_method ping
  111. config_get_bool httping_ssl $INTERFACE httping_ssl 0
  112. validate_track_method $track_method $SRC_IP || {
  113. track_method=ping
  114. if validate_track_method $track_method; then
  115. LOG warn "Using ping to track interface $INTERFACE avaliability"
  116. else
  117. LOG err "No track method avaliable"
  118. exit 1
  119. fi
  120. }
  121. config_get reliability $INTERFACE reliability 1
  122. config_get count $INTERFACE count 1
  123. config_get timeout $INTERFACE timeout 4
  124. config_get interval $INTERFACE interval 10
  125. config_get down $INTERFACE down 5
  126. config_get up $INTERFACE up 5
  127. config_get size $INTERFACE size 56
  128. config_get max_ttl $INTERFACE max_ttl 60
  129. config_get failure_interval $INTERFACE failure_interval $interval
  130. config_get_bool keep_failure_interval $INTERFACE keep_failure_interval 0
  131. config_get recovery_interval $INTERFACE recovery_interval $interval
  132. config_get_bool check_quality $INTERFACE check_quality 0
  133. config_get failure_latency $INTERFACE failure_latency 1000
  134. config_get recovery_latency $INTERFACE recovery_latency 500
  135. config_get failure_loss $INTERFACE failure_loss 40
  136. config_get recovery_loss $INTERFACE recovery_loss 10
  137. local score=$(($down+$up))
  138. local track_ips=$(echo $* | cut -d ' ' -f 5-99)
  139. local host_up_count=0
  140. local lost=0
  141. local turn=0
  142. local ping_protocol=4
  143. local sleep_time result ping_result ping_result_raw ping_status loss latency
  144. firstconnect
  145. while true; do
  146. sleep_time=$interval
  147. for track_ip in $track_ips; do
  148. if [ $host_up_count -lt $reliability ]; then
  149. case "$track_method" in
  150. ping)
  151. # pinging IPv6 hosts with an interface is troublesome
  152. # https://bugs.openwrt.org/index.php?do=details&task_id=2897
  153. # so get the IP address of the interface and use that instead
  154. if [ -z ${track_ip##*:*} ]; then
  155. ping_protocol=6
  156. else
  157. unset SRC_IP
  158. fi
  159. if [ $check_quality -eq 0 ]; then
  160. $PING -$ping_protocol -I ${SRC_IP:-$DEVICE} -c $count -W $timeout -s $size -t $max_ttl -q $track_ip &> /dev/null
  161. result=$?
  162. else
  163. ping_result_raw="$($PING -$ping_protocol -I ${SRC_IP:-$DEVICE} -c $count -W $timeout -s $size -t $max_ttl -q $track_ip 2>/dev/null)"
  164. ping_status=$?
  165. ping_result=$(echo "$ping_result_raw" | tail -n2)
  166. loss="$(echo "$ping_result" | grep "packet loss" | cut -d "," -f3 | awk '{print $1}' | sed -e 's/%//')"
  167. if [ "$ping_status" -ne 0 ] || [ "$loss" -eq 100 ]; then
  168. latency=999999
  169. loss=100
  170. else
  171. latency="$(echo "$ping_result" | grep -E 'rtt|round-trip' | cut -d "=" -f2 | cut -d "/" -f2 | cut -d "." -f1)"
  172. fi
  173. fi
  174. ;;
  175. arping)
  176. arping -I $DEVICE -c $count -w $timeout -q $track_ip &> /dev/null
  177. result=$?
  178. ;;
  179. httping)
  180. if [ "$httping_ssl" -eq 1 ]; then
  181. httping -y $SRC_IP -c $count -t $timeout -q "https://$track_ip" &> /dev/null
  182. else
  183. httping -y $SRC_IP -c $count -t $timeout -q "http://$track_ip" &> /dev/null
  184. fi
  185. result=$?
  186. ;;
  187. nping-tcp)
  188. result=$(nping -e $DEVICE -c $count $track_ip --tcp | grep Lost | awk '{print $12}')
  189. ;;
  190. nping-udp)
  191. result=$(nping -e $DEVICE -c $count $track_ip --udp | grep Lost | awk '{print $12}')
  192. ;;
  193. nping-icmp)
  194. result=$(nping -e $DEVICE -c $count $track_ip --icmp | grep Lost | awk '{print $12}')
  195. ;;
  196. nping-arp)
  197. result=$(nping -e $DEVICE -c $count $track_ip --arp | grep Lost | awk '{print $12}')
  198. ;;
  199. esac
  200. if [ $check_quality -eq 0 ]; then
  201. if [ $result -eq 0 ]; then
  202. let host_up_count++
  203. update_status "$track_ip" "up"
  204. if [ $score -le $up ]; then
  205. LOG info "Check ($track_method) success for target \"$track_ip\" on interface $INTERFACE ($DEVICE). Current score: $score"
  206. fi
  207. else
  208. let lost++
  209. update_status "$track_ip" "down"
  210. if [ $score -gt $up ]; then
  211. LOG info "Check ($track_method) failed for target \"$track_ip\" on interface $INTERFACE ($DEVICE). Current score: $score"
  212. fi
  213. fi
  214. else
  215. if [ "$loss" -ge "$failure_loss" -o "$latency" -ge "$failure_latency" ]; then
  216. let lost++
  217. update_status "$track_ip" "down" $latency $loss
  218. if [ $score -gt $up ]; then
  219. LOG info "Check (${track_method}: latency=${latency}ms loss=${loss}%) failed for target \"$track_ip\" on interface $INTERFACE ($DEVICE). Current score: $score"
  220. fi
  221. elif [ "$loss" -le "$recovery_loss" -a "$latency" -le "$recovery_latency" ]; then
  222. let host_up_count++
  223. update_status "$track_ip" "up" $latency $loss
  224. if [ $score -le $up ]; then
  225. LOG info "Check (${track_method}: latency=${latency}ms loss=${loss}%) success for target \"$track_ip\" on interface $INTERFACE ($DEVICE). Current score: $score"
  226. fi
  227. else
  228. echo "skipped" > /var/run/mwan3track/$INTERFACE/TRACK_${track_ip}
  229. fi
  230. fi
  231. else
  232. echo "skipped" > /var/run/mwan3track/$INTERFACE/TRACK_${track_ip}
  233. fi
  234. done
  235. if [ $host_up_count -lt $reliability ]; then
  236. let score--
  237. if [ $score -lt $up ]; then
  238. score=0
  239. [ ${keep_failure_interval} -eq 1 ] && {
  240. sleep_time=$failure_interval
  241. }
  242. else
  243. sleep_time=$failure_interval
  244. fi
  245. if [ $score -eq $up ]; then
  246. disconnected
  247. score=0
  248. fi
  249. else
  250. if [ $score -lt $(($down+$up)) ] && [ $lost -gt 0 ]; then
  251. LOG info "Lost $(($lost*$count)) ping(s) on interface $INTERFACE ($DEVICE). Current score: $score"
  252. fi
  253. let score++
  254. lost=0
  255. if [ $score -gt $up ]; then
  256. echo "online" > /var/run/mwan3track/$INTERFACE/STATUS
  257. score=$(($down+$up))
  258. elif [ $score -le $up ]; then
  259. sleep_time=$recovery_interval
  260. fi
  261. if [ $score -eq $up ]; then
  262. connected $INTERFACE $DEVICE
  263. fi
  264. fi
  265. let turn++
  266. mkdir -p "/var/run/mwan3track/${1}"
  267. echo "${lost}" > /var/run/mwan3track/$INTERFACE/LOST
  268. echo "${score}" > /var/run/mwan3track/$INTERFACE/SCORE
  269. echo "${turn}" > /var/run/mwan3track/$INTERFACE/TURN
  270. echo "$(get_uptime)" > /var/run/mwan3track/$INTERFACE/TIME
  271. host_up_count=0
  272. sleep "${sleep_time}" &
  273. wait
  274. if [ "${IFDOWN_EVENT}" -eq 1 ]; then
  275. LOG debug "Register ifdown event on interface ${INTERFACE} (${DEVICE})"
  276. disconnected 1
  277. IFDOWN_EVENT=0
  278. fi
  279. if [ "${IFUP_EVENT}" -eq 1 ]; then
  280. LOG debug "Register ifup event on interface ${INTERFACE} (${DEVICE})"
  281. firstconnect
  282. IFUP_EVENT=0
  283. fi
  284. done
  285. }
  286. main "$@"