2014-08-08 19:09:54 +00:00
#!/bin/sh
2017-04-04 14:28:18 +00:00
. /lib/functions.sh
2017-11-09 08:57:58 +00:00
. /lib/mwan3/common.sh
2017-04-04 14:28:18 +00:00
2017-04-04 14:00:06 +00:00
INTERFACE=""
2017-04-06 13:41:05 +00:00
DEVICE=""
2020-05-20 09:34:16 +00:00
PING="/bin/ping"
2017-04-06 13:41:05 +00:00
IFDOWN_EVENT=0
2020-07-26 21:21:50 +00:00
IFUP_EVENT=0
2017-04-04 14:00:06 +00:00
clean_up() {
2020-07-26 21:21:50 +00:00
LOG notice "Stopping mwan3track for interface \"${INTERFACE}\""
2017-04-04 14:00:06 +00:00
exit 0
}
2017-04-04 13:33:37 +00:00
2017-04-06 13:41:05 +00:00
if_down() {
2020-07-26 21:21:50 +00:00
LOG info "Detect ifdown event on interface ${INTERFACE} (${DEVICE})"
2017-04-06 13:41:05 +00:00
IFDOWN_EVENT=1
}
2020-07-26 21:21:50 +00:00
if_up() {
LOG info "Detect ifup event on interface ${INTERFACE} (${DEVICE})"
IFUP_EVENT=1
}
2017-07-25 17:34:40 +00:00
validate_track_method() {
case "$1" in
ping)
2020-07-26 21:21:50 +00:00
[ -x "$PING" ] || {
LOG warn "Missing ping. Please enable ping util and recompile busybox."
2017-11-02 01:44:33 +00:00
return 1
}
2017-07-25 17:34:40 +00:00
;;
arping)
2020-08-09 23:19:25 +00:00
command -v arping 1>/dev/null 2>&1 || {
2020-07-26 21:21:50 +00:00
LOG warn "Missing arping. Please install iputils-arping package."
2017-07-25 17:34:40 +00:00
return 1
}
;;
httping)
2020-08-09 23:19:25 +00:00
command -v httping 1>/dev/null 2>&1 || {
2020-07-26 21:21:50 +00:00
LOG warn "Missing httping. Please install httping package."
2017-07-25 17:34:40 +00:00
return 1
}
2017-11-02 01:33:59 +00:00
[ -n "$2" -a "$2" != "0.0.0.0" -a "$2" != "::" ] || {
2020-07-26 21:21:50 +00:00
LOG warn "Cannot determine source IP for the interface which is required by httping."
2017-11-02 01:33:59 +00:00
return 1
}
2017-07-25 17:34:40 +00:00
;;
2018-12-04 13:47:26 +00:00
nping-*)
2020-08-09 23:19:25 +00:00
command -v nping 1>/dev/null 2>&1 || {
2020-07-26 21:21:50 +00:00
LOG warn "Missing nping. Please install nping package."
2018-12-04 13:47:26 +00:00
return 1
}
;;
2017-07-25 17:34:40 +00:00
*)
2020-07-26 21:21:50 +00:00
LOG warn "Unsupported tracking method: $track_method"
2017-07-25 17:34:40 +00:00
return 2
;;
esac
}
2020-07-26 21:21:50 +00:00
disconnected() {
echo "offline" > /var/run/mwan3track/$INTERFACE/STATUS
echo "$(get_uptime)" > /var/run/mwan3track/$INTERFACE/OFFLINE
echo "0" > /var/run/mwan3track/$INTERFACE/ONLINE
score=0
[ "$1" == 1 ] && return
LOG notice "Interface $INTERFACE ($DEVICE) is offline"
env -i ACTION="disconnected" INTERFACE="$INTERFACE" DEVICE="$DEVICE" /sbin/hotplug-call iface
}
connected() {
echo "online" > /var/run/mwan3track/$INTERFACE/STATUS
echo "0" > /var/run/mwan3track/$INTERFACE/OFFLINE
echo "$(get_uptime)" > /var/run/mwan3track/$INTERFACE/ONLINE
host_up_count=0
lost=0
turn=0
loss=0
[ "$1" == 1 ] && return
LOG notice "Interface $INTERFACE ($DEVICE) is online"
env -i ACTION="connected" INTERFACE="$INTERFACE" DEVICE="$DEVICE" /sbin/hotplug-call iface
}
firstconnect() {
if [ "$STATUS" = "offline" ]; then
disconnected 1
else
connected 1
fi
}
update_status() {
local status track_ip
track_ip=$1
status=$2
echo "$1" > /var/run/mwan3track/$INTERFACE/TRACK_${track_ip}
[ -z "$3" ] && return
echo "$3" > /var/run/mwan3track/$INTERFACE/LATENCY_${track_ip}
echo "$4" > /var/run/mwan3track/$INTERFACE/LOSS_${track_ip}
}
2017-04-04 13:01:34 +00:00
main() {
2017-04-04 14:28:18 +00:00
local reliability count timeout interval failure_interval
local recovery_interval down up size
2018-05-11 07:00:57 +00:00
local keep_failure_interval check_quality failure_latency
local recovery_latency failure_loss recovery_loss
2019-11-11 08:05:49 +00:00
local max_ttl httping_ssl
2014-08-08 19:09:54 +00:00
2017-07-24 08:59:50 +00:00
[ -z "$5" ] && echo "Error: should not be started manually" && exit 0
2014-08-08 19:09:54 +00:00
2017-04-04 14:00:06 +00:00
INTERFACE=$1
2017-04-06 13:41:05 +00:00
DEVICE=$2
2017-07-24 08:59:50 +00:00
STATUS=$3
2017-08-17 11:20:10 +00:00
SRC_IP=$4
2020-07-26 21:21:50 +00:00
mkdir -p /var/run/mwan3track/$INTERFACE
2018-01-31 10:58:38 +00:00
trap clean_up TERM
trap if_down USR1
2020-07-26 21:21:50 +00:00
trap if_up USR2
2017-04-04 13:01:34 +00:00
2017-04-04 14:28:18 +00:00
config_load mwan3
2020-07-26 21:21:50 +00:00
config_get track_method $INTERFACE track_method ping
config_get_bool httping_ssl $INTERFACE httping_ssl 0
2017-11-02 01:33:59 +00:00
validate_track_method $track_method $SRC_IP || {
2017-07-25 17:34:40 +00:00
track_method=ping
2017-11-02 01:44:33 +00:00
if validate_track_method $track_method; then
2020-07-26 21:21:50 +00:00
LOG warn "Using ping to track interface $INTERFACE avaliability"
2017-11-02 01:44:33 +00:00
else
2020-07-26 21:21:50 +00:00
LOG err "No track method avaliable"
2017-11-02 01:44:33 +00:00
exit 1
fi
2017-07-25 17:34:40 +00:00
}
2020-07-26 21:21:50 +00:00
config_get reliability $INTERFACE reliability 1
config_get count $INTERFACE count 1
config_get timeout $INTERFACE timeout 4
config_get interval $INTERFACE interval 10
config_get down $INTERFACE down 5
config_get up $INTERFACE up 5
config_get size $INTERFACE size 56
config_get max_ttl $INTERFACE max_ttl 60
config_get failure_interval $INTERFACE failure_interval $interval
config_get_bool keep_failure_interval $INTERFACE keep_failure_interval 0
config_get recovery_interval $INTERFACE recovery_interval $interval
config_get_bool check_quality $INTERFACE check_quality 0
config_get failure_latency $INTERFACE failure_latency 1000
config_get recovery_latency $INTERFACE recovery_latency 500
config_get failure_loss $INTERFACE failure_loss 40
config_get recovery_loss $INTERFACE recovery_loss 10
2017-04-04 14:28:18 +00:00
local score=$(($down+$up))
2017-07-24 08:59:50 +00:00
local track_ips=$(echo $* | cut -d ' ' -f 5-99)
2017-04-04 13:01:34 +00:00
local host_up_count=0
local lost=0
2017-04-06 13:17:17 +00:00
local turn=0
2020-05-20 09:34:16 +00:00
local ping_protocol=4
2020-07-26 21:21:50 +00:00
local sleep_time result ping_result ping_result_raw ping_status loss latency
2017-04-04 13:01:34 +00:00
2020-07-26 21:21:50 +00:00
firstconnect
2017-04-04 13:01:34 +00:00
while true; do
2017-04-04 14:28:18 +00:00
sleep_time=$interval
2017-04-04 13:01:34 +00:00
for track_ip in $track_ips; do
2017-08-25 09:42:32 +00:00
if [ $host_up_count -lt $reliability ]; then
case "$track_method" in
ping)
2020-03-13 13:58:10 +00:00
# pinging IPv6 hosts with an interface is troublesome
# https://bugs.openwrt.org/index.php?do=details&task_id=2897
2020-04-27 15:55:23 +00:00
# so get the IP address of the interface and use that instead
2020-07-26 21:21:50 +00:00
if [ -z ${track_ip##*:*} ]; then
2020-05-20 09:34:16 +00:00
ping_protocol=6
2020-07-26 21:21:50 +00:00
else
unset SRC_IP
2020-03-13 13:58:10 +00:00
fi
2018-05-11 06:30:54 +00:00
if [ $check_quality -eq 0 ]; then
2020-07-26 21:21:50 +00:00
$PING -$ping_protocol -I ${SRC_IP:-$DEVICE} -c $count -W $timeout -s $size -t $max_ttl -q $track_ip &> /dev/null
2018-05-11 06:30:54 +00:00
result=$?
else
2020-07-26 21:21:50 +00:00
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)"
2020-05-20 09:34:16 +00:00
ping_status=$?
ping_result=$(echo "$ping_result_raw" | tail -n2)
2018-05-11 06:30:54 +00:00
loss="$(echo "$ping_result" | grep "packet loss" | cut -d "," -f3 | awk '{print $1}' | sed -e 's/%//')"
2020-05-20 09:34:16 +00:00
if [ "$ping_status" -ne 0 ] || [ "$loss" -eq 100 ]; then
2018-05-11 06:30:54 +00:00
latency=999999
2020-05-20 09:34:16 +00:00
loss=100
2018-04-22 07:19:46 +00:00
else
2018-05-11 06:30:54 +00:00
latency="$(echo "$ping_result" | grep -E 'rtt|round-trip' | cut -d "=" -f2 | cut -d "/" -f2 | cut -d "." -f1)"
2018-04-22 07:19:46 +00:00
fi
2018-05-11 06:30:54 +00:00
fi
2018-04-22 07:19:46 +00:00
;;
2017-08-25 09:42:32 +00:00
arping)
2018-04-22 07:19:46 +00:00
arping -I $DEVICE -c $count -w $timeout -q $track_ip &> /dev/null
result=$?
;;
2017-08-25 09:42:32 +00:00
httping)
2019-11-11 08:05:49 +00:00
if [ "$httping_ssl" -eq 1 ]; then
httping -y $SRC_IP -c $count -t $timeout -q "https://$track_ip" &> /dev/null
else
httping -y $SRC_IP -c $count -t $timeout -q "http://$track_ip" &> /dev/null
fi
2018-04-22 07:19:46 +00:00
result=$?
;;
2018-12-04 13:47:26 +00:00
nping-tcp)
result=$(nping -e $DEVICE -c $count $track_ip --tcp | grep Lost | awk '{print $12}')
;;
nping-udp)
result=$(nping -e $DEVICE -c $count $track_ip --udp | grep Lost | awk '{print $12}')
;;
nping-icmp)
result=$(nping -e $DEVICE -c $count $track_ip --icmp | grep Lost | awk '{print $12}')
;;
nping-arp)
result=$(nping -e $DEVICE -c $count $track_ip --arp | grep Lost | awk '{print $12}')
;;
2017-08-25 09:42:32 +00:00
esac
2018-04-22 07:19:46 +00:00
if [ $check_quality -eq 0 ]; then
if [ $result -eq 0 ]; then
let host_up_count++
2020-07-26 21:21:50 +00:00
update_status "$track_ip" "up"
2018-04-22 07:19:46 +00:00
if [ $score -le $up ]; then
2020-07-26 21:21:50 +00:00
LOG info "Check ($track_method) success for target \"$track_ip\" on interface $INTERFACE ($DEVICE). Current score: $score"
2018-04-22 07:19:46 +00:00
fi
else
let lost++
2020-07-26 21:21:50 +00:00
update_status "$track_ip" "down"
2018-04-22 07:19:46 +00:00
if [ $score -gt $up ]; then
2020-07-26 21:21:50 +00:00
LOG info "Check ($track_method) failed for target \"$track_ip\" on interface $INTERFACE ($DEVICE). Current score: $score"
2018-04-22 07:19:46 +00:00
fi
2018-03-21 13:13:57 +00:00
fi
2017-08-25 09:42:32 +00:00
else
2018-04-22 07:19:46 +00:00
if [ "$loss" -ge "$failure_loss" -o "$latency" -ge "$failure_latency" ]; then
let lost++
2020-07-26 21:21:50 +00:00
update_status "$track_ip" "down" $latency $loss
2018-04-22 07:19:46 +00:00
if [ $score -gt $up ]; then
2020-07-26 21:21:50 +00:00
LOG info "Check (${track_method}: latency=${latency}ms loss=${loss}%) failed for target \"$track_ip\" on interface $INTERFACE ($DEVICE). Current score: $score"
2018-04-22 07:19:46 +00:00
fi
elif [ "$loss" -le "$recovery_loss" -a "$latency" -le "$recovery_latency" ]; then
let host_up_count++
2020-07-26 21:21:50 +00:00
update_status "$track_ip" "up" $latency $loss
2018-04-22 07:19:46 +00:00
if [ $score -le $up ]; then
2020-07-26 21:21:50 +00:00
LOG info "Check (${track_method}: latency=${latency}ms loss=${loss}%) success for target \"$track_ip\" on interface $INTERFACE ($DEVICE). Current score: $score"
2018-04-22 07:19:46 +00:00
fi
else
2020-07-26 21:21:50 +00:00
echo "skipped" > /var/run/mwan3track/$INTERFACE/TRACK_${track_ip}
2018-03-21 13:13:57 +00:00
fi
2017-08-25 09:42:32 +00:00
fi
2017-04-04 13:01:34 +00:00
else
2020-07-26 21:21:50 +00:00
echo "skipped" > /var/run/mwan3track/$INTERFACE/TRACK_${track_ip}
2017-04-04 13:01:34 +00:00
fi
done
2017-04-04 14:28:18 +00:00
if [ $host_up_count -lt $reliability ]; then
2017-04-04 13:01:34 +00:00
let score--
2017-04-04 14:28:18 +00:00
if [ $score -lt $up ]; then
2017-04-04 13:01:34 +00:00
score=0
2017-07-20 06:55:55 +00:00
[ ${keep_failure_interval} -eq 1 ] && {
sleep_time=$failure_interval
}
2017-04-04 13:01:34 +00:00
else
2017-04-04 14:28:18 +00:00
sleep_time=$failure_interval
2017-04-04 13:01:34 +00:00
fi
2017-04-04 14:28:18 +00:00
if [ $score -eq $up ]; then
2020-07-26 21:21:50 +00:00
disconnected
2017-04-04 13:01:34 +00:00
score=0
fi
2017-03-24 14:21:23 +00:00
else
2017-04-04 14:28:18 +00:00
if [ $score -lt $(($down+$up)) ] && [ $lost -gt 0 ]; then
2020-07-26 21:21:50 +00:00
LOG info "Lost $(($lost*$count)) ping(s) on interface $INTERFACE ($DEVICE). Current score: $score"
2017-04-04 13:01:34 +00:00
fi
let score++
lost=0
2017-04-04 14:28:18 +00:00
if [ $score -gt $up ]; then
2020-07-26 21:21:50 +00:00
echo "online" > /var/run/mwan3track/$INTERFACE/STATUS
2017-04-04 14:28:18 +00:00
score=$(($down+$up))
elif [ $score -le $up ]; then
sleep_time=$recovery_interval
2017-04-04 13:01:34 +00:00
fi
2017-04-04 14:28:18 +00:00
if [ $score -eq $up ]; then
2020-07-26 21:21:50 +00:00
connected $INTERFACE $DEVICE
2017-04-04 13:01:34 +00:00
fi
2014-08-08 19:09:54 +00:00
fi
2017-04-06 13:17:17 +00:00
let turn++
2017-05-12 13:30:14 +00:00
mkdir -p "/var/run/mwan3track/${1}"
2020-07-26 21:21:50 +00:00
echo "${lost}" > /var/run/mwan3track/$INTERFACE/LOST
echo "${score}" > /var/run/mwan3track/$INTERFACE/SCORE
echo "${turn}" > /var/run/mwan3track/$INTERFACE/TURN
echo "$(get_uptime)" > /var/run/mwan3track/$INTERFACE/TIME
2017-04-06 13:17:17 +00:00
2017-04-04 13:01:34 +00:00
host_up_count=0
2017-04-06 14:36:46 +00:00
sleep "${sleep_time}" &
wait
2017-04-06 13:41:05 +00:00
if [ "${IFDOWN_EVENT}" -eq 1 ]; then
2020-07-26 21:21:50 +00:00
LOG debug "Register ifdown event on interface ${INTERFACE} (${DEVICE})"
disconnected 1
2017-04-06 13:41:05 +00:00
IFDOWN_EVENT=0
fi
2020-07-26 21:21:50 +00:00
if [ "${IFUP_EVENT}" -eq 1 ]; then
LOG debug "Register ifup event on interface ${INTERFACE} (${DEVICE})"
firstconnect
IFUP_EVENT=0
fi
2017-04-04 13:01:34 +00:00
done
}
2014-08-08 19:09:54 +00:00
2017-04-04 13:01:34 +00:00
main "$@"