#!/bin/bash
# Define the path to oratab
ORATAB="/etc/oratab"
# Check if oratab exists
if [ ! -f $ORATAB ]; then
echo "$ORATAB not found. Exiting."
exit 1
fi
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 set Oracle environment for a specific database
set_oracle_env() {
DB_NAME=$1
ORACLE_HOME=$2
export ORACLE_HOME=$ORACLE_HOME
export ORACLE_SID=$DB_NAME
export PATH=$ORACLE_HOME/bin:$PATH
echo "Set environment for database: $DB_NAME with ORACLE_HOME: $ORACLE_HOME"
}
# Function to check if a database is up and running
check_db_status() {
DB_NAME=$1
echo "Checking status for database: $DB_NAME..."
ps -ef | grep pmon | grep $DB_NAME | grep -v grep > $DB_STATUS_FILE
if [ -s $DB_STATUS_FILE ]; then
echo "Database $DB_NAME is up and running."
else
echo "Database $DB_NAME is down."
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() {
DB_NAME=$1
echo "Performing Data Guard checks for $DB_NAME..."
dgmgrl <<EOF > /tmp/dg_status.log
connect /;
show configuration;
exit;
EOF
if [ $? -eq 0 ]; then
echo "Data Guard configuration checked for $DB_NAME."
# Checking for lag
LAG_INFO=$(dgmgrl <<EOF
connect /;
show database verbose primary;
exit;
EOF
)
echo "$LAG_INFO" | grep 'Transport lag\|Apply lag' > /tmp/dg_lag.log
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 for $DB_NAME: ${LAG} seconds."
else
echo "Data Guard transport lag is within acceptable limits for $DB_NAME."
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 for $DB_NAME: ${APPLY_LAG} seconds."
else
echo "Data Guard apply lag is within acceptable limits for $DB_NAME."
fi
else
echo "No significant Data Guard lag detected for $DB_NAME."
fi
else
echo "Data Guard check failed for $DB_NAME."
fi
}
# Read oratab and loop through each database
while IFS=: read -r DB_NAME ORACLE_HOME DB_STARTUP_FLAG; do
# Skip comments and entries with no ORACLE_HOME
if [[ "$DB_NAME" =~ ^# ]] || [ -z "$ORACLE_HOME" ]; then
continue
fi
# Set the Oracle environment for each database
set_oracle_env "$DB_NAME" "$ORACLE_HOME"
# Run checks for each database
check_db_status "$DB_NAME"
check_goldengate
check_wallets
check_dgmgrl_lag "$DB_NAME"
echo "-------------------------------------------"
done < $ORATAB
echo "Health check completed for all databases."





0 comments:
Post a Comment