Saturday, 14 September 2024

Filled under:

 #!/bin/bash


# Oracle environment setup

export ORACLE_HOME="/u01/app/oracle/product/19.0.0/dbhome_1"  # Update this with your Oracle home

export PATH=$ORACLE_HOME/bin:$PATH

export ORACLE_SID="orcl"  # Update with your Oracle SID


DB_STATUS_FILE="/tmp/db_status.log"

GGSCI_PATH="/usr/local/bin/ggsci"  # Modify this to your GoldenGate installation path

LAG_THRESHOLD=0  # Set your lag threshold in seconds (0 for no lag allowed)


# Function to check if databases are up and running

check_db_status() {

    echo "Checking databases..."

    ps -ef | grep pmon | grep -v grep > $DB_STATUS_FILE


    # Count databases up and running

    DB_COUNT=$(cat $DB_STATUS_FILE | wc -l)

    

    if [ $DB_COUNT -gt 0 ]; then

        echo "$DB_COUNT databases are up and running."

    else

        echo "No databases are up."

    fi

}


# Function to check if GoldenGate exists

check_goldengate() {

    echo "Checking for GoldenGate installation..."

    

    if locate ggsci &>/dev/null; then

        echo "GoldenGate is installed."

    else

        echo "GoldenGate not found."

    fi

}


# Function to check databases with wallets

check_wallets() {

    echo "Checking for databases with wallets..."

    

    WALLET_COUNT=$(ls -ltr /u01/app/oracle/okv 2>/dev/null | grep -v total | wc -l)

    

    if [ $WALLET_COUNT -gt 0 ]; then

        echo "$WALLET_COUNT databases have wallets."

    else

        echo "No databases have wallets."

    fi

}


# Function to perform Data Guard checks with dgmgrl, including lag

check_dgmgrl_lag() {

    echo "Performing Data Guard checks..."


    # Run dgmgrl commands directly without su

    dgmgrl -silent <<EOF > /tmp/dg_status.log

    connect /;

    show configuration;

    exit;

EOF


    if [ $? -eq 0 ]; then

        echo "Data Guard configuration checked."


        # Checking for lag

        LAG_INFO=$(dgmgrl -silent <<EOF

        connect /;

        show database verbose primary;  # Adjust this depending on your primary/standby setup

        exit;

EOF

        )


        echo "$LAG_INFO" | grep 'Transport lag\|Apply lag' > /tmp/dg_lag.log


        # Extract the lag values from the log and report if any exceed the threshold

        if grep -q "seconds" /tmp/dg_lag.log; then

            LAG=$(grep 'Transport lag' /tmp/dg_lag.log | awk '{print $NF}' | sed 's/[seconds|second]//')

            if [ "$LAG" -gt $LAG_THRESHOLD ]; then

                echo "Warning: Data Guard transport lag detected: ${LAG} seconds."

            else

                echo "Data Guard transport lag is within acceptable limits."

            fi


            APPLY_LAG=$(grep 'Apply lag' /tmp/dg_lag.log | awk '{print $NF}' | sed 's/[seconds|second]//')

            if [ "$APPLY_LAG" -gt $LAG_THRESHOLD ]; then

                echo "Warning: Data Guard apply lag detected: ${APPLY_LAG} seconds."

            else

                echo "Data Guard apply lag is within acceptable limits."

            fi

        else

            echo "No significant Data Guard lag detected."

        fi

    else

        echo "Data Guard check failed."

    fi

}


# Execute all checks

echo "Starting Oracle Database Health Checks..."

check_db_status

check_goldengate

check_wallets

check_dgmgrl_lag

echo "Health check completed."


0 comments:

Post a Comment