#!/bin/vbash # Define variables PING_TARGET="1.1.1.1" PING_COUNT=3 VRRP_GROUP="vrrp0" LOW_PRIORITY=50 NORMAL_PRIORITY=200 LOG_FILE="/var/log/vrrp_migrate.log" # Logging function log() { echo "$(date): $1" | tee -a $LOG_FILE } # Load VyOS environment source /opt/vyatta/etc/functions/script-template # Get the current VRRP priority CURRENT_PRIORITY=$(run show configuration commands | grep "set high-availability vrrp group $VRRP_GROUP priority" | awk '{print $7}') # Log current state log "Starting VRRP monitoring script. Current priority: $CURRENT_PRIORITY." # Check connectivity to the target if ! ping -c $PING_COUNT $PING_TARGET > /dev/null; then log "Ping to $PING_TARGET failed." if [ "$CURRENT_PRIORITY" != "$LOW_PRIORITY" ]; then log "Reducing VRRP priority to $LOW_PRIORITY." configure set high-availability vrrp group $VRRP_GROUP priority $LOW_PRIORITY commit save log "VRRP priority updated to $LOW_PRIORITY." else log "Priority already set to $LOW_PRIORITY. No changes needed." fi else log "Ping to $PING_TARGET succeeded." if [ "$CURRENT_PRIORITY" != "$NORMAL_PRIORITY" ]; then log "Restoring VRRP priority to $NORMAL_PRIORITY." configure set high-availability vrrp group $VRRP_GROUP priority $NORMAL_PRIORITY commit save log "VRRP priority updated to $NORMAL_PRIORITY." else log "Priority already set to $NORMAL_PRIORITY. No changes needed." fi fi log "VRRP monitoring script completed."