usb_direct: fix netmon script when bonding is enabled

This commit is contained in:
Alfonso Gamboa 2023-07-19 13:05:33 -07:00
parent 86b4f38429
commit ac34e561cc
3 changed files with 89 additions and 35 deletions

View File

@ -57,28 +57,3 @@ wait_for_icamera() {
sleep 5
done
}
gateway_supervisor() {
last=0
last2=0
while [[ 1 ]]; do
ping -c1 -W 2 `/opt/wz_mini/bin/busybox ip route | awk '/default/ { print $3 }'` >& /dev/null
status=$?
if [[ $status == 1 ]]; then
echo "$1: $(date) Unable to reach default route via USB Direct Link..."
if [[ $last == 1 && $last2 == 1 ]]; then
echo "$1: $(date) USB Direct is Down, bring down usb0"
ifconfig usb0 down
status=0
fi
fi
last2=$last
last=$status
sleep 10
done
}

View File

@ -61,7 +61,7 @@ bonding_setup() {
#Run the DHCP client, and bind mount our fake wpa_cli.sh to fool iCamera
ifconfig wlan0 up
pkill udhcpc
udhcpc -i wlan0 -x hostname:$CUSTOM_HOSTNAME -p /var/run/udhcpc.pid -b
udhcpc -i wlan0 -x hostname:$CUSTOM_HOSTNAME -p /var/run/udhcpc.pid -b -S
# If running with Interface Bonding enabled, kill any existing
# wpa_supplicant that might be running and spawn our own instead
@ -78,8 +78,10 @@ bonding_setup() {
mount -o bind /opt/wz_mini/bin/wpa_cli.sh /bin/wpa_cli
fi
echo "Run network monitor for USB Direct"
/opt/wz_mini/usr/bin/netmon.sh &
if [[ "$ENABLE_USB_DIRECT" == "true" ]]; then
echo "Run network monitor for USB Direct"
/opt/wz_mini/usr/bin/netmon.sh &
fi
}

View File

@ -1,14 +1,91 @@
#!/bin/sh
set -x
. /opt/wz_mini/wz_mini.conf
. /opt/wz_mini/etc/rc.common
if [[ "$ENABLE_USB_DIRECT" == "true" ]]; then
wait_for_wlan_ip $(basename "$0")
sleep 5
gateway_supervisor $(basename "$0") &
fi
wait_for_wlan_ip $(basename "$0")
echo "kill udhcpc extra"
kill $(pgrep -f 'udhcpc -i wlan0 -H WyzeCam')
sleep 30
echo "kill icamera udhcpc extra"
kill $(pgrep -f 'udhcpc -i wlan0 -H WyzeCam -p /var/run/udhcpc.pid -b')
# Specify the interfaces
MAIN="usb0"
BACKUP="wlan_old"
BOND="wlan0"
# Specify the log file
LOGFILE="/opt/wz_mini/log/netmon.log"
gateway_supervisor() {
# Get the default gateway
GATEWAY=$(/opt/wz_mini/tmp/.bin/ip route show default | awk '/default/ {print $3}')
# Initialize variables
is_interface_down=0
attempt=0
# Check if the gateway can be reached
while true; do
if /opt/wz_mini/tmp/.bin/ifconfig $MAIN | grep -q "UP"; then
is_interface_down=0
echo "$(date) - Interface $MAIN is up." >> $LOGFILE
fi
if [ $is_interface_down -eq 0 ]; then
attempt=$((attempt+1))
if /opt/wz_mini/tmp/.bin/ping -c 1 $GATEWAY > /dev/null 2>&1; then
echo "$(date) - Internet connection is active." >> $LOGFILE
attempt=0
else
echo "$(date) - Attempt $attempt: Internet connection is down." >> $LOGFILE
if [ $attempt -ge 10 ]; then
/opt/wz_mini/tmp/.bin/ifconfig $MAIN down
echo "$(date) - Interface $MAIN has been brought down." >> $LOGFILE
is_interface_down=1
fi
fi
else
if /opt/wz_mini/tmp/.bin/ifconfig $MAIN | grep -q "UP"; then
is_interface_down=0
echo "$(date) - Interface $MAIN is up." >> $LOGFILE
attempt=0
fi
fi
sleep 5
done
}
monitor_bond() {
# Initial active interface
active_interface=$(cat /proc/net/bonding/$BOND | grep "Currently Active Slave" | awk '{print $4}')
while true; do
# Current active interface
current_interface=$(cat /proc/net/bonding/$BOND | grep "Currently Active Slave" | awk '{print $4}')
if [ "$active_interface" != "$current_interface" ]; then
echo "$(date) - Network interface switched from $active_interface to $current_interface." >> $LOGFILE
# Kill any running udhcpc processes
killall udhcpc
# Start a new udhcpc client
udhcpc -i wlan0 -p /var/run/udhcpc.pid -b -S
# Update the active interface
active_interface=$current_interface
fi
# Sleep for 15 seconds before checking again
sleep 15
done
}
# Start both functions in the background
gateway_supervisor &
monitor_bond &