Files
oracle-clone-standalone/oracle_list_db.sh
2025-10-16 05:45:19 -04:00

164 lines
5.2 KiB
Bash
Executable File

#!/bin/bash
ORIG_ARGS="$*"
#
# Example API call script for Die Mobiliar
# v0.2 - James Pattinson - August 2021
#
# Lists the registered DBs for a given Oracle Host or RAC
# and their assigned SLAs
#
# usage: oracle_list_db.sh [-l <logfile>] <HOSTNAME>
MYDIR="$(dirname "$(realpath "$0")")"
# source $MYDIR/rbk_api.conf
RBK_API_LOG=""
if [ "$1" = "-l" ]; then
if [ -z "$2" ] || [ -z "$3" ]; then
echo "Usage: $0 [-l <logfile>] <dbhost>"
exit 1
fi
RBK_API_LOG="$2"
export RBK_API_LOG
RBK_HOST="$3"
shift 3
else
if [ $# -ne 1 ]; then
echo "Usage: $0 [-l <logfile>] <dbhost>"
exit 1
fi
RBK_HOST=$1
shift 1
fi
# Now RBK_API_LOG is set, so write the log header if enabled
if [ -n "$RBK_API_LOG" ]; then
{
echo "==== Rubrik Oracle List DB Log ===="
date
echo "Script: $0"
echo "Parameters: $ORIG_ARGS"
echo
} >> "$RBK_API_LOG"
fi
source $MYDIR/oracle_funcs.sh
# Set up cleanup trap to ensure temporary files are removed
trap 'rm -f /tmp/rbkdata.$$; cleanup' EXIT INT TERM
echo "Connecting to Rubrik with IP $RUBRIK_IP"
# API call to list Oracle DBs
ENDPOINT="https://$RUBRIK_IP/api/internal/oracle/db"
# Check if rest_api_get function exists and call it
if ! rest_api_get; then
echo "ERROR: Failed to retrieve data from Rubrik API"
exit 1
fi
# Check if the API response file exists and is not empty
if [ ! -s /tmp/rbkresponse.$$ ]; then
echo "ERROR: Empty or missing API response"
cleanup
exit 1
fi
# Extract the data and store in a temporary file for processing
# Simplified jq command - single pass instead of piping through jq twice
if ! jq -r --arg HOST "$RBK_HOST" '
.data[] |
select(.infraPath[0].name==$HOST and .isRelic==false) |
"\(.sid)|\(.effectiveSlaDomainName)|\(.isArchiveLogModeEnabled)|\(.dataGuardType)|\(.dataGuardGroupName)|\(.isDbLocalToTheCluster)"
' /tmp/rbkresponse.$$ > /tmp/rbkdata.$$; then
echo "ERROR: Failed to process API response with jq"
cleanup
exit 1
fi
# Calculate column widths and display results
if [ -s /tmp/rbkdata.$$ ]; then
# Initialize minimum column widths based on headers
col1_width=3 # SID
col2_width=3 # SLA
col3_width=14 # ArchivelogMode
col4_width=7 # DG Type
col5_width=8 # DG Group
col6_width=8 # Location
# Calculate actual maximum widths needed
while IFS='|' read -r sid sla archlog dgtype dggroup islocal || [ -n "$sid" ]; do
# Handle null values and empty strings
[ "$sid" = "null" ] && sid=""
[ "$sla" = "null" ] && sla=""
[ "$archlog" = "null" ] && archlog=""
[ "$dgtype" = "null" ] && dgtype=""
[ "$dggroup" = "null" ] && dggroup=""
[ "$islocal" = "null" ] && islocal=""
# Convert boolean to Location text
if [ "$islocal" = "true" ]; then
location="Local"
elif [ "$islocal" = "false" ]; then
location="Remote"
else
location=""
fi
# Update column widths if current data is longer
[ ${#sid} -gt $col1_width ] && col1_width=${#sid}
[ ${#sla} -gt $col2_width ] && col2_width=${#sla}
[ ${#archlog} -gt $col3_width ] && col3_width=${#archlog}
[ ${#dgtype} -gt $col4_width ] && col4_width=${#dgtype}
[ ${#dggroup} -gt $col5_width ] && col5_width=${#dggroup}
[ ${#location} -gt $col6_width ] && col6_width=${#location}
done < /tmp/rbkdata.$$
# Print headers with proper spacing
printf "%-${col1_width}s %-${col2_width}s %-${col3_width}s %-${col4_width}s %-${col5_width}s %-${col6_width}s\n" \
"SID" "SLA" "ArchivelogMode" "DG Type" "DG Group" "Location"
# Print separator line
printf "%-${col1_width}s %-${col2_width}s %-${col3_width}s %-${col4_width}s %-${col5_width}s %-${col6_width}s\n" \
"$(printf '%*s' $col1_width | tr ' ' '-')" \
"$(printf '%*s' $col2_width | tr ' ' '-')" \
"$(printf '%*s' $col3_width | tr ' ' '-')" \
"$(printf '%*s' $col4_width | tr ' ' '-')" \
"$(printf '%*s' $col5_width | tr ' ' '-')" \
"$(printf '%*s' $col6_width | tr ' ' '-')"
# Print data rows with proper spacing
while IFS='|' read -r sid sla archlog dgtype dggroup islocal || [ -n "$sid" ]; do
# Handle null values and empty strings
[ "$sid" = "null" ] && sid=""
[ "$sla" = "null" ] && sla=""
[ "$archlog" = "null" ] && archlog=""
[ "$dgtype" = "null" ] && dgtype=""
[ "$dggroup" = "null" ] && dggroup=""
[ "$islocal" = "null" ] && islocal=""
# Convert boolean to Location text
if [ "$islocal" = "true" ]; then
location="Local"
elif [ "$islocal" = "false" ]; then
location="Remote"
else
location=""
fi
printf "%-${col1_width}s %-${col2_width}s %-${col3_width}s %-${col4_width}s %-${col5_width}s %-${col6_width}s\n" \
"$sid" "$sla" "$archlog" "$dgtype" "$dggroup" "$location"
done < /tmp/rbkdata.$$
else
echo "No Oracle databases found for host $RBK_HOST"
fi
# Clean up temporary files
rm -f /tmp/rbkdata.$$
cleanup