Files
oracle-clone-standalone/oracle_list_db.sh
2025-10-14 09:16:37 -04:00

113 lines
3.8 KiB
Bash
Executable File

#!/bin/bash
#
# 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 <HOSTNAME>
MYDIR="$(dirname "$(realpath "$0")")"
# source $MYDIR/rbk_api.conf
source $MYDIR/oracle_funcs.sh
# Set up cleanup trap to ensure temporary files are removed
trap 'rm -f /tmp/rbkdata.$$; cleanup' EXIT INT TERM
if [ $# -ne 1 ]; then
echo "Usage: $0 <dbhost>"
exit 1
fi
RBK_HOST=$1
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)"
' /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
# Calculate actual maximum widths needed
while IFS='|' read -r sid sla archlog dgtype dggroup || [ -n "$sid" ]; do
# Handle null values and empty strings
[ "$sid" = "null" ] && sid=""
[ "$sla" = "null" ] && sla=""
[ "$archlog" = "null" ] && archlog=""
[ "$dgtype" = "null" ] && dgtype=""
[ "$dggroup" = "null" ] && dggroup=""
# 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}
done < /tmp/rbkdata.$$
# Print headers with proper spacing
printf "%-${col1_width}s %-${col2_width}s %-${col3_width}s %-${col4_width}s %-${col5_width}s\n" \
"SID" "SLA" "ArchivelogMode" "DG Type" "DG Group"
# Print separator line
printf "%-${col1_width}s %-${col2_width}s %-${col3_width}s %-${col4_width}s %-${col5_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 ' ' '-')"
# Print data rows with proper spacing
while IFS='|' read -r sid sla archlog dgtype dggroup || [ -n "$sid" ]; do
# Handle null values and empty strings
[ "$sid" = "null" ] && sid=""
[ "$sla" = "null" ] && sla=""
[ "$archlog" = "null" ] && archlog=""
[ "$dgtype" = "null" ] && dgtype=""
[ "$dggroup" = "null" ] && dggroup=""
printf "%-${col1_width}s %-${col2_width}s %-${col3_width}s %-${col4_width}s %-${col5_width}s\n" \
"$sid" "$sla" "$archlog" "$dgtype" "$dggroup"
done < /tmp/rbkdata.$$
else
echo "No Oracle databases found for host $RBK_HOST"
fi
# Clean up temporary files
rm -f /tmp/rbkdata.$$
cleanup