#!/bin/bash
#Understood. Below is an enhanced version of the script that includes the calculation of maximum capacity, current capacity, and available capacity with a buffer. This script assumes you have predefined maximum capacities for CPU, IOPS, QPS, TPS, and sessions.
# Oracle environment variables
#export ORACLE_SID=your_oracle_sid
#export ORACLE_HOME=/path/to/your/oracle_home
#export PATH=$ORACLE_HOME/bin:$PATH
# Database credentials
#DB_USER="your_db_user"
#DB_PASS="your_db_password"
# Temporary file to store SQL output
SQL_OUTPUT="/tmp/db_capacity_analysis.txt"
# Predefined maximum capacities
MAX_CPU_UTILIZATION=60 # in percentage
MAX_IOPS=500000 # in IOPS
MAX_QPS=200000 # in QPS
MAX_TPS=200000 # in TPS
MAX_SESSIONS=10000 # in sessions
# Buffer percentage
BUFFER_PERCENTAGE=15
# Function to get current CPU utilization
get_cpu_utilization() {
sar -u 1 5 | grep "Average" | awk '{print $3 + $5}'
}
# Function to get current IOPS
get_iops() {
iostat -dx 1 5 | grep -A 1 "Device" | tail -n 1 | awk '{print $2}'
}
# Function to get current QPS and TPS
get_qps_tps() {
sqlplus -s $DB_USER/$DB_PASS <<EOF > $SQL_OUTPUT
SET PAGESIZE 0 FEEDBACK OFF VERIFY OFF HEADING OFF ECHO OFF
SELECT SUM(value) FROM v\$sysstat WHERE name = 'execute count';
SELECT SUM(value) FROM v\$sysstat WHERE name = 'user commits' OR name = 'user rollbacks';
EXIT;
EOF
QPS=$(sed -n '1p' $SQL_OUTPUT)
TPS=$(sed -n '2p' $SQL_OUTPUT)
echo "$QPS $TPS"
}
# Function to get current session count
get_session_count() {
sqlplus -s $DB_USER/$DB_PASS <<EOF > $SQL_OUTPUT
SET PAGESIZE 0 FEEDBACK OFF VERIFY OFF HEADING OFF ECHO OFF
SELECT COUNT(*) FROM v\$session WHERE type = 'USER';
EXIT;
EOF
cat $SQL_OUTPUT
}
# Function to get current IOPS from Oracle
get_oracle_iops() {
sqlplus -s $DB_USER/$DB_PASS <<EOF > $SQL_OUTPUT
SET PAGESIZE 0 FEEDBACK OFF VERIFY OFF HEADING OFF ECHO OFF
SELECT SUM(value) FROM v\$sysstat WHERE name IN ('physical reads', 'physical writes');
EXIT;
EOF
cat $SQL_OUTPUT
}
# Main function to gather all metrics and calculate capacities
gather_metrics() {
echo "Gathering database capacity metrics..."
# Get current metrics
CURRENT_CPU_UTILIZATION=$(get_cpu_utilization)
CURRENT_IOPS=$(get_iops)
read CURRENT_QPS CURRENT_TPS <<< $(get_qps_tps)
CURRENT_SESSIONS=$(get_session_count)
CURRENT_ORACLE_IOPS=$(get_oracle_iops)
# Calculate available capacities with buffer
BUFFER=$(echo "scale=2; $BUFFER_PERCENTAGE / 100" | bc)
AVAILABLE_CPU_UTILIZATION=$(echo "scale=2; $MAX_CPU_UTILIZATION * (1 - $BUFFER) - $CURRENT_CPU_UTILIZATION" | bc)
AVAILABLE_IOPS=$(echo "scale=2; $MAX_IOPS * (1 - $BUFFER) - $CURRENT_IOPS" | bc)
AVAILABLE_QPS=$(echo "scale=2; $MAX_QPS * (1 - $BUFFER) - $CURRENT_QPS" | bc)
AVAILABLE_TPS=$(echo "scale=2; $MAX_TPS * (1 - $BUFFER) - $CURRENT_TPS" | bc)
AVAILABLE_SESSIONS=$(echo "scale=2; $MAX_SESSIONS * (1 - $BUFFER) - $CURRENT_SESSIONS" | bc)
AVAILABLE_ORACLE_IOPS=$(echo "scale=2; $MAX_IOPS * (1 - $BUFFER) - $CURRENT_ORACLE_IOPS" | bc)
# Print results
echo "Max Capacity for Apps:"
echo "CPU Utilization: $MAX_CPU_UTILIZATION%"
echo "IOPS: $MAX_IOPS"
echo "QPS: $MAX_QPS"
echo "TPS: $MAX_TPS"
echo "Sessions: $MAX_SESSIONS"
echo "Current Capacity of Apps:"
echo "CPU Utilization: $CURRENT_CPU_UTILIZATION%"
echo "IOPS: $CURRENT_IOPS"
echo "QPS: $CURRENT_QPS"
echo "TPS: $CURRENT_TPS"
echo "Sessions: $CURRENT_SESSIONS"
echo "Available Capacity with $BUFFER_PERCENTAGE% Buffer:"
echo "CPU Utilization: $AVAILABLE_CPU_UTILIZATION%"
echo "IOPS: $AVAILABLE_IOPS"
echo "QPS: $AVAILABLE_QPS"
echo "TPS: $AVAILABLE_TPS"
echo "Sessions: $AVAILABLE_SESSIONS"
echo "Oracle IOPS: $AVAILABLE_ORACLE_IOPS"
echo "Metrics gathering complete."
}
# Run the main function
gather_metrics
Explanation:
Environment Variables: Set the Oracle environment variables (ORACLE_SID, ORACLE_HOME, and PATH).
Database Credentials: Define the database user and password.
Temporary File: Create a temporary file to store SQL output.
Predefined Maximum Capacities: Define the maximum capacities for CPU utilization, IOPS, QPS, TPS, and sessions.
Buffer Percentage: Define the buffer percentage to be used in capacity calculations.
Functions:
get_cpu_utilization: Uses sar to get current CPU utilization.
get_iops: Uses iostat to get current IOPS.
get_qps_tps: Uses sqlplus to get current QPS and TPS from Oracle.
get_session_count: Uses sqlplus to get the current session count.
get_oracle_iops: Uses sqlplus to get current IOPS from Oracle.
Main Function: Gathers current metrics, calculates available capacities with the buffer, and prints the results.
Usage:
Save the script to a file, e.g., db_capacity_analysis.sh.
Make the script executable: chmod +x db_capacity_analysis.sh.
Run the script: ./db_capacity_analysis.sh.
Ensure you replace placeholders like your_oracle_sid, your_oracle_home, your_db_user, and your_db_password with actual values specific to your environment.