Forum Discussion

Zeeguy09's avatar
Zeeguy09
Icon for Nimbostratus rankNimbostratus
Jun 27, 2024

Script to send an email if Traffic-group failovers on F5

I am using this script to detect the status of a traffic-group and send out an email if it changes its status from Active to Standby, some how i am not getting any email when i flip over the traffic group between active standby boxes. Though i have tested email through CLI and mail works. need experts advice if i am missing any thing?

 

 

 

#!/bin/bash

# Variables
EMAIL_TO="x@x.com"

EMAIL_SUBJECT="HLR-STG-LB01 WHARF Traffic-Group Failover Alert"
TRAFFIC_GROUP="wharf"
CHECK_INTERVAL=60
LOG_FILE="/var/log/failover_notify.log"

# Function to send email
send_email() {
    local message=$1
    echo -e "To: ${EMAIL_TO}\nSubject: ${EMAIL_SUBJECT}\n\n${message}" | ssmtp ${EMAIL_TO}
}

# Function to log messages
log_message() {
    local message=$1
    echo "$(date): ${message}" >> ${LOG_FILE} 2>&1
}

# Function to get the current status of the traffic group
get_traffic_group_status() {
    tmsh show cm traffic-group | grep "${TRAFFIC_GROUP}"
}

# Initial state
previous_status=$(get_traffic_group_status)

# Main loop
while true; do
    current_status=$(get_traffic_group_status)

    if [[ "${previous_status}" != "${current_status}" ]]; then
        if echo "${current_status}" | grep -qi "standby"; then
            log_message "Traffic group ${TRAFFIC_GROUP} failed over to standby."
            send_email "Alert: Traffic group ${TRAFFIC_GROUP} has failed over to standby on another device."
        elif echo "${current_status}" | grep -qi "active"; then
            log_message "Traffic group ${TRAFFIC_GROUP} is now active."
            send_email "Info: Traffic group ${TRAFFIC_GROUP} is now active on this device."
        fi
        previous_status=${current_status}
    fi

    sleep ${CHECK_INTERVAL}
done
~
~

 

Added this script to crontab.

ensured that script is running

 

#ps aux | grep failover_notify.sh
root     23928  0.0  0.0 115208  1500 pts/1    T    12:04   0:00 /bin/bash ./failover_notify.sh
root     31495  0.0  0.0 114736   948 pts/1    S+   12:52   0:00 grep failover_notify.sh

 

 

No RepliesBe the first to reply