88 lines
2.7 KiB
Bash
Executable File
88 lines
2.7 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
# File paths for output
|
|
OUTPUT_FILE="/tmp/datafile_usage.tmp"
|
|
PDB_LIST_FILE="/tmp/pdb_list.tmp"
|
|
> "$OUTPUT_FILE" # Clear the output file before starting
|
|
touch $OUTPUT_FILE
|
|
chown oracle $OUTPUT_FILE
|
|
|
|
# Function to execute the datafile usage query in a specific container
|
|
run_query() {
|
|
local container_name="$1"
|
|
su - oracle -s /bin/bash -c "
|
|
sqlplus -s / as sysdba <<EOF >> \"$OUTPUT_FILE\"
|
|
whenever sqlerror exit sql.sqlcode;
|
|
|
|
SET ECHO ON
|
|
SET HEADING ON
|
|
SET UNDERLINE '='
|
|
SET FEEDBACK OFF
|
|
SET LINESIZE 200
|
|
SET PAGESIZE 100
|
|
COLUMN datafile_path FORMAT A50
|
|
COLUMN tablespace_name FORMAT A20
|
|
COLUMN allocated_mb FORMAT 9999999.99
|
|
COLUMN used_mb FORMAT 9999999.99
|
|
COLUMN free_mb FORMAT 9999999.99
|
|
COLUMN pct_used FORMAT 999.99
|
|
|
|
-- Switch to the specified container
|
|
ALTER SESSION SET CONTAINER = $container_name;
|
|
|
|
-- Print the container name for clarity
|
|
PROMPT Datafile usage for container: $container_name;
|
|
|
|
SELECT d.file_name AS datafile_path,
|
|
d.tablespace_name,
|
|
ROUND(d.bytes / 1024 / 1024, 2) AS allocated_mb,
|
|
ROUND((d.bytes - NVL(fs.bytes, 0)) / 1024 / 1024, 2) AS used_mb,
|
|
ROUND(NVL(fs.bytes, 0) / 1024 / 1024, 2) AS free_mb,
|
|
ROUND(((d.bytes - NVL(fs.bytes, 0)) / d.bytes) * 100, 2) AS pct_used
|
|
FROM dba_data_files d
|
|
LEFT JOIN (
|
|
SELECT file_id, SUM(bytes) AS bytes
|
|
FROM dba_free_space
|
|
GROUP BY file_id
|
|
) fs ON d.file_id = fs.file_id
|
|
ORDER BY d.tablespace_name, d.file_name;
|
|
|
|
exit;
|
|
EOF
|
|
"
|
|
}
|
|
|
|
# Run the query for the CDB (root container)
|
|
echo "Gathering datafile usage for CDB (root container)..." >> "$OUTPUT_FILE"
|
|
run_query 'CDB\$ROOT'
|
|
|
|
# Get a list of all PDBs, excluding PDB$SEED, and write to a temporary file
|
|
su - oracle -s /bin/bash -c "
|
|
sqlplus -s / as sysdba <<'EOF'
|
|
set heading off
|
|
set feedback off
|
|
set pagesize 0
|
|
spool $PDB_LIST_FILE
|
|
SELECT NAME FROM v\$pdbs WHERE NAME NOT IN ('PDB\$SEED');
|
|
spool off
|
|
exit;
|
|
EOF
|
|
"
|
|
|
|
# Verify that the PDB_LIST_FILE was created and contains data
|
|
if [[ -f "$PDB_LIST_FILE" && -s "$PDB_LIST_FILE" ]]; then
|
|
# Read each valid PDB name from the temporary file
|
|
while IFS= read -r PDB; do
|
|
PDB=$(echo "$PDB" | xargs) # Trim any leading/trailing whitespace
|
|
if [[ -n "$PDB" ]]; then
|
|
echo "Gathering datafile usage for PDB: $PDB..." >> "$OUTPUT_FILE"
|
|
run_query "$PDB"
|
|
fi
|
|
done < "$PDB_LIST_FILE"
|
|
else
|
|
echo "No PDBs found or could not access v\$pdbs view" >> "$OUTPUT_FILE"
|
|
fi
|
|
|
|
# Clean up the temporary file
|
|
rm -f "$PDB_LIST_FILE"
|