Last active
January 17, 2025 04:28
-
-
Save qiwichupa/8f78eca9766e07f46397ae5264be629d to your computer and use it in GitHub Desktop.
checks if the gateway is not available and switches the active interface in bond
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Checks if the gateway is not available and switches the active interface in bond. | |
# The script is made for zvirt node, designed for bond management via NetworkManager. | |
# | |
# v2024.04.05 | |
# | |
# Change your "bondname" and add script to crontab like: | |
# @reboot root /opt/bond_mon.sh | |
bondname=bond0 | |
upwait=30 | |
checkinterval=10 | |
# get ip of gateway as testip (can be changed to any IP that can ping) | |
testip=$(ip -j route show 0.0.0.0/0 dev $bondname | jq -r '.[0].gateway') | |
echo ==== Monitoring Started. Bond $bondname, test $testip ==== | systemd-cat -t bond_mon | |
while true | |
do | |
if [ -f /proc/net/bonding/${bondname} ] | |
then | |
slaves=$(cat /proc/net/bonding/${bondname} | grep "Slave Interface" | cut -d ":" -f 2 | sed 's/\s//') | |
activeslave=$(cat /proc/net/bonding/${bondname} | grep "Currently Active Slave" | cut -d ":" -f 2 | sed 's/\s//') | |
echo ${bondname} slaves: $slaves | systemd-cat -t bond_mon | |
echo ${bondname} active: $activeslave | systemd-cat -t bond_mon | |
while ping -c 5 ${testip} > /dev/null 2>&1 | |
do | |
sleep $checkinterval | |
done | |
slavesX2="$slaves $slaves" | |
current_slave_found=0 | |
for slave in $slavesX2 | |
do | |
if [[ $current_slave_found == 1 ]] | |
then | |
echo ${bondname} changing slave to: $slave | systemd-cat -t bond_mon | |
nmcli dev mod ${bondname} +bond.options "active_slave=${slave}" | |
break | |
fi | |
if [[ $slave == "$activeslave" ]] | |
then | |
current_slave_found=1 | |
fi | |
done | |
fi | |
sleep $upwait | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment