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.

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