Skip to content

Instantly share code, notes, and snippets.

@qiwichupa
Last active April 5, 2024 10:57
Show Gist options
  • Save qiwichupa/8f78eca9766e07f46397ae5264be629d to your computer and use it in GitHub Desktop.
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
#!/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