Oracle Database Experts

By Charles Kim

August 27th, 2010

View your alert.log file in Oracle Database 11g

function ALERT {
export DB1=$ORACLE_SID
export DB=$(echo $ORACLE_SID|tr '[A-Z]' '[a-z]' |sed -e '$s/.$//')
export ASMDB=rdbms
if [ "$DB" = "+asm" ]; then export ASMDB=asm ; fi
if [ "$DB" = "+as" ]; then export ASMDB=asm ; export DB="+asm"; fi
[ -d "$ORACLE_BASE/diag/$ASMDB/$DB/$ORACLE_SID/trace" ] && view $ORACLE_BASE/diag/$ASMDB/$DB/$ORACLE_SID/trace/alert_$ORACLE_SID.log ||view $ORACLE_BASE/diag/$ASMDB/$DB1/$ORACLE_SID/trace/alert_$ORACLE_SID.log
}

Notes:
* Works in both RAC and non-RAC.
* Works for ASM and non-ASM instances.
* The ALERT function will open the alert log associated with your current ORACLE_SID

** Requirements:
1. ORACLE_BASE must already be set
2. Add this script to your .profile or .bash_profile depending on the shell you are using

Posted by Charles Kim, Oracle ACE Director

August 27th, 2010

Change directory to the trace directory in Oracle Database 11g

function trace {
# DB = The sed command strips off the last character of ORACLE_SID off
export DB1=$ORACLE_SID
export DB=$(echo $ORACLE_SID|tr '[A-Z]' '[a-z]' |sed -e '$s/.$//')
export ASMDB=rdbms
if [ "$DB" = "+asm" ]; then export ASMDB=asm ; fi
if [ "$DB" = "+as" ]; then export ASMDB=asm ; fi
[ -d "$ORACLE_BASE/diag/$ASMDB/$DB/$ORACLE_SID/trace" ] && cd $ORACLE_BASE/diag/$ASMDB/$DB/$ORACLE_SID/trace || cd $ORACLE_BASE/diag/$ASMDB/$DB1/$ORACLE_SID/trace
}

Notes:
* Works in both RAC and non-RAC.
* Works for ASM and non-ASM instances.
* The trace function will take you to the directory where all the trace files are located including the alert log

** Requirements:
1. ORACLE_BASE must already be set
2. Add this script to your .profile or .bash_profile depending on the shell you are using

Posted by Charles Kim, Oracle ACE Director

December 19th, 2009

One Liner to View the Inventory XML file

Solaris:

cat /var/opt/oracle/oraInst.loc |grep inventory_loc |cut -d= -f2 |xargs -I {} -t cat {}/ContentsXML/inventory.xml

Linux and other Unix OS:

cat /etc/oraInst.loc |grep inventory_loc |cut -d= -f2 |xargs -I {} -t cat {}/ContentsXML/inventory.xml

Notes:
1. The -t option tells it to display out the syntax before execution
2. The -I is for Insert mode. utility is executed for each
line from standard input, taking the entire
line as a single argument, inserting it in
argument s for each occurrence of replstr.

Posted by Charles Kim, Oracle ACE Director

January 8th, 2008

Tablespace Monitoring Script

Imagine a comprehensive monitoring script that will allow you to customize how many times an alert will be sent. Not only does it tell you the alert but it also informs you the incident number of the alert. Here’s an example of the subject of the message that you will receive:

Tablespace Threshold:DB:WARNING:rac01.dbaexpert.com:gc::Alert#: 5 for Tbsp_Free < 15

And here’s an example of the message detail:

TABLESPACE_NAME %_FREE
—————————— ——
DATA 14.8
SYSAUX 15.4

#!/bin/ksh
######################################################################################################
#
# TITLE……: tbspmon.ksh
# LOCATION…: /apps/oracle/general/sh
# PARAMETERS : PAGER Threshold and Database
#
# USAGE……: tbspmon.ksh $PAGER_THRESHOLD $DB
# OUTPUTS….: List of databases that have tablespaces that are about to run out of space
# DESCRIPTION
# FUNCTION: 1) Runs SQL code that saves info for tablespaces that have less free space than the
# specified have less free space than the specified pager threshold.
# 2) Sends appropriate e-mail and pager notifications, for tablespaces that have less free
# space than the specified e-mail threshold and pager threshold respectively.
# NOTES……: This function was written for the Korn shell and may have
# minor portability problems between different platforms.
# AUTHOR…..:
#
# Modification
#-Date———-Author & Description——————————————————————
#
#
######################################################################################################

export PAGER_THRESHOLD=$1
export DB=$2
FN=`echo $0 | sed s/\.*[/]//`
MAX_NUMBER_ALERTS=5
TMPDIR=/tmp

TBSP_CONFIG=$(echo $0 | sed s/\.*[/]// |awk -F”.” {‘print $1 “.exceptions”‘})


#———————————————
# Parameters from the ${0}.conf file
# And define error and log files
#———————————————
export LOGDIR=${TMPDIR}
export LOGFILE=${TMPDIR}/${DB}_tbsp_free_email_notif.alert
echo $LOGFILE

if [ -f "$LOGFILE" ]; then
rm $LOGFILE
fi

function show_debug_parameters
{
echo “Max Number of Alerts: $MAX_NUMBER_ALERTS”
echo “Log Directory: $LOGDIR”
echo $COUNTER
}

function exam_tablespace_threshold
{

print “TBSP CONFIG: $TBSP_CONFIG”
# — Generate Ignore List of database/tablespaces from being monitored
cat $SH/$TBSP_CONFIG |egrep -v “\^#|grep”| grep $DB |awk -F”:” {‘print $2′} |read IGNORE_LIST

[ "$IGNORE_LIST" = "" ] && export IGNORE_LIST=”‘SOME_BOGUS_TABLESPACE’”
print “IGNORE LIST: $IGNORE_LIST”

#export TNS_ADMIN=/var/opt/oracle/dba
export PW=$(cat $SH/.rodba.pw)
export CONNECT_STRING=rodba/${PW}@${DB}
echo “Connect String: $CONNECT_STRING”

sqlplus -s $CONNECT_STRING 2>&1 <<__ENDSQL
whenever sqlerror exit sql.sqlcode;

set pages 60
set lines 90
set verif off
set trims on

col pct_free for 999.9 hea ‘%_FREE’
define ORA_SID=’$DB’
define PAGE_THRESHOLD=$PAGER_THRESHOLD
define LOG_DIR=’$LOGDIR’


spool $LOGFILE
select tablespace_name, pct_free
from (
SELECT b.tablespace_name, b.tablespace_size_mb,
sum(nvl(fs.bytes,0))/1024/1024 free_size_mb,
(sum(nvl(fs.bytes,0))/1024/1024/b.tablespace_size_mb *100) pct_free
FROM dba_free_space fs,
(SELECT tablespace_name, sum(bytes)/1024/1024 tablespace_size_mb
FROM dba_data_files
GROUP BY tablespace_name
)b
where fs.tablespace_name = b.tablespace_name
and b.tablespace_name NOT IN (SELECT tablespace_name FROM dba_tablespaces WHERE status=’READ ONLY’)
and b.tablespace_name NOT IN (${IGNORE_LIST})
group by b.tablespace_name, b.tablespace_size_mb
) tbsp_pct_free_v
WHERE PCT_FREE < &PAGE_THRESHOLD
ORDER BY pct_free;
spool off

set pages 14
set lines 80
set verif on
__ENDSQL

grep “TABLESPACE_” $LOGFILE

if [ "$?" -eq 0 ]
then
(( EMAIL_COUNTER = ${COUNTER} + 1 ))

# —————————————————————
# Set ALERT_COUNTER to be passed into alert_notification.ksh
# —————————————————————
export ALERT_COUNTER=$EMAIL_COUNTER

print “Sending Alert ${FN}_PG `hostname` $DB Alert#: $EMAIL_COUNTER for Tbsp_Free”
$SH/alert_notification.ksh ${FN}_PG `hostname` $DB “” “Alert#: $EMAIL_COUNTER for Tbsp_Free < $PAGER_THRESHOLD” $LOGFILE

# Increment the counter by one and append it to the LOGFILE filename
(( COUNTER = ${COUNTER} + 1 ))
echo $COUNTER
mv ${LOGFILE} ${LOGFILE}.${COUNTER}

fi
}

#+++++++++++++++++++++++++++++++++++++++++++++++++++++
# MAIN LOGIC
#+++++++++++++++++++++++++++++++++++++++++++++++++++++

#——————————————–
# Count the number of error files (LOGFILE)
# in the /tmp directory
#——————————————–
# if [ -f ${LOGFILE}.* ]; then
export COUNTER=`ls ${LOGFILE}.* |wc -l`
echo “Counted x number of alert files in the $TMPDIR: $COUNTER”
# else
# COUNTER=0
# echo “Counter is set to 0 because there is no alert files”
# fi

show_debug_parameters;

#——————————————————
# If # of error files on disk is greater than
# $MAX_NUMBER_ALERTS, then skip to the next database
#——————————————————
echo “Current Counter: $COUNTER”
echo “Max number before stopping: $MAX_NUMBER_ALERTS”
if [ "${COUNTER}" -ge "$MAX_NUMBER_ALERTS" ]; then
echo “Exceeded Max number of Alerts: $MAX_NUMBER_ALERTS”
exit 1;
else
exam_tablespace_threshold;
fi


Technorati :

January 7th, 2008

Oracle Database Related Daily Cleanup Job

#!/bin/ksh
# Script: oracle_cleanup.daily.ksh
# Purpose: remove unneeded files
# Note:
#—————————————————————
# Modifications
# Date By Change Made
# ———- ————– ——————————–
#
#—————————————————————
# This script runs weekly on an UNIX system and performs a cleanup on
# Oracle files and/or logs that accumulate on a UNIX system. In some cases,
# the files are truncated; in other cases they are simply removed.
#
# Any and all changes made to this script should be considered very
# carefully.

. $HOME/.ORACLE_BASE
export PATH=/usr/local/bin:/usr/sbin:$ORACLE_BASE/general/sh:$PATH

# ****************************************************************************
# * DEFINE FULL QUALIFIED UNIX SYSTEM COMMANDS *
# ****************************************************************************
CHMOD=/bin/chmod
CHOWN=/bin/chown
CP=/bin/cp
DATE=/bin/date
TOUCH=/bin/touch
FIND=/usr/bin/find
RM=/bin/rm
TAR=/bin/tar

# ****************************************************************************
# * DEFINE DIRECTORIES FOR SEARCHES *
# ****************************************************************************
if [ "$ORACLE_BASE" = "" ]; then
echo “ORACLE BASE Directory not defined”
exit 1
fi

TRACE_DIR=$ORACLE_BASE/admin
AUD_DIR=$ORACLE_BASE/admin

# ****************************************************************************
# * DEFINE NULL DEVICE FOR TRUNCATING FILES
# ****************************************************************************

NULL=/dev/null

# ****************************************************************************
# * DEFINE CURRENT DAY DATESTAMP (MMDDYY) TO USE IN LOG RENAMING *
# ****************************************************************************

DATESTAMP=`$DATE +%m%d%y`

# ****************************************************************************
# * FIND and delete *.trc files more than 5 days old. *
# ****************************************************************************

$FIND $TRACE_DIR -name ‘*.trc’ -type f -mtime +5 -exec $RM {} \;

# ****************************************************************************
# * FIND and delete *.aud files more than 5 days old. *
# ****************************************************************************

$FIND $AUD_DIR -name ‘*.aud’ -type f -mtime +5 -exec $RM {} \;

# ****************************************************************************
# * FIND and delete files owned by the dba group more than 5 days old. *
# ****************************************************************************

$FIND /tmp -group dba -type f -mtime +5 -exec rm -f {} \;
$FIND /tmp/dba -group dba -type f -mtime +5 -exec rm -f {} \;

# ****************************************************************************
# * FIND and delete all core files owned by oracle. *
# ****************************************************************************

$FIND $ORACLE_BASE -name ‘core’ -type f -mtime +3 -exec $RM {} \;

# ****************************************************************************
# * FIND and delete all nohup files owned by oracle. *
# ****************************************************************************

$FIND $ORACLE_BASE -name ‘nohup.out’ -type f -mtime +1 -exec $RM {} \;

# ****************************************************************************
# * Removing uncleared events
# ****************************************************************************
print “Clearing Events …”;ls -l /tmp/*.alert.*; rm /tmp/*.alert.*

exit 0

January 2nd, 2008

Using rdist to synchronize file systems

rdist versions 6.1 and greater can use ssh instead of rsh using the -P option:

-P <transport-path>
Set the path to the transport command to be used. This is normally rsh(1c) but can be any other program – such as
ssh(1) – which understands rsh(1c) command line syntax and which provides an appropriate connection to the remote
host. The transport-path may be a colon seperated list of possible pathnames. In thi case, the first component
of the path to exist is used. i.e. /usr/bin/ssh, /usr/ucb/rsh:/usr/bin/remsh, /usr/bsd/rsh.

Here is a sample rdist configuration file (rdist.conf) to synchronize the SQL and shell scripts to remote servers:

HOSTS = ( rac1.dbaexpert.com rac2.dbaexpert.com rac3.dbaexpert.com rac11.dbaexpert.com rac12.dbaexpert.com rac13.dbaexpert.com )
FILES = ( /u01/app/oracle/.profile /u01/app/oracle/.aliases /u01/app/oracle/.rhosts /u01/app/oracle/general/sql /u01/app/oracle/gen)
${FILES} -> ${HOSTS}
install -R;

# The -R option will remove files on the remote file system if the file is removed from the local file system

To manually synchronize the file systems for all of the 6 servers listed above, you would use rdist with the -f option and pass in the rdist.conf file.

Posted by: Charles Kim @ DBAExpert.com

November 9th, 2007

Use uuencode from unix to attach files to mail recipients

The rpm that is required to install uucp in Redhat is sharutils:

root# rpm -ihv sharutils-4.2.1-22.2.x86_64.rpm
Preparing… ########################################### [100%]
1:sharutils ########################################### [100%]

Once you have uuencode installed and configured, you can attach file using the example code below:

uuencode rman_backup.ksh rman_backup.ksh |mailx -s “Attached rman backup shell script” oradba@dbaexpert.com

Posted by: Charles Kim @ DBAExpert.com

|