0% found this document useful (0 votes)
46 views4 pages

Dba Tool Tbs GB Remain v7.sql

This document contains the source code for a stored procedure called DBA_TOOL_TBS_GB_REMAIN_V7.5.1 that checks tablespace space usage and adds datafiles if needed. It gets the disk group name, checks free space, adds datafiles if space is low, and rebuilds tablespace bitmaps. The procedure logs output and raises warnings if file counts or free space are too high/low. It has been updated over multiple versions for bug fixes and compatibility.

Uploaded by

Dilip Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views4 pages

Dba Tool Tbs GB Remain v7.sql

This document contains the source code for a stored procedure called DBA_TOOL_TBS_GB_REMAIN_V7.5.1 that checks tablespace space usage and adds datafiles if needed. It gets the disk group name, checks free space, adds datafiles if space is low, and rebuilds tablespace bitmaps. The procedure logs output and raises warnings if file counts or free space are too high/low. It has been updated over multiple versions for bug fixes and compatibility.

Uploaded by

Dilip Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

/*

###################################################################################
####################################
#
# NAME: dba_tool_tbs_gb_remain_v7.sql
#
# AUTHOR: CernerWorks Database Services Team
#
# SUPPORT: [email protected]
#
# DESCRIPTION: The Script create the procedure dba_tool_tbs_gb_remain_v7
# Wiki WI: TBD
# JIRA: TBD
#
# USER: Oracle
#

###################################################################################
####################################
#
# ASSUMPTIONS:
#

###################################################################################
####################################
#
# AUTHOR DATE COMMENTS
# VER 1.0.0 pa026907 02/10/2021 - Script is created.
# VER 2.0.0 pa026907 05/10/2021 - Updated v_dg to upperase.
# VER 3.0.0 pa026907 06/24/2021 - Bug Fix to account for
freespace
# VER 4.0.0 pa026907 07/12/2021 - Updated Cursor Query
# VER 5.0.0 gc027765 07/23/2021 - reverting to VER 2.0.0 -
previous to accounting for free space
# - reverting using May 10th git
commit - 56efa66f5e
# VER 5.1.0 gc027765 03/20/2023 - adding excluding TS that
reaches file limit (1023) + additional
# - date/timestamp reporting.
# - see:
https://jira3.cerner.com/browse/CWXTIMIG-229367

###################################################################################
####################################
*/
create or replace PROCEDURE DBA_TOOL_TBS_GB_REMAIN_V7
AS
vSql1 VARCHAR2(2000);
vDG VARCHAR2(2000);
vMax VARCHAR2(2000);
vMsg1 VARCHAR2(2000);
vFreespace NUMBER;
vFC NUMBER;
GbRemain NUMBER;
AlarmPoint NUMBER := 2;
WarnPoint NUMBER := 20;
TbsWarning NUMBER;
FCWarning NUMBER := 1000;
theUser VARCHAR2(100);
containerName VARCHAR2(100);
db_version NUMBER;
max_datafiles NUMBER DEFAULT 1023;

CURSOR c1(num_datafiles NUMBER) IS


SELECT df.tablespace_name TABLESPACE,
COUNT(*) filecount,
ROUND (SUM(df.bytes/1024/1024/1024)) allocated,
ROUND (SUM(NVL(NULLIF(df.maxbytes,0),df.bytes)/1024/1024/1024)) maxsize
FROM sys.dba_data_files df,
sys.dba_tablespaces tbs
WHERE df.tablespace_name = tbs.tablespace_name
AND df.status = 'AVAILABLE'
AND tbs.status <>'READ ONLY'
AND tbs.contents='PERMANENT'
GROUP BY df.tablespace_name
HAVING count(*) != num_datafiles;

BEGIN
----------------------------------------------------------------------------------
--DBA_TOOL_TBS_GB_REMAIN_V7 - VERSION 5.1
----------------------------------------------------------------------------------

SELECT SUBSTR(VERSION,1,2) INTO db_version FROM PRODUCT_COMPONENT_VERSION WHERE


product LIKE 'Oracle%';
theUser := USER;
SYS.DBMS_SYSTEM.ksdwrt(2, to_char(sysdate,'DD-MON-YYYY HH24:MI:SS')|| ' -->
DBA_TOOL_TBS_GB_REMAIN_V7.5.1 start');

IF (db_version >= 12) THEN


SELECT sys_context('USERENV','CON_NAME') into containerName from dual;
SYS.DBMS_SYSTEM.ksdwrt(2, to_char(sysdate,'DD-MON-YYYY HH24:MI:SS')|| ' -->
DBA_TOOL_TBS_GB_REMAIN_V7.5.1 running in container: '||containerName);
max_datafiles := 1023;
ELSE
max_datafiles := 1022;
END IF;

FOR c1_rec IN c1(max_datafiles)


LOOP

vMax := c1_rec.maxsize;
IF vMax = 0 THEN
vFreespace := 0;
ELSE
vFreespace := (c1_rec.maxsize - c1_rec.allocated);
END IF;

vFC := c1_rec.filecount;
IF vFC > 256 THEN
TbsWarning := (WarnPoint * 5);
ELSE
TbsWarning := (WarnPoint);
END IF;

IF vFreespace < TbsWarning THEN

IF vFC >= FCWarning THEN


IF vFC = max_datafiles THEN
vMsg1 := 'ORA-01686: max # of files ('||max_datafiles||')
reached for the tablespace ' || c1_rec.tablespace ;
ELSE
vMsg1 := 'CWX-01686: nearing max # of files ('||
max_datafiles||') for the tablespace ' || c1_rec.tablespace || ', currently ' ||
vFC || ' datafiles are allocated.';
END IF;
sys.dbms_system.ksdwrt(2, to_char(sysdate,'DD-MON-YYYY
HH24:MI:SS')||' '||vMsg1);

END IF;

IF vFC != max_datafiles THEN

SYS.DBMS_SYSTEM.ksdwrt(2, to_char(sysdate,'DD-MON-YYYY
HH24:MI:SS')|| ' --> DBA_TOOL_TBS_GB_REMAIN_V7.5.1 getting DG name: '||
c1_rec.tablespace);
SELECT DISTINCT(substr(value,2)) into vDG FROM v$parameter WHERE
NAME like 'db_create_file_dest';
SYS.DBMS_SYSTEM.ksdwrt(2, to_char(sysdate,'DD-MON-YYYY
HH24:MI:SS')|| ' --> DBA_TOOL_TBS_GB_REMAIN_V7.5.1 DG name is: '||vDG);
SYS.DBMS_SYSTEM.ksdwrt(2, to_char(sysdate,'DD-MON-YYYY
HH24:MI:SS')|| ' --> DBA_TOOL_TBS_GB_REMAIN_V7.5.1 checking DG free space: '||
c1_rec.tablespace);
SELECT round(free_mb/1024) INTO GbRemain FROM V$ASM_DISKGROUP WHERE
UPPER(name) = UPPER(vDG);
vSql1 := 'alter tablespace ' || c1_rec.tablespace || ' add datafile
''+' || vDG || ''' size 100M autoextend on MAXSIZE UNLIMITED';

IF vFreespace <= AlarmPoint THEN


vMsg1 := 'CWX-01632: PLEASE REVIEW SIGNIFICANT SPACE
CONSUMPTION. tablespace' || c1_rec.tablespace || ' has ' || vFreespace || ' GBs
remaining. Diskgroup ' || vDG || ' has ' || GbRemain || ' GBs remaining. Executing
the following: ' || vSql1;
ELSE
vMsg1 := 'CWX-01632: Tablespace' || c1_rec.tablespace || ' has
' || vFreespace || ' GBs remaining. Diskgroup ' || vDG || ' has ' || GbRemain || '
GBs remaining. Executing the following: ' || vSql1;
END IF;

sys.dbms_system.ksdwrt(2, to_char(sysdate,'DD-MON-YYYY
HH24:MI:SS')||' '||vMsg1);
EXECUTE IMMEDIATE vSql1;
vSql1 := 'exec dbms_space_admin.tablespace_rebuild_bitmaps(''' ||
c1_rec.tablespace || ''')';
vMsg1 := 'Executing the following tablespace bitmap rebuild
maintenance: <' || vSql1 ||'>';
sys.dbms_system.ksdwrt(2, to_char(sysdate,'DD-MON-YYYY
HH24:MI:SS')||' '||vMsg1);
dbms_space_admin.tablespace_rebuild_bitmaps(c1_rec.tablespace);

END IF;
END IF;

END LOOP;
SYS.DBMS_SYSTEM.ksdwrt(2, to_char(sysdate,'DD-MON-YYYY HH24:MI:SS')|| ' -->
DBA_TOOL_TBS_GB_REMAIN_V7.5.1 complete');
EXCEPTION
WHEN OTHERS THEN
dbms_output.put_line('DBA_TOOL_TBS_GB_REMAIN_V7.5.1 Procedure Failed.
Error: '|| SQLERRM);
END;

You might also like