#!/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)
# Arrays to hold summary information
DB_HEALTH_GREEN=()
WALLET_IDENTIFIERS=()
DB_RESTRICTED_MODE=()
# 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 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]//')
APPLY_LAG=$(grep 'Apply lag' /tmp/dg_lag.log | awk '{print $NF}' | sed 's/[seconds|second]//')
if [ "$LAG" -gt $LAG_THRESHOLD ] || [ "$APPLY_LAG" -gt $LAG_THRESHOLD ]; then
echo "$DB_NAME has Data Guard lag: Transport Lag: ${LAG} seconds, Apply Lag: ${APPLY_LAG} seconds."
else
DB_HEALTH_GREEN+=("$DB_NAME DB HC green")
fi
else
DB_HEALTH_GREEN+=("$DB_NAME DB HC green")
fi
else
echo "$DB_NAME Data Guard check failed."
fi
}
# Function to check if database is in read-write mode and not restricted mode
check_db_mode() {
DB_NAME=$1
echo "Checking mode for database: $DB_NAME..."
sqlplus -s / as sysdba <<EOF > /tmp/db_mode.log
SET PAGESIZE 0 FEEDBACK OFF VERIFY OFF HEADING OFF ECHO OFF;
SELECT STATUS FROM V\$DATABASE;
SELECT RESTRICTED FROM V\$PARAMETER WHERE NAME = 'restricted';
EXIT;
EOF
if [ $? -eq 0 ]; then
STATUS=$(grep -i "READ WRITE" /tmp/db_mode.log)
RESTRICTED=$(grep -i "TRUE" /tmp/db_mode.log)
if [ -n "$STATUS" ]; then
echo "Database $DB_NAME is in read-write mode."
else
echo "Database $DB_NAME is not in read-write mode."
DB_RESTRICTED_MODE+=("Database $DB_NAME is not in read-write mode.")
fi
if [ -n "$RESTRICTED" ]; then
echo "Database $DB_NAME is in restricted mode."
DB_RESTRICTED_MODE+=("Database $DB_NAME is in restricted mode.")
else
echo "Database $DB_NAME is not in restricted mode."
fi
else
echo "Failed to check database mode for $DB_NAME."
fi
}
# Check wallet identifiers in /u01/app/oracle/okv/
check_wallet_identifiers() {
echo "Checking for wallet identifiers in /u01/app/oracle/okv/..."
# Ensure the directory exists
if [ -d "/u01/app/oracle/okv" ]; then
# List files in /u01/app/oracle/okv/ and capture their names
WALLET_FILES=$(ls /u01/app/oracle/okv/)
# Initialize wallet count
WALLET_COUNT=0
if [ -n "$WALLET_FILES" ]; then
# Report the wallets
while IFS= read -r FILE; do
# Use file name as wallet identifier
WALLET_IDENTIFIERS+=("Database with wallet identifier $FILE has wallet configured.")
WALLET_COUNT=$((WALLET_COUNT + 1))
done <<< "$WALLET_FILES"
echo "Total number of wallets found: $WALLET_COUNT"
else
echo "No wallet identifiers found in /u01/app/oracle/okv/."
fi
else
echo "/u01/app/oracle/okv directory does not exist."
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_dgmgrl_lag "$DB_NAME"
check_db_mode "$DB_NAME"
echo "-------------------------------------------"
done < $ORATAB
# Run wallet identifier check
check_wallet_identifiers
# Print summary of wallet identifiers
echo "================== WALLET IDENTIFIERS =================="
if [ ${#WALLET_IDENTIFIERS[@]} -eq 0 ]; then
echo "No wallets are configured or no identifiers found."
else
for wallet in "${WALLET_IDENTIFIERS[@]}"; do
echo "$wallet"
done
fi
# Print databases that passed all checks
echo "================== HEALTH CHECK =================="
if [ ${#DB_HEALTH_GREEN[@]} -eq 0 ]; then
echo "No databases passed the health check."
else
for db in "${DB_HEALTH_GREEN[@]}"; do
echo "$db"
done
fi
# Print databases in restricted mode or not in read-write mode
echo "================== RESTRICTED MODE =================="
if [ ${#DB_RESTRICTED_MODE[@]} -eq 0 ]; then
echo "All databases are in read-write mode and not in restricted mode."
else
for restricted in "${DB_RESTRICTED_MODE[@]}"; do
echo "$restricted"
done
fi
echo "============================================="





0 comments:
Post a Comment