Skip to content
db2 2 min read

Check db2 HADR status with multiple instances

Those of you that have to deal with multiple db2 instances on 1 machine know that it is annoying to get a quick overview of the HADR status in 1 go. You need to switch accounts, run the commands, etc...

I created a script you can run as root that will give you an overview, I added some color coding for convenience as well

Red will indicate an issue, blue if HADR is connected, purple for standby and green for primary

#!/bin/bash
#===============================================================================
# Script Name: check_hadr_status_all_instances.sh
# Description: Check HADR status for all databases in all DB2 instances on a server.
# Created by Wannes Rams
# 2026
# Usage: sudo ./check_hadr_status_all_instances.sh
#===============================================================================

# Color codes for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
CYAN='\033[0;36m'
MAGENTA='\033[0;35m'
NC='\033[0m' # No Color

# Function to log messages with a default color
log_message() {
    local message="$2"
    local color="${1:-$NC}" # Default to no color
    echo -e "${color}$(date '+%Y-%m-%d %H:%M:%S') - $message${NC}"
}

# Function to list databases for a given DB2 instance
list_databases_for_instance() {
    local instance="$1"

    su - "$instance" -c "
        if [ -f \"\$HOME/sqllib/db2profile\" ]; then
        . \$HOME/sqllib/db2profile
        fi
        db2 list db directory | grep -i 'Database alias' | awk '{print \$NF}'
    " 2>/dev/null
}

# Function to check HADR status for a database
check_hadr_status() {
    local instance="$1"
    local database="$2"

    log_message $NC "Checking HADR status for Instance: $instance, Database: $database..."

    local status=$(su - "$instance" -c "
        if [ -f \"\$HOME/sqllib/db2profile\" ]; then
            . \$HOME/sqllib/db2profile
        fi
        db2pd -db $database -hadr
    " 2>/dev/null)

    if echo "$status" | grep -q "HADR is not active"; then
        log_message $YELLOW "HADR status for $instance/$database: NOT ACTIVE"
    else
        # Evaluating simplified conditions for demonstration purposes:
        if echo "$status" | grep -q "HADR_ROLE = PRIMARY"; then
            log_message $GREEN "HADR status for $instance/$database: ACTIVE (PRIMARY)"
                if echo "$status" | grep -q "HADR_CONNECT_STATUS = CONNECTED"; then
                        log_message $CYAN "HADR status for $instance/$database: CONNECTED"
                else
                        log_message $RED "HADR status for $instance/$database: DISCONNECTED"
                fi
        elif echo "$status" | grep -q "HADR_ROLE = STANDBY"; then
            log_message $MAGENTA "HADR status for $instance/$database: ACTIVE (STANDBY)"
                if echo "$status" | grep -q "HADR_CONNECT_STATUS = CONNECTED"; then
                        log_message $CYAN "HADR status for $instance/$database: CONNECTED"
                else
                        log_message $RED "HADR status for $instance/$database: DISCONNECTED"
                fi
        else
            log_message $RED "HADR status for $instance/$database: UNKNOWN STATE"
        fi
    fi
}

# Main logic
if command -v db2ilist >/dev/null 2>&1; then
    instances=$(db2ilist)
else
    log_message $RED "ERROR: db2ilist command not found. Ensure that DB2 is installed and db2ilist is in your PATH."
    exit 1
fi

for instance in $instances; do
    log_message $NC "Processing instance: $instance"
    databases=$(list_databases_for_instance "$instance")

    for database in $databases; do
        check_hadr_status "$instance" "$database"
    done
done

log_message $CYAN "HADR status check completed."