Is there a way to set the ping time out for the fail over detection. I would like to set the ping limit to 250ms, any ping taking longer than 250ms would be considered failed.
f
Is there a way to set the ping time out for the fail over detection. I would like to set the ping limit to 250ms, any ping taking longer than 250ms would be considered failed.
f
Hello grizzlystar,
Thank you for your question.
Unfortunately, there is no built-in option to define what should be considered a “successful” ping. However, you might achieve more control by creating a custom failover script. This script should be placed in the following file: /etc/mwan3.user
This file is specifically designed for custom rules and scripts in MWAN3 (the multi-WAN manager used in OpenWrt). You can write your own logic here to define custom ping thresholds, destinations, or success/failure conditions.
For more details and examples, please visit the MWAN3 documentation:
I hope this helps! If you need additional assistance, please don’t hesitate to reach out.
Best regards,
V.
I wrote a script. I’m getting a error when I restart mwan3, it says
cat: can’t open ‘/var/run/mwan3/active_wan’: No such file or directory
Cannot find device “”
Error: any valid prefix is expected rather than “”.
cat: can’t open ‘/tmp/run/mwan3/active_wan’: No such file or directory
cat: can’t open ‘/tmp/run/mwan3/active_wan’: No such file or directory
cat: can’t open ‘/tmp/run/mwan3/active_wan’: No such file or directory
cat: can’t open ‘/tmp/run/mwan3/active_wan’: No such file or directory
cat: can’t open ‘/tmp/run/mwan3/active_wan’: No such file or directory
cat: can’t open ‘/tmp/run/mwan3/active_wan’: No such file or directory
I know /tmp/run/mwan3/active_wan exits and has the correct permissions.
##########################################################
This it what I did
uci set mwan3.wan.track_method=‘exec’
uci set mwan3.wan.track_exec=‘/usr/local/bin/mwan3_latency_check.sh wan 8.8.8.8 100’
uci set mwan3.wan.interval=‘5’
uci set mwan3.wan.down=‘3’
uci set mwan3.wan.up=‘3’
commit mwan3/etc/init.d/mwan3 stop
/etc/init.d/mwan3 start
Here is the script to check latency ( default mwan can only check latency in seconds )
the script works. You can test it with
/usr/local/bin/mwan3_latency_check.sh wan 8.8.8.8 200
echo "Exit code: $?" # Should be 0 for success
/usr/local/bin/mwan3_latency_check.sh wan 8.8.8.8 10
echo "Exit code: $?" # Should be 0 for success
/usr/local/bin/mwan3_latency_check.sh wan 8.8.8.8 1
echo "Exit code: $?" # Should be 0 for success
#!/bin/sh
# MWAN3 Custom Latency Check Script
# Marks interface as down if ping latency exceeds threshold
# Usage: /usr/local/bin/mwan3_latency_check.sh <interface> [ping_host] [max_latency_ms]
# Or use environment variable: MAX_LATENCY=150 /usr/local/bin/mwan3_latency_check.sh wan 8.8.8.8
# Configuration - can be overridden by environment variables or arguments
INTERFACE="$1"
PING_HOST="${2:-8.8.8.8}" # Default to Google DNS if not specified
# Latency threshold - priority order:
# 1. Command line argument (3rd parameter)
# 2. Environment variable MAX_LATENCY
# 3. Default value of 200ms
if [ -n "$3" ]; then
MAX_LATENCY="$3"
elif [ -n "$MAX_LATENCY" ]; then
MAX_LATENCY="$MAX_LATENCY"
else
MAX_LATENCY=200 # Default threshold in milliseconds
fi
# Other configurable parameters (can be set via environment variables)
PING_COUNT="${PING_COUNT:-3}" # Number of pings to average
PING_TIMEOUT="${PING_TIMEOUT:-1}" # Timeout for each ping in seconds
PING_WAIT="${PING_WAIT:-0}" # Wait time between pings (0=no wait, 1=1 second)
CHECK_METHOD="${CHECK_METHOD:-max}" # Check method: 'max' or 'avg'
# Validate input
if [ -z "$INTERFACE" ]; then
cat << EOF
Error: Interface name required
Usage: $0 <interface> [ping_host] [max_latency_ms]
Examples:
$0 wan # Use defaults: 8.8.8.8, 200ms
$0 wan 1.1.1.1 # Custom host, default 200ms
$0 wan 8.8.8.8 150 # Custom host and 150ms threshold
MAX_LATENCY=100 $0 wan # Set threshold via environment variable
Environment variables:
MAX_LATENCY - Maximum acceptable latency in ms (default: 200)
PING_COUNT - Number of pings to perform (default: 3)
PING_TIMEOUT - Timeout for each ping in seconds (default: 1)
PING_WAIT - Wait time between pings in seconds (default: 0)
CHECK_METHOD - 'max' or 'avg' for latency checking (default: max)
EOF
exit 2
fi
# Validate MAX_LATENCY is a number
if ! echo "$MAX_LATENCY" | grep -q '^[0-9]\+$'; then
echo "Error: MAX_LATENCY must be a positive number (got: $MAX_LATENCY)"
exit 2
fi
# Function to get device name from interface
get_device() {
# Try to get the actual device name for the interface
# This handles both direct interfaces (eth0, wlan0) and logical interfaces (wan, wan2)
local dev=""
# First try to get from UCI config
if [ -f /etc/config/network ]; then
dev=$(uci -q get network.${INTERFACE}.ifname)
[ -z "$dev" ] && dev=$(uci -q get network.${INTERFACE}.device)
fi
# If not found in UCI, assume interface name is the device name
[ -z "$dev" ] && dev="$INTERFACE"
echo "$dev"
}
# Function to perform latency check
check_latency() {
local device="$1"
local host="$2"
local total_latency=0
local successful_pings=0
local max_seen=0
local latency_values=""
logger -t mwan3_latency "[$INTERFACE] Starting check: host=$host, threshold=${MAX_LATENCY}ms, method=$CHECK_METHOD"
# Perform multiple pings
for i in $(seq 1 $PING_COUNT); do
# Execute ping and extract latency
result=$(ping -c 1 -W $PING_TIMEOUT -I "$device" "$host" 2>/dev/null)
if [ $? -eq 0 ]; then
# Extract latency value (in ms)
latency=$(echo "$result" | grep 'time=' | sed -n 's/.*time=\([0-9.]*\).*/\1/p')
if [ -n "$latency" ]; then
# Convert to integer for comparison (remove decimal part)
latency_int=$(echo "$latency" | cut -d'.' -f1)
# Track maximum latency seen
[ "$latency_int" -gt "$max_seen" ] && max_seen=$latency_int
# Add to total and track values
total_latency=$((total_latency + latency_int))
successful_pings=$((successful_pings + 1))
latency_values="${latency_values}${latency}ms "
# Log individual ping result (debug level)
[ "$LOG_LEVEL" = "debug" ] && logger -t mwan3_latency "[$INTERFACE] Ping $i to $host: ${latency}ms"
fi
else
[ "$LOG_LEVEL" = "debug" ] && logger -t mwan3_latency "[$INTERFACE] Ping $i to $host: failed"
fi
# Wait between pings if configured (except for last one)
if [ "$i" -lt "$PING_COUNT" ] && [ "$PING_WAIT" -gt 0 ]; then
sleep $PING_WAIT
fi
done
# Calculate results
if [ "$successful_pings" -eq 0 ]; then
# All pings failed - interface is down
logger -t mwan3_latency "[$INTERFACE] All pings failed to $host - marking interface DOWN"
return 1
fi
# Calculate average latency
avg_latency=$((total_latency / successful_pings))
# Determine which metric to use for threshold checking
if [ "$CHECK_METHOD" = "avg" ]; then
check_value=$avg_latency
check_label="avg"
else
check_value=$max_seen
check_label="max"
fi
# Log summary
logger -t mwan3_latency "[$INTERFACE] Results: ${latency_values}| avg=${avg_latency}ms, max=${max_seen}ms, checking=${check_label}"
# Check against threshold
if [ "$check_value" -gt "$MAX_LATENCY" ]; then
logger -t mwan3_latency "[$INTERFACE] HIGH LATENCY: ${check_label}=${check_value}ms > threshold=${MAX_LATENCY}ms - marking DOWN"
return 1
else
logger -t mwan3_latency "[$INTERFACE] Latency OK: ${check_label}=${check_value}ms <= threshold=${MAX_LATENCY}ms - interface UP"
return 0
fi
}
# Function for multi-host checking (optional, more robust)
check_multiple_hosts() {
local device="$1"
local hosts="${MULTI_HOSTS:-8.8.8.8 1.1.1.1 208.67.222.222}" # Can override via environment
local failures=0
local required_success="${REQUIRED_SUCCESS:-1}" # At least 1 host must pass
local success=0
logger -t mwan3_latency "[$INTERFACE] Multi-host check starting (threshold=${MAX_LATENCY}ms)"
for host in $hosts; do
if check_latency "$device" "$host"; then
success=$((success + 1))
# If we have enough successful hosts, we can return early
if [ "$success" -ge "$required_success" ]; then
logger -t mwan3_latency "[$INTERFACE] Multi-host check PASSED ($success hosts OK)"
return 0
fi
else
failures=$((failures + 1))
fi
done
# Not enough hosts passed
logger -t mwan3_latency "[$INTERFACE] Multi-host check FAILED (only $success hosts OK, needed $required_success)"
return 1
}
# Function to show current configuration (for debugging)
show_config() {
cat << EOF
MWAN3 Latency Check Configuration:
Interface: $INTERFACE
Device: $(get_device)
Target Host: $PING_HOST
Max Latency: ${MAX_LATENCY}ms
Ping Count: $PING_COUNT
Ping Timeout: ${PING_TIMEOUT}s
Ping Wait: ${PING_WAIT}s
Check Method: $CHECK_METHOD
EOF
}
# Main execution
main() {
# Show configuration in debug mode
if [ "$DEBUG" = "1" ] || [ "$LOG_LEVEL" = "debug" ]; then
show_config | logger -t mwan3_latency
fi
# Get the actual device name
DEVICE=$(get_device)
logger -t mwan3_latency "[$INTERFACE] Starting latency check (device: $DEVICE, host: $PING_HOST, threshold: ${MAX_LATENCY}ms)"
# Perform the latency check
if [ "$PING_HOST" = "multi" ]; then
# Use multiple hosts for redundancy
check_multiple_hosts "$DEVICE"
else
# Check single specified host
check_latency "$DEVICE" "$PING_HOST"
fi
# Return appropriate exit code for MWAN3
# Exit 0 = interface up, Exit 1 = interface down
exit $?
}
# Handle special commands
case "$1" in
--help|-h)
$0
exit 0
;;
--config)
INTERFACE="${2:-wan}"
show_config
exit 0
;;
*)
# Normal operation
main
;;
esac
Hello,
Custom scripting falls outside of our standard technical support scope, as we do not directly provide programming-related assistance. That said, I’ll do my best to guide you.
The mwan3.user file is a special script used by the Multi-WAN Manager (MWAN3) in RUTOS. Its purpose is to define custom actions that automatically run whenever a WAN interface changes state.
For example, the script is triggered when:
When such events occur, MWAN3 passes details such as the interface name and event type to the mwan3.user script. You can then insert your own commands — for instance:
By default, this file only contains a template, but it can be freely customized to meet your needs. It’s a flexible tool for automating actions in failover or load balancing scenarios.
Additionally, you may submit your request through the Contact Us form at teltonika-networks.com → Contact Us. This will allow us to involve our R&D department to assess the feasibility of further development. Please note that such requests are evaluated as part of our custom development services, which may be subject to additional costs.
Best regards,
V.
This topic was automatically closed after 60 days. New replies are no longer allowed.