Saturday, 15 February 2020

Tablespace monitoring script

Below script monitors and send mail if TBS is reached 80%. this is generic script works for all the databases in one server.
Note: it will not take undo tablespace into account.
you supposed to create directory /u01/oracle/admin/output to  create tbs_mon.out file   

#!/bin/bash
OUTPUT_LOC=/u01/oracle/admin/output
DB_LIST=`egrep -i ":Y|:N" /etc/oratab | cut -d":" -f1 | grep -v "\#" | grep -v "\*"|grep -v ASM`
for DB in $DB_LIST ; do
echo "HOST: `hostname`  DB Name: $DB" > $OUTPUT_LOC/tbs_mon.out
export ORACLE_SID=$DB
export ORACLE_HOME=`egrep -i ":Y|:N" /etc/oratab |grep $ORACLE_SID| cut -d":" -f2 | grep -v "\#" | grep -v "\*"`
export PATH=$ORACLE_HOME/bin:$PATH
TBS=`sqlplus -s "/ as sysdba" <<EOF
set heading off feedback off verify off
select tablespace_name from dba_tablespaces where contents != 'UNDO';
exit;
EOF`
for i in $TBS;
do
if [ ! -z "$i" ]
then
TBS_SPACE=`sqlplus -s "/ as sysdba" <<EOF
set pagesize 200
set linesize 121
set heading off feedback off verify off
SELECT tablespace_name,
ROUND(SUM(max_gb) - (SUM(total_gb)-SUM(free_gb))) FREE_SPACE_GB,
ROUND(SUM(max_gb)) MAX_SZ_GB, ROUND((SUM(total_gb)-SUM(free_gb))/SUM(max_gb)*100) PCT_FULL
FROM (
SELECT tablespace_name, SUM(bytes)/1024/1024/1024 FREE_GB,
0 TOTAL_GB, 0 MAX_GB
FROM dba_free_space
GROUP BY tablespace_name
UNION
SELECT tablespace_name, 0 CURRENT_GB,
SUM(bytes)/1024/1024/1024 TOTAL_GB,
SUM(DECODE(maxbytes,0,bytes, maxbytes))/1024/1024/1024 MAX_GB
FROM dba_data_files
GROUP BY tablespace_name) where tablespace_name='$i'
GROUP BY tablespace_name;
exit;
EOF`
TBS_NAME="`echo $TBS_SPACE|awk '{ print $1}'`"
PCT_FULL="`echo $TBS_SPACE|awk '{ print $4}'`"
if [ ! -z $PCT_FULL ]
then
if [ $PCT_FULL -ge 80 ]
then
echo "TBS:" $i "  PCT_FULL: " $PCT_FULL >>$OUTPUT_LOC/tbs_mon.out
fi
fi
fi
done
CHECK_FILE=`grep -i PCT_FULL $OUTPUT_LOC/tbs_mon.out`

if [ ! -z  "$CHECK_FILE" ]
then
cat -v $OUTPUT_LOC/tbs_mon.out | mail -r "sender@company.com" -s "`hostname`:oracle TBS utilization in critical" "whoever@company.com whoever2@company.com"
fi
done

1 comment:

  1. Please do you have a similar scipt just for UNDO and Temp tablespace?

    ReplyDelete