packages/net/mwan3/files/usr/sbin/mwan3track
Marcin Jurkowski de4fc2b5de mwan3: support various interface tracking methods
Adds support for interface tracking using either ping, arping or
httping. This allows to track interface status on networks with filtered
ICMP traffic or simply to monitor data link layer etc.

To facilitate binding to a specified interface its IP address is passed
as a new mwan3track parameter. It's currently required by httping
and possibly by other tools that may be added in the future.

Signed-off-by: Marcin Jurkowski <marcin1j@gmail.com>
2017-08-02 17:44:07 +02:00

165 lines
3.8 KiB
Bash
Executable file

#!/bin/sh
. /lib/functions.sh
LOG="logger -t $(basename "$0")[$$] -p"
INTERFACE=""
DEVICE=""
IFDOWN_EVENT=0
clean_up() {
$LOG notice "Stopping mwan3track for interface \"${INTERFACE}\""
if [ "$(pgrep -f "mwan3track ${INTERFACE}")" = "" ]; then
rm -rf "/var/run/mwan3track/${INTERFACE}" &> /dev/null
fi
if [ -z "$(ls -A "/var/run/mwan3track")" ]; then
rm -rf "/var/run/mwan3track"
fi
exit 0
}
if_down() {
$LOG info "Detect ifdown event on interface ${INTERFACE} (${DEVICE})"
IFDOWN_EVENT=1
}
validate_track_method() {
case "$1" in
ping)
# Assume that ping is installed
;;
arping)
which arping 1>/dev/null 2>&1 || {
$LOG warn "Missing arping. Please install iputils-arping package."
return 1
}
;;
httping)
which httping 1>/dev/null 2>&1 || {
$LOG warn "Missing httping. Please install httping package."
return 1
}
;;
*)
$LOG warn "Unsupported tracking method: $track_method"
return 2
;;
esac
}
main() {
local reliability count timeout interval failure_interval
local recovery_interval down up size
[ -z "$3" ] && echo "Error: should not be started manually" && exit 0
INTERFACE=$1
DEVICE=$2
mkdir -p /var/run/mwan3track/$1
trap clean_up SIGINT SIGTERM
trap if_down SIGUSR1
config_load mwan3
config_get track_method $1 track_method ping
validate_track_method $track_method || {
$LOG warn "Using ping to track interface $INTERFACE avaliability"
track_method=ping
}
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 failure_interval $1 failure_interval $interval
config_get recovery_interval $1 recovery_interval $interval
local score=$(($down+$up))
local track_ips=$(echo $* | cut -d ' ' -f 4-99)
local host_up_count=0
local lost=0
local sleep_time=0
local turn=0
echo "offline" > /var/run/mwan3track/$1/STATUS
while true; do
sleep_time=$interval
for track_ip in $track_ips; do
case "$track_method" in
ping)
ping -I $2 -c $count -W $timeout -s $size -q $track_ip &> /dev/null ;;
arping)
arping -I $2 -c $count -w $timeout -q $track_ip &> /dev/null ;;
httping)
httping -y $3 -c $count -t $timeout -q $track_ip &> /dev/null ;;
esac
if [ $? -eq 0 ]; then
let host_up_count++
echo "up" > /var/run/mwan3track/$1/TRACK_${track_ip}
else
let lost++
echo "down" > /var/run/mwan3track/$1/TRACK_${track_ip}
fi
done
if [ $host_up_count -lt $reliability ]; then
let score--
if [ $score -lt $up ]; then
score=0
else
sleep_time=$failure_interval
fi
if [ $score -eq $up ]; then
echo "offline" > /var/run/mwan3track/$1/STATUS
$LOG notice "Interface $1 ($2) is offline"
env -i ACTION=ifdown INTERFACE=$1 DEVICE=$2 /sbin/hotplug-call iface
score=0
fi
else
if [ $score -lt $(($down+$up)) ] && [ $lost -gt 0 ]; then
$LOG info "Lost $(($lost*$count)) ping(s) on interface $1 ($2)"
fi
let score++
lost=0
if [ $score -gt $up ]; then
echo "online" > /var/run/mwan3track/$1/STATUS
score=$(($down+$up))
elif [ $score -le $up ]; then
sleep_time=$recovery_interval
fi
if [ $score -eq $up ]; then
$LOG notice "Interface $1 ($2) is online"
env -i ACTION=ifup INTERFACE=$1 DEVICE=$2 /sbin/hotplug-call iface
rm -rf "/var/run/mwan3track/${1}" &> /dev/null
exit 0
fi
fi
let turn++
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
host_up_count=0
sleep "${sleep_time}" &
wait
if [ "${IFDOWN_EVENT}" -eq 1 ]; then
score=0
echo "offline" > /var/run/mwan3track/$1/STATUS
IFDOWN_EVENT=0
fi
done
}
main "$@"