Monday, January 5, 2015

Points to remember in Couchbase

#!/bin/bash

# Path to the file containing instance and hostname information
FILE_PATH="path/to/your/file.txt"

# Function to execute a command over SSH
execute_ssh_command() {
local hostname=$1
local command=$2
ssh "$hostname" "$command"
}

# Function to parse DGMGRL output
parse_dgmgrl_output() {
local output=$1
local transport_lag=$(echo "$output" | grep -oP 'Transport Lag: \K\d+')
local apply_lag=$(echo "$output" | grep -oP 'Apply Lag: \K\d+')
local switchover_ready=$(echo "$output" | grep -oP 'Ready for Switchover: \K(Yes|No)')
echo "$transport_lag $apply_lag $switchover_ready"
}

# Function to check RAC status
check_rac_status() {
local hostname=$1
local db_name=$2
local output=$(execute_ssh_command "$hostname" "srvctl status service -d $db_name")
local running_services=$(echo "$output" | grep -c "is running")
local not_running_services=$(echo "$output" | grep -c "is not running")
echo "Running: $running_services, Not Running: $not_running_services"
}

# Function to check triggers
check_triggers() {
local hostname=$1
local output=$(execute_ssh_command "$hostname" "check if %DNG% triggers are available")
if echo "$output" | grep -q "valid"; then
echo "Valid"
else
echo "Invalid"
fi
}

# Main function
main() {
echo -e "Date\tPrimary DB\tRAC?\tStandby Name\tServices Running\tTrigger\tSwitchover Ready?\tLag\tIssues"
while IFS= read -r line; do
instance=$(echo "$line" | awk '{print $1}')
hostname=$(echo "$line" | awk '{print $4}')
# Set ORACLE_SID environment variable
export ORACLE_SID="$instance"
# Execute DGMGRL commands
dgmgrl_output=$(execute_ssh_command "$hostname" "dgmgrl command to show configuration lag")
validation_output=$(execute_ssh_command "$hostname" "dgmgrl command to validate database $instance")
# Parse outputs
read transport_lag apply_lag switchover_ready <<< $(parse_dgmgrl_output "$dgmgrl_output")
rac_status=$(check_rac_status "$hostname" "$instance")
trigger_status=$(check_triggers "$hostname")
# Determine issues
if [[ "$switchover_ready" == "Yes" && "$transport_lag" -lt 10 && "$apply_lag" -lt 10 ]]; then
issues="No issues"
else
issues="Issues detected"
fi
# Print results
echo -e "$(date '+%Y-%m-%d %H:%M:%S')\t$instance\tYes\t$instance\t$rac_status\t$trigger_status\t$switchover_ready\tTransport: ${transport_lag}s, Apply: ${apply_lag}s\t$issues"
done < "$FILE_PATH"
}

# Run the main function
main