Shell Script
Shell Script
TO AUTOMATE PROCESSES
cat gg_alert.sh
#!/bin/bash
EMAIL_LIST="[email protected]"
OIFS=$IFS
IFS="
"
NIFS=$IFS
function status {
OUTPUT=`$GG_HOME/ggsci << EOF
info all
exit
EOF`
}
function alert {
for line in $OUTPUT
do
if [[ $(echo "${line}"|egrep 'STOP|ABEND' >/dev/null;echo $?) = 0 ]]
then
GNAME=$(echo "${line}" | awk -F" " '{print $3}')
GSTAT=$(echo "${line}" | awk -F" " '{print $2}')
GTYPE=$(echo "${line}" | awk -F" " '{print $1}')
case $GTYPE in
"MANAGER")
cat $GG_HOME/dirrpt/MGR.rpt | mailx -s "${HOSTNAME} - GoldenGate ${GTYPE}
${GSTAT}" $NOTIFY ;;
"EXTRACT"|"REPLICAT")
cat $GG_HOME/dirrpt/"${GNAME}".rpt |mailx -s "${HOSTNAME} - GoldenGate
${GTYPE} ${GNAME} ${GSTAT}" $EMAIL_LIST ;;
esac
fi
done
}
export GG_HOME=/goldengate/install/software/gghome_1
export ORACLE_HOME=/oracle/app/oracle/product/12.1.0/db_1
export LD_LIBRARY_PATH=$ORACLE_HOME/lib
status
alert
SCRIPT PREPARATION:
cat /home/oracle/dgmgrl_standby_lag.sh
#!/bin/bash
export ORACLE_HOME=/oracle/app/oracle/product/12.1.0/dbhome_1
export ORACLE_SID=primdb
export PATH=$ORACLE_HOME/bin:$PATH
echo -
DB_DG_DATABASE.log
cat /home/oracle/DB_DG_DATABASE.log >
FILTERED_DB_DG_DATABASE.log
time_value=`cut - -f 14 FILTERED_DB_DG_DATABASE.log`
time_param=`cut - -f 15 FILTERED_DB_DG_DATABASE.log`
-ge 1 ]]
then
mailx - [email protected]<DB_DG_DATABASE.log
else
-ge 30 ]]
then
mailx -
[email protected]<DB_DG_DATABASE.log
else
if -ge 1 ]]
then
mailx - [email protected]
<DB_DG_DATABASE.log
fi
fi
fi
If the requirement is to delete archive log backups automatically (without taking backup), then
below shell script can be configured in crontab.
cat rman_arch_del.sh
#!/bin/bash
export ORACLE_HOME=/oracle/app/oracle/product/12.1.0.2.0
export ORACLE_SID=PARIS12C
export PATH=$ORACLE_HOME/bin:$PATH
delBackup () {
rman log=/home/oracle/arch_del.log << EOF
connect target /
-
CROSSCHECK ARCHIVELOG ALL;
DELETE EXPIRED ARCHIVELOG ALL;
exit
EOF
}
# Main
delBackup
Now configure in crontab:
Below is the shell script, to be configured in crontab, which will send mail incase of blocking
session observed in the database .
In the mail body it will contain the blocking sessions details also.
1. Prepare the blocker.sql file.[ for blocking sessions more than 10 seconds)
2. Shell script.(/home/oracle/monitor/blocker.sh )
export ORACLE_HOME=/oracle/app/oracle/product/12.1.0/dbhome_1
export ORACLE_SID=ORCL
export PATH=$ORACLE_HOME/bin:$PATH
logfile=/home/oracle/monitor/block_alert.log
sqlplus -s "/as sysdba" > /dev/null << EOF
spool $logfile
@/home/oracle/monitor/blocker.sql
spool off
exit
EOF
count=`cat $logfile|wc -l`
if [ $count -ge 1 ];
then mailx -s "BLOCKING SESSION REPORTED IN PROD DB ( > 10 SEC) "
[email protected] < $logfile
fi
cat /export/home/oracle/asm_dg.sh
export ORACLE_HOME=/oracle/app/oracle/product/12.1.0.2/dbhome_1
export ORACLE_SID=PRODDB1
export PATH=$ORACLE_HOME/bin:$PATH
logfile=/export/home/oracle/asm_dg.log
sqlplus -s "/as sysdba" > /dev/null << EOF spool $logfile
SET LINESIZE 150
SET PAGESIZE 9999
SET VERIFY off
COLUMN group_name
FORMAT a25 HEAD 'DISKGROUP_NAME'
COLUMN state FORMAT a11 HEAD 'STATE'
COLUMN type FORMAT a6 HEAD 'TYPE'
COLUMN total_mb FORMAT 999,999,999 HEAD 'TOTAL SIZE(GB)'
COLUMN free_mb FORMAT 999,999,999 HEAD 'FREE SIZE (GB)'
COLUMN used_mb FORMAT 999,999,999 HEAD 'USED SIZE (GB)'
COLUMN pct_used FORMAT 999.99 HEAD 'PERCENTAGE USED'
3. Configure in crontab:
0,15,30,45 * * * * /export/home/oracle/asm_dg.sh
Configure a shell script in crontab, that will send alert to DB support Team in case of any
invalid login attempts in the database.
Audit succeeded.
cat /export/home/oracle/invalid_log.sh
export ORACLE_HOME=/oracle/app/oracle/product/12.1.0/dbhome_1
export ORACLE_SID=SBIP18DB
export PATH=$ORACLE_HOME/bin:$PATH
logfile=/export/home/oracle/test.log
sqlplus -s "/as sysdba" > /dev/null << EOF
spool $logfile
set pagesize 1299
set lines 299
col username for a15
col userhost for a13
col timestamp for a39
col terminal for a23
SELECT username,userhost,terminal,to_char(timestamp,'DD/MM/YY HH24:MI:SS' )
"TIMESTAMP" ,
CASE
when returncode=1017 then 'INVALID-attempt'
when returncode=28000 then 'account locked'
end "FAILED LOGIN ACTION"
FROM dba_audit_session where timestamp > sysdate-1/9and returncode in
(1017,28000);
spool off
exit
EOF
count=`cat $logfile|wc -l`
#echo $count
if [ $count -ge 4 ];
then
mailx -s "INVALID ATTEMPS IN DB " [email protected] < $logfile
fi
4. Configure in crontab:
0,15,30,45 * * * * /export/home/oracle/invalid_log.sh
Below is script to notification when a mount point or filesystem usage crosses a threshold value.
#!/bin/sh
do
if [ $val -ge 90 ]
then
echo "The $fs usage high $val% \n \n \n `df -h $fs`" | mailx -s "Filesystem
$fs Usage high on Server `hostname`" [email protected]
fi
done
Put in crontab:
00 * * * * /usr/local/scripts/diskalert.sh
zpool list | awk '{print $5}'| grep -v CAP | cut -d% -f1| while read val
do
if [ $val -ge 80 ]
then
echo "The $fs usage high $val% \n \n \n `df -h $fs`" | mailx -s "Filesystem
$fs Usage high on Server `hostname`" [email protected]
fi
done
00 * * * * /usr/local/scripts/zpoolusage.sh
Alert log size will grow in Oracle database from day to day. So for housekeeping, we need to
move the existing alert log to a backup location and compress there. Upon moving the alert log,
the database will create a fresh alert log automatically.
We need to define the ORACLE_HOME in the script. and ORACLE_SID will be passed as an
argument while running the script.
# $Header: rotatealertlog.sh
#
*============================================================================
========+
# | AUTHOR : DBACLASS SUPPORT TEAM
# | |
#
+============================================================================
========+
# |
#!/bin/bash
echo ========================
echo Set Oracle Database Env
echo ========================
echo =======
echo Extract Alert log location
echo =======
export VAL_DUMP=$(${ORACLE_HOME}/bin/sqlplus -S /nolog <<EOF
conn /as sysdba
set pages 0 feedback off;
prompt
SELECT value from v\$parameter where NAME='core_dump_dest';
exit;
EOF
)
export LOCATION=`echo ${VAL_DUMP} | perl -lpe'$_ = reverse' |awk '{print
$1}'|perl -lpe'$_ = reverse'`
export ALERTDB=${LOCATION}/alert_$ORACLE_SID.log
export ELOG=$( echo ${ALERTDB} | sed s/cdump/trace/)
echo =======
echo Compress current
echo =======
if [ -e "$ELOG" ] ; then
mv ${ELOG} ${ELOG}_${TO_DATE};
gzip ${ELOG}_${TO_DATE};
> ${ELOG}
else
echo not found
fi
exit
00 22 * * 5 /u01/app/oracle/dbscripts/rotatealertlog.sh PRODDB
Below script can be configured in crontab to send a notification to the support DBAs in case
tablespace usage crosses a threshold.
1. First, make the below .sql file, which will be used inside the shell script.
In this script we have defined the threshold as 90%. You can change it as per your requirement.
cat /export/home/oracle/Housekeeping/scripts/tablespace_alert.sql
set feedback off
set pagesize 70;
set linesize 2000
set head on
COLUMN Tablespace format a25 heading 'Tablespace Name'
COLUMN autoextensible format a11 heading 'AutoExtend'
COLUMN files_in_tablespace format 999 heading 'Files'
COLUMN total_tablespace_space format 99999999 heading 'TotalSpace'
COLUMN total_used_space format 99999999 heading 'UsedSpace'
COLUMN total_tablespace_free_space format 99999999 heading 'FreeSpace'
COLUMN total_used_pct format 9999 heading '%Used'
COLUMN total_free_pct format 9999 heading '%Free'
COLUMN max_size_of_tablespace format 99999999 heading 'ExtendUpto'
COLUM total_auto_used_pct format 999.99 heading 'Max%Used'
COLUMN total_auto_free_pct format 999.99 heading 'Max%Free'
WITH tbs_auto AS
(SELECT DISTINCT tablespace_name, autoextensible
FROM dba_data_files
WHERE autoextensible = 'YES'),
files AS
(SELECT tablespace_name, COUNT (*) tbs_files,
SUM (BYTES/1024/1024) total_tbs_bytes
FROM dba_data_files
GROUP BY tablespace_name),
fragments AS
(SELECT tablespace_name, COUNT (*) tbs_fragments,
SUM (BYTES)/1024/1024 total_tbs_free_bytes,
MAX (BYTES)/1024/1024 max_free_chunk_bytes
FROM dba_free_space
GROUP BY tablespace_name),
AUTOEXTEND AS
(SELECT tablespace_name, SUM (size_to_grow) total_growth_tbs
FROM (SELECT tablespace_name, SUM (maxbytes)/1024/1024
size_to_grow
FROM dba_data_files
WHERE autoextensible = 'YES'
GROUP BY tablespace_name
UNION
SELECT tablespace_name, SUM (BYTES)/1024/1024 size_to_grow
FROM dba_data_files
WHERE autoextensible = 'NO'
GROUP BY tablespace_name)
GROUP BY tablespace_name)
SELECT c.instance_name,a.tablespace_name Tablespace,
CASE tbs_auto.autoextensible
WHEN 'YES'
THEN 'YES'
ELSE 'NO'
END AS autoextensible,
files.tbs_files files_in_tablespace,
files.total_tbs_bytes total_tablespace_space,
(files.total_tbs_bytes - fragments.total_tbs_free_bytes
) total_used_space,
fragments.total_tbs_free_bytes total_tablespace_free_space,
round(( ( (files.total_tbs_bytes - fragments.total_tbs_free_bytes)
/ files.total_tbs_bytes
)
* 100
)) total_used_pct,
round(((fragments.total_tbs_free_bytes / files.total_tbs_bytes) * 100
)) total_free_pct
FROM dba_tablespaces a,v$instance c , files, fragments, AUTOEXTEND,
tbs_auto
WHERE a.tablespace_name = files.tablespace_name
AND a.tablespace_name = fragments.tablespace_name
AND a.tablespace_name = AUTOEXTEND.tablespace_name
AND a.tablespace_name = tbs_auto.tablespace_name(+)
and (((files.total_tbs_bytes - fragments.total_tbs_free_bytes)/
files.total_tbs_bytes))* 100 > 90
order by total_free_pct;
At the beginning of the script, we need to define the env variables like ORACLE_HOME,
PATCH, LD_LIBRARY_PATH, ORACLE_SID.
cat /export/home/oracle/Housekeeping/scripts/tablespace_threshold.ksh
#!/bin/sh
export ORACLE_HOME=/u01/app/oracle/product/12.1.0/dbhome_1
export PATH=$ORACLE_HOME/bin:$PATH
export LD_LIBRARY_PATH=$ORACLE_HOME/lib
export ORACLE_SID=PRODDB
cd /export/home/oracle/Housekeeping/scripts
logfile=/export/home/oracle/Housekeeping/scripts/Tablespace_alert.log
cnt1=`ps -ef|grep pmon|grep $ORACLE_SID|wc -l`
if [ $cnt1 -eq 1 ];
then
sqlplus -s "/as sysdba" > /dev/null << EOF
spool $logfile
@/export/home/oracle/Housekeeping/scripts/tablespace_alert.sql
spool off
exit
EOF
# If there are more then these two lines in the output file, mail it.
count=`cat $logfile|wc -l`
#echo $count
if [ $count -ge 4 ];
then
mailx -s "TABLESPACE ALERT FOR PROD DB " [email protected] <$logfile
fi
fi
Configure a shell script to monitor alert log for all the databases on a server once in every 15
min.And in the case of any ORA- error mail to the DBA TEAM.
Below script is prepared using the ADRCI utility of oracle 11g. It will monitor alert log for all
the databases having same oracle base.
SCRIPT:(Adrci_alert_log.ksh)
#######################################################
###### ALERT LOG CHECKING VIA ADRCI
###### Author - DBACLASS ADMIN
#######################################################
LOG_DIR=/export/home/oracle/Housekeeping/logs/alert_log_check_daily.txt
adrci_homes=( $(adrci exec="show homes" | egrep -e rdbms ))
echo '##############################' > $LOG_DIR
echo '###########################ALERT LOG OUTPUT FOR LAST 15 MINUTES
###########################' >> $LOG_DIR
echo '##############################' >> $LOG_DIR
done
num_errors=`grep -c 'ORA' $LOG_DIR`
if [ $num_errors != 0 ]
then
mailx -s "ORA- error found in alert Log of the server " [email protected]
<$LOG_DIR
fi
0,15,30,45 * * * *
/export/home/oracle/Housekeeping/scripts/Adrci_alert_log.ksh >
/export/home/oracle/Housekeeping/logs/error_alert.log 2>&1