@ -3,60 +3,107 @@
. /lib/functions.sh
. /lib/functions.sh
. /lib/mwan3/common.sh
. /lib/mwan3/common.sh
LOG="logger -t $(basename "$0")[$$] -p"
INTERFACE=""
INTERFACE=""
DEVICE=""
DEVICE=""
PING="/bin/ping"
PING="/bin/ping"
IFDOWN_EVENT=0
IFDOWN_EVENT=0
IFUP_EVENT=0
clean_up() {
clean_up() {
$ LOG notice "Stopping mwan3track for interface \"${INTERFACE}\""
LOG notice "Stopping mwan3track for interface \"${INTERFACE}\""
exit 0
exit 0
}
}
if_down() {
if_down() {
$ LOG info "Detect ifdown event on interface ${INTERFACE} (${DEVICE})"
LOG info "Detect ifdown event on interface ${INTERFACE} (${DEVICE})"
IFDOWN_EVENT=1
IFDOWN_EVENT=1
}
}
if_up() {
LOG info "Detect ifup event on interface ${INTERFACE} (${DEVICE})"
IFUP_EVENT=1
}
validate_track_method() {
validate_track_method() {
case "$1" in
case "$1" in
ping)
ping)
command -v ping 1>/dev/null 2>&1 || {
$LOG warn "Missing ping. Please install iputils-ping package or enable ping util and recompile busybox."
[ -x "$PING" ] || {
LOG warn "Missing ping. Please enable ping util and recompile busybox."
return 1
return 1
}
}
;;
;;
arping)
arping)
command -v arping 1>/dev/null 2>&1 || {
command -v arping 1>/dev/null 2>&1 || {
$ LOG warn "Missing arping. Please install iputils-arping package."
LOG warn "Missing arping. Please install iputils-arping package."
return 1
return 1
}
}
;;
;;
httping)
httping)
command -v httping 1>/dev/null 2>&1 || {
command -v httping 1>/dev/null 2>&1 || {
$ LOG warn "Missing httping. Please install httping package."
LOG warn "Missing httping. Please install httping package."
return 1
return 1
}
}
[ -n "$2" -a "$2" != "0.0.0.0" -a "$2" != "::" ] || {
[ -n "$2" -a "$2" != "0.0.0.0" -a "$2" != "::" ] || {
$ LOG warn "Cannot determine source IP for the interface which is required by httping."
LOG warn "Cannot determine source IP for the interface which is required by httping."
return 1
return 1
}
}
;;
;;
nping-*)
nping-*)
command -v nping 1>/dev/null 2>&1 || {
command -v nping 1>/dev/null 2>&1 || {
$ LOG warn "Missing nping. Please install nping package."
LOG warn "Missing nping. Please install nping package."
return 1
return 1
}
}
;;
;;
*)
*)
$ LOG warn "Unsupported tracking method: $track_method"
LOG warn "Unsupported tracking method: $track_method"
return 2
return 2
;;
;;
esac
esac
}
}
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}
}
main() {
main() {
local reliability count timeout interval failure_interval
local reliability count timeout interval failure_interval
local recovery_interval down up size
local recovery_interval down up size
@ -70,64 +117,49 @@ main() {
DEVICE=$2
DEVICE=$2
STATUS=$3
STATUS=$3
SRC_IP=$4
SRC_IP=$4
mkdir -p /var/run/mwan3track/$1
mkdir -p /var/run/mwan3track/$INTERFACE
trap clean_up TERM
trap clean_up TERM
trap if_down USR1
trap if_down USR1
trap if_up USR2
config_load mwan3
config_load mwan3
config_get track_method $1 track_method ping
config_get_bool httping_ssl $1 httping_ssl 0
config_get track_method $INTERFACE track_method ping
config_get_bool httping_ssl $INTERFACE httping_ssl 0
validate_track_method $track_method $SRC_IP || {
validate_track_method $track_method $SRC_IP || {
track_method=ping
track_method=ping
if validate_track_method $track_method; then
if validate_track_method $track_method; then
$ LOG warn "Using ping to track interface $INTERFACE avaliability"
LOG warn "Using ping to track interface $INTERFACE avaliability"
else
else
$ LOG err "No track method avaliable"
LOG err "No track method avaliable"
exit 1
exit 1
fi
fi
}
}
config_get reliability $1 reliability 1
config_get count $1 count 1
config_get timeout $1 timeout 4
config_get interval $1 interval 10
config_get down $1 down 5
config_get up $1 up 5
config_get size $1 size 56
config_get max_ttl $1 max_ttl 60
config_get failure_interval $1 failure_interval $interval
config_get_bool keep_failure_interval $1 keep_failure_interval 0
config_get recovery_interval $1 recovery_interval $interval
config_get_bool check_quality $1 check_quality 0
config_get failure_latency $1 failure_latency 1000
config_get recovery_latency $1 recovery_latency 500
config_get failure_loss $1 failure_loss 40
config_get recovery_loss $1 recovery_loss 10
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
local score=$(($down+$up))
local score=$(($down+$up))
local track_ips=$(echo $* | cut -d ' ' -f 5-99)
local track_ips=$(echo $* | cut -d ' ' -f 5-99)
local host_up_count=0
local host_up_count=0
local lost=0
local lost=0
local sleep_time=0
local turn=0
local turn=0
local result
local ping_protocol=4
local ping_protocol=4
local ping_result
local ping_result_raw
local ping_status
local loss=0
local latency=0
local sleep_time result ping_result ping_result_raw ping_status loss latency
if [ "$STATUS" = "offline" ]; then
echo "offline" > /var/run/mwan3track/$1/STATUS
echo "0" > /var/run/mwan3track/$1/ONLINE
echo "$(get_uptime)" > /var/run/mwan3track/$1/OFFLINE
score=0
else
echo "online" > /var/run/mwan3track/$1/STATUS
echo "0" > /var/run/mwan3track/$1/OFFLINE
echo "$(get_uptime)" > /var/run/mwan3track/$1/ONLINE
env -i ACTION="connected" INTERFACE="$1" DEVICE="$2" /sbin/hotplug-call iface
fi
firstconnect
while true; do
while true; do
sleep_time=$interval
sleep_time=$interval
@ -139,16 +171,16 @@ main() {
# pinging IPv6 hosts with an interface is troublesome
# pinging IPv6 hosts with an interface is troublesome
# https://bugs.openwrt.org/index.php?do=details&task_id=2897
# https://bugs.openwrt.org/index.php?do=details&task_id=2897
# so get the IP address of the interface and use that instead
# so get the IP address of the interface and use that instead
if echo $track_ip | grep -q ':'; then
ADDR=$(ip -6 addr ls dev "$DEVICE" | sed -ne '/\/128/d' -e 's/ *inet6 \([^ \/]*\).* scope global.*/\1/p' | head -n1)
[ -z "$ADDR" ] && ADDR=$(ip -6 addr ls dev "$DEVICE" | sed -ne 's/ *inet6 \([^ \/]*\).* scope global.*/\1/p')
if [ -z ${track_ip##*:*} ]; then
ping_protocol=6
ping_protocol=6
else
unset SRC_IP
fi
fi
if [ $check_quality -eq 0 ]; then
if [ $check_quality -eq 0 ]; then
$PING -$ping_protocol -I ${ADDR :-$DEVICE} -c $count -W $timeout -s $size -t $max_ttl -q $track_ip &> /dev/null
$PING -$ping_protocol -I ${SRC_IP :-$DEVICE} -c $count -W $timeout -s $size -t $max_ttl -q $track_ip &> /dev/null
result=$?
result=$?
else
else
ping_result_raw="$($PING -$ping_protocol -I ${ADDR :-$DEVICE} -c $count -W $timeout -s $size -t $max_ttl -q $track_ip 2>/dev/null)"
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)"
ping_status=$?
ping_status=$?
ping_result=$(echo "$ping_result_raw" | tail -n2)
ping_result=$(echo "$ping_result_raw" | tail -n2)
loss="$(echo "$ping_result" | grep "packet loss" | cut -d "," -f3 | awk '{print $1}' | sed -e 's/%//')"
loss="$(echo "$ping_result" | grep "packet loss" | cut -d "," -f3 | awk '{print $1}' | sed -e 's/%//')"
@ -188,42 +220,40 @@ main() {
if [ $check_quality -eq 0 ]; then
if [ $check_quality -eq 0 ]; then
if [ $result -eq 0 ]; then
if [ $result -eq 0 ]; then
let host_up_count++
let host_up_count++
echo "up" > /var/run/mwan3track/$1/TRACK_${track_ip}
update_status "$track_ip" "up"
if [ $score -le $up ]; then
if [ $score -le $up ]; then
$ LOG info "Check ($track_method) success for target \"$track_ip\" on interface $1 ($2) "
LOG info "Check ($track_method) success for target \"$track_ip\" on interface $INTERFACE ($DEVICE). Current score: $score "
fi
fi
else
else
let lost++
let lost++
echo "down" > /var/run/mwan3track/$1/TRACK_${track_ip}
update_status "$track_ip" "down"
if [ $score -gt $up ]; then
if [ $score -gt $up ]; then
$ LOG info "Check ($track_method) failed for target \"$track_ip\" on interface $1 ($2) "
LOG info "Check ($track_method) failed for target \"$track_ip\" on interface $INTERFACE ($DEVICE). Current score: $score "
fi
fi
fi
fi
else
else
if [ "$loss" -ge "$failure_loss" -o "$latency" -ge "$failure_latency" ]; then
if [ "$loss" -ge "$failure_loss" -o "$latency" -ge "$failure_latency" ]; then
let lost++
let lost++
echo "down" > /var/run/mwan3track/$1/TRACK_${track_ip}
echo "$latency" > /var/run/mwan3track/$1/LATENCY_${track_ip}
echo "$loss" > /var/run/mwan3track/$1/LOSS_${track_ip}
update_status "$track_ip" "down" $latency $loss
if [ $score -gt $up ]; then
if [ $score -gt $up ]; then
$ LOG info "Check (${track_method}: latency=${latency}ms loss=${loss}%) failed for target \"$track_ip\" on interface $1 ($2) "
LOG info "Check (${track_method}: latency=${latency}ms loss=${loss}%) failed for target \"$track_ip\" on interface $INTERFACE ($DEVICE). Current score: $score "
fi
fi
elif [ "$loss" -le "$recovery_loss" -a "$latency" -le "$recovery_latency" ]; then
elif [ "$loss" -le "$recovery_loss" -a "$latency" -le "$recovery_latency" ]; then
let host_up_count++
let host_up_count++
echo "up" > /var/run/mwan3track/$1/TRACK_${track_ip}
echo "$latency" > /var/run/mwan3track/$1/LATENCY_${track_ip}
echo "$loss" > /var/run/mwan3track/$1/LOSS_${track_ip}
update_status "$track_ip" "up" $latency $loss
if [ $score -le $up ]; then
if [ $score -le $up ]; then
$ LOG info "Check (${track_method}: latency=${latency}ms loss=${loss}%) success for target \"$track_ip\" on interface $1 ($2) "
LOG info "Check (${track_method}: latency=${latency}ms loss=${loss}%) success for target \"$track_ip\" on interface $INTERFACE ($DEVICE). Current score: $score "
fi
fi
else
else
echo "skipped" > /var/run/mwan3track/$1 /TRACK_${track_ip}
echo "skipped" > /var/run/mwan3track/$INTERFACE /TRACK_${track_ip}
fi
fi
fi
fi
else
else
echo "skipped" > /var/run/mwan3track/$1 /TRACK_${track_ip}
echo "skipped" > /var/run/mwan3track/$INTERFACE /TRACK_${track_ip}
fi
fi
done
done
@ -240,53 +270,50 @@ main() {
fi
fi
if [ $score -eq $up ]; then
if [ $score -eq $up ]; then
echo "offline" > /var/run/mwan3track/$1/STATUS
env -i ACTION=ifdown INTERFACE=$1 DEVICE=$2 /sbin/hotplug-call iface
disconnected
score=0
score=0
fi
fi
else
else
if [ $score -lt $(($down+$up)) ] && [ $lost -gt 0 ]; then
if [ $score -lt $(($down+$up)) ] && [ $lost -gt 0 ]; then
$ LOG info "Lost $(($lost*$count)) ping(s) on interface $1 ($2) "
LOG info "Lost $(($lost*$count)) ping(s) on interface $INTERFACE ($DEVICE). Current score: $score "
fi
fi
let score++
let score++
lost=0
lost=0
if [ $score -gt $up ]; then
if [ $score -gt $up ]; then
echo "online" > /var/run/mwan3track/$1 /STATUS
echo "online" > /var/run/mwan3track/$INTERFACE /STATUS
score=$(($down+$up))
score=$(($down+$up))
elif [ $score -le $up ]; then
elif [ $score -le $up ]; then
sleep_time=$recovery_interval
sleep_time=$recovery_interval
fi
fi
if [ $score -eq $up ]; then
if [ $score -eq $up ]; then
$LOG notice "Interface $1 ($2) is online"
echo "online" > /var/run/mwan3track/$1/STATUS
env -i ACTION=ifup INTERFACE=$1 DEVICE=$2 /sbin/hotplug-call iface
exit 0
connected $INTERFACE $DEVICE
fi
fi
fi
fi
let turn++
let turn++
mkdir -p "/var/run/mwan3track/${1}"
mkdir -p "/var/run/mwan3track/${1}"
echo "${lost}" > /var/run/mwan3track/$1 /LOST
echo "${score}" > /var/run/mwan3track/$1 /SCORE
echo "${turn}" > /var/run/mwan3track/$1 /TURN
echo "$(get_uptime)" > /var/run/mwan3track/$1 /TIME
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
host_up_count=0
host_up_count=0
sleep "${sleep_time}" &
sleep "${sleep_time}" &
wait
wait
if [ "${IFDOWN_EVENT}" -eq 1 ]; then
if [ "${IFDOWN_EVENT}" -eq 1 ]; then
echo "offline" > /var/run/mwan3track/$1/STATUS
echo "$(get_uptime)" > /var/run/mwan3track/$1/OFFLINE
echo "0" > /var/run/mwan3track/$1/ONLINE
$LOG notice "Interface $1 ($2) is offline"
env -i ACTION="disconnected" INTERFACE="$1" DEVICE="$2" /sbin/hotplug-call iface
score=0
LOG debug "Register ifdown event on interface ${INTERFACE} (${DEVICE})"
disconnected 1
IFDOWN_EVENT=0
IFDOWN_EVENT=0
fi
fi
if [ "${IFUP_EVENT}" -eq 1 ]; then
LOG debug "Register ifup event on interface ${INTERFACE} (${DEVICE})"
firstconnect
IFUP_EVENT=0
fi
done
done
}
}