12-简单日检脚本:
1° crs_stat 格式化输出改造
su - grid
mkdir /home/grid/checkRAC
cd /home/grid/checkRAC
cat >crs_stat_format.sh<<EOF
#!/bin/bash
source ~/.bash_profile
crs_stat|\\
awk -F'=' \\
'BEGIN{printf "|---------TIME--------";\\
printf "|----------------NAME---------------";\\
printf "|---------------TYPE-------------";\\
printf "|--TARGET--";\\
printf "|---------STATE--------";\\
print "|---ALERT---|";\\
}'\\
'{ if(\$1 ~ /NAME/) {name=\$2}\\
else if(\$1 ~ /TYPE/) {type=\$2}\\
else if(\$1 ~ /TARGET/){target=\$2}\\
else if(\$1 ~ /STATE/) {state=\$2;\\
if(target=="ONLINE" && state=="OFFLINE") error="Warning!";else error=""}\\
else {\\
printf("| %s | %-33s |",strftime("%Y-%m-%d_%H:%M:%S"),name);\\
printf(" %-30s | %-8s | %-20s | %9s |\n",type,target,state,error);\\
}}'\\
'END {printf "|---------------------|";\\
printf "-----------------------------------|";\\
printf "--------------------------------|";\\
printf "----------|";\\
printf "----------------------|";\\
print "-----------|";\\
}'
EOF
chmod +x crs_stat_format.sh
2° asm使用空间检查
su - grid
mkdir /home/grid/checkRAC
cd /home/grid/checkRAC
cat >checkASM.sh<<EOF
#!/bin/bash
source ~/.bash_profile
/bin/echo -e 'set linesize 150
col NAME for a15
col STATE for a15
col TYPE for a15
select NAME,STATE,TYPE,
trunc(TOTAL_MB/1024) TOTAL_GB,
trunc(FREE_MB/1024) FREE_GB,
trunc(USABLE_FILE_MB/1024) USABLE_FILE_GB,
OFFLINE_DISKS
from v\$asm_diskgroup;'|sqlplus -silent / as sysdba
EOF
chmod +x checkASM.sh
3° DBFILE、PGA、SESSION使用检查
su - oracle
mkdir /home/oracle/checkRAC
cd /home/oracle/checkRAC
cat >checkDB.sh<<EOFALL
/bin/echo -e '
set pagesize 1000
set linesize 300
set feedback off
col CUR_PGA for a10
col Name for a20
col \042ExtentManagement\042 for a9
col \042SegmentSpaceManagement\042 for a15
conn / as sysdba
SELECT /*+ first_rows */
D.TABLESPACE_NAME \042Name\042,
round(NVL(A.max_extend/1024/1024/1024,0),1) \042ExtensibleSize_G\042,
round(NVL(A.max_extend/1024/1024/1024,0),1)-
round(DECODE(D.CONTENTS,\047UNDO\047,NVL(U.BYTES,0)/1024/1024/1024,NVL(A.BYTES-NVL(F.BYTES,0),0)/1024/1024/1024),1) \042ExtensibleFreeSpace_G\042,
round(NVL(A.BYTES/1024/1024/1024,0),1) \042Size_G\042,
round(DECODE(D.CONTENTS,\047UNDO\047,NVL(U.BYTES,0)/1024/1024/1024,NVL(A.BYTES-NVL(F.BYTES,0),0)/1024/1024/1024),1) \042UsageAmount_G\042,
round(DECODE(D.CONTENTS,\047UNDO\047,NVL(U.BYTES/A.BYTES*100,0),NVL((A.BYTES-NVL(F.BYTES,0))/A.BYTES*100,0)),1) \042UsageRate\042,
round(DECODE(D.CONTENTS,\047UNDO\047,NVL(A.BYTES-NVL(U.BYTES, 0), 0)/1024/1024/1024,NVL(F.BYTES,0)/1024/1024/1024),1) \042Free_G\042,
D.STATUS \042Status\042,
A.COUNT \042DataFileCount\042,
D.CONTENTS \042Type\042,
D.EXTENT_MANAGEMENT \042ExtentManagement\042,
D.SEGMENT_SPACE_MANAGEMENT \042SegmentSpaceManagement\042
FROM SYS.DBA_TABLESPACES D,
(SELECT TABLESPACE_NAME,SUM(BYTES) BYTES,COUNT(FILE_ID) COUNT,sum(case when maxbytes=0 then BYTES else maxbytes end) max_extend
FROM DBA_DATA_FILES
GROUP BY TABLESPACE_NAME) A,
(SELECT TABLESPACE_NAME, SUM(BYTES) BYTES
FROM DBA_FREE_SPACE
GROUP BY TABLESPACE_NAME) F,
(SELECT TABLESPACE_NAME, SUM(BYTES) BYTES
FROM (SELECT TABLESPACE_NAME, SUM(BYTES) BYTES, STATUS
FROM DBA_UNDO_EXTENTS
WHERE STATUS = \047ACTIVE\047
GROUP BY TABLESPACE_NAME, STATUS
UNION ALL
SELECT TABLESPACE_NAME, SUM(BYTES) BYTES, STATUS
FROM DBA_UNDO_EXTENTS
WHERE STATUS = \047UNEXPIRED\047
GROUP BY TABLESPACE_NAME, STATUS)
GROUP BY TABLESPACE_NAME) U
WHERE D.TABLESPACE_NAME = A.TABLESPACE_NAME(+)
AND D.TABLESPACE_NAME = F.TABLESPACE_NAME(+)
AND D.TABLESPACE_NAME = U.TABLESPACE_NAME(+)
AND NOT (D.EXTENT_MANAGEMENT = \047LOCAL\047 AND D.CONTENTS = \047TEMPORARY\047)
UNION ALL
SELECT
D.TABLESPACE_NAME,
round(NVL(A.max_extend/1024/1024/1024,0),1) \042ExtensibleSize\042,
round(NVL(A.max_extend/1024/1024/1024,0),1) - round(NVL(T.BYTES,0)/1024/1024/1024,1) \042ExtensibleFreeSpace\042,
round(NVL(A.BYTES/1024/1024/1024,0),1) \042Size(G)\042,
round( NVL(T.BYTES,0)/1024/1024/1024,1) \042UsageAmount(G)\042,
round(NVL(T.BYTES/A.BYTES*100,0),1) \042UsageRate\042,
round((NVL(A.BYTES,0)/1024/1024/1024-NVL(T.BYTES,0)/1024/1024/1024),1) \042Free(G)\042,
D.STATUS,
A.COUNT,
D.CONTENTS,
D.EXTENT_MANAGEMENT,
D.SEGMENT_SPACE_MANAGEMENT
FROM SYS.DBA_TABLESPACES D,
(SELECT TABLESPACE_NAME, SUM(BYTES) BYTES, COUNT(FILE_ID) COUNT,sum(case when maxbytes=0 then BYTES else maxbytes end) max_extend
FROM DBA_TEMP_FILES
GROUP BY TABLESPACE_NAME) A,
(SELECT SS.TABLESPACE_NAME, SUM((SS.USED_BLOCKS*TS.BLOCKSIZE)) BYTES
FROM GV\044SORT_SEGMENT SS, SYS.TS\044 TS
WHERE SS.TABLESPACE_NAME=TS.NAME
GROUP BY SS.TABLESPACE_NAME) T
WHERE D.TABLESPACE_NAME=A.TABLESPACE_NAME(+)
AND D.TABLESPACE_NAME=T.TABLESPACE_NAME(+)
AND D.EXTENT_MANAGEMENT=\047LOCAL\047
AND D.CONTENTS = \047TEMPORARY\047
ORDER BY 1 DESC;
select t.INST_ID \042Instance_number\042,trunc(sum(t.PGA_ALLOC_MEM)/1024/1024)||\047 MB\047 \042Cur_pga\042 from gv\044process t group by t.INST_ID;
select INST_ID \042Instance_number\042,count(*) \042Cur_session_number\042 from gv\044process group by INST_ID;'|sqlplus -silent /nolog
EOFALL
chmod +x checkDB.sh
4° OGG状态检测和日志日切
su - oracle
mkdir /home/oracle/checkRAC
mkdir /ggs/log_history
cd /home/oracle/checkRAC
cat >checkOGG.sh<<EOFALL
#!/bin/bash
source ~/.bash_profile
TIME=\$(date '+%F_%H_%M_%S')
# OGG运行日志日切
# mv OGG log
cd /ggs
cp /ggs/ggserr.log /ggs/log_history/ggserr.log.bak\$TIME
if [ -f /ggs/log_history/ggserr.log.bak\$TIME ]
then
echo ''>/ggs/ggserr.log
echo "\$(date '+%F %R:%S') INFO mv ggserr.log is done "'!'
else
echo "\$(date '+%F %R:%S') ERROR mv ggserr.log catch error "'!'
fi
echo -e "\n\$(date '+%F %R:%S') INFO Check ERROR from OGG log "
cat /ggs/log_history/ggserr.log.bak\$TIME |grep -v ' INFO '
echo -e "\n\$(date '+%F %R:%S') INFO Check ERROR from OGG log done "'!'
# OGG进程状态检查
# check current OGG processes
echo -e "\n\$(date '+%F %R:%S') INFO Start Check Current OGG Processes "
/ggs/ggsci<<EOF
info all
info *
EOF
echo -e '\n'
sleep 10
/ggs/ggsci<<EOF
info *
lag *
EOF
echo -e '\n'
echo "\$(date '+%F %R:%S') INFO Check Current OGG Processes done "'!'
# DDL追踪日志日切
# mv OGG ddl trace log
DDL_TRACE_LOG=\$(find \$ORACLE_BASE -name 'ggs_ddl_trace.log')
DDL_TRACE_LOG_BAK=/ggs/log_history/ggs_ddl_trace.log.bak\$TIME
cp \${DDL_TRACE_LOG} \${DDL_TRACE_LOG_BAK}
if [ -f \${DDL_TRACE_LOG_BAK} ]
then
echo ''>\${DDL_TRACE_LOG}
cd /ggs/log_history
tar -czf ggs_ddl_trace.log.bak\$TIME.tar.gz ggs_ddl_trace.log.bak\$TIME --remove-files
echo -e "\n\$(date '+%F %R:%S') INFO mv ggs_ddl_trace.log is done "'!'
else
echo -e "\n\$(date '+%F %R:%S') ERROR mv ggs_ddl_trace.log catch error "'!'
fi
EOFALL
chmod +x checkOGG.sh
5° 日志日切,集群检测
mkdir /root/checkOS/checkRAC
cd /root/checkOS/checkRAC
cat >checkRAC.sh<<EOFALL
#!/bin/bash
source ~/.bash_profile
# 脚本运行环境
DATE=\$(date +%F)
TIME=\$(date +%F_%H-%M-%S)
WORK_PATH=/root/checkOS/checkRAC
SOURCE_LOG_PATH="\$WORK_PATH"/Log_"\$TIME"
RUN_LOG="\$WORK_PATH"/run.log_"\$TIME"
mkdir -p "\$SOURCE_LOG_PATH"
touch "\$RUN_LOG"
# 日志颜色设置
RED="\\033[1;31m"
GREEN="\\033[1;32m"
YELLOW="\\033[1;33m"
RES="\\033[0;39m"
# RAC环境
NODE_NUM=1
ORACLE_SID=orcl
ASM_SID=+ASM
HOSTNAME=\$(hostname)
GRID_BASE=/u01/app/grid
GRID_HOME=/u01/11.2.0/grid
ORACLE_BASE=/u01/app/oracle
ORACLE_HOME=/u01/app/oracle/product/11.2.0/db_1
# 根据实际情况设置
# 日志文件
RAC_ALERT="\$GRID_HOME"/log/"\$HOSTNAME"/alert"\$HOSTNAME".log
ASM_ALERT="\$GRID_BASE"/diag/asm/+asm/"\$ASM_SID""\$NODE_NUM"/trace/alert_"\$ASM_SID""\$NODE_NUM".log
ORACLE_ALERT="\$ORACLE_BASE"/diag/rdbms/"\$ORACLE_SID"/"\$ORACLE_SID""\$NODE_NUM"/trace/alert_"\$ORACLE_SID""\$NODE_NUM".log
LISTENER_LOG="\$GRID_HOME"/log/diag/tnslsnr/"\$HOSTNAME"/listener_scan1/trace/listener_scan1.log
# CRS_LOG="\$GRID_HOME"/log/"\$HOSTNAME"/crsd/crsd.log
# CSS_LOG="\$GRID_HOME"/log/"\$HOSTNAME"/cssd/ocssd.log
# EVM_LOG="\$GRID_HOME"/log/"\$HOSTNAME"/evmd/evmd.log
# 日志备份函数
BACKUP()
{
unset SOUR
unset DEST
SOUR="\$1"
if [ -n "\$SOUR" ]
then
DESTF=\$(basename "\$SOUR")
DEST="\$SOURCE_LOG_PATH"/"\$DESTF"
else
/bin/echo -e "\${RED}[-] \$TIME Please Input Log File!\${RES}">>"\$RUN_LOG"
return 1
fi
if [ -e "\$SOUR" ]
then
/bin/grep -i ora- -C 3 "\$SOUR">>"\$RUN_LOG"
/bin/cp "\$SOUR" "\$DEST"
/bin/echo ''>"\$SOUR"
/bin/echo -e "\${GREEN}[+] \$TIME Backup \$SOUR done.\${RES}">>"\$RUN_LOG"
return 0
else
/bin/echo -e "\${RED}[-] \$TIME Log File \$SOUR is not exist!\${RES}">>"\$RUN_LOG"
return 2
fi
}
# 检查OS状态
# checkOSstatus
cd /root/checkOS/
/bin/echo -e ''\$_{1..161}'#'|sed 's/ //g'>>"\$RUN_LOG"
/root/checkOS/checkYesterday.sh>>"\$RUN_LOG"
/bin/echo -e ''\$_{1..2}'\n'|sed 's/ //g'>>"\$RUN_LOG"
/root/checkOS/checkToday.sh>>"\$RUN_LOG"
/bin/echo -e ''\$_{1..161}'#'|sed 's/ //g'>>"\$RUN_LOG"
/bin/echo -e ''\$_{1..3}'\n'|sed 's/ //g'>>"\$RUN_LOG"
/bin/echo -e ''\$_{1..161}'#'|sed 's/ //g'>>"\$RUN_LOG"
/bin/df -hP>>"\$RUN_LOG"
/bin/echo -e ''\$_{1..1}'\n'|sed 's/ //g'>>"\$RUN_LOG"
/bin/df -ihP>>"\$RUN_LOG"
/bin/echo -e ''\$_{1..161}'#'|sed 's/ //g'>>"\$RUN_LOG"
/bin/echo -e ''\$_{1..7}'\n'|sed 's/ //g'>>"\$RUN_LOG"
# 检查集群状态
# checkCluster
/bin/echo -e ''\$_{1..161}'#'|sed 's/ //g'>>"\$RUN_LOG"
/bin/su - grid -c "/home/grid/checkRAC/crs_stat_format.sh">>"\$RUN_LOG"
/bin/echo -e ''\$_{1..161}'#'|sed 's/ //g'>>"\$RUN_LOG"
/bin/echo -e ''\$_{1..7}'\n'|sed 's/ //g'>>"\$RUN_LOG"
# 检查ASM空间使用状态
# checkASM
/bin/echo -e ''\$_{1..161}'#'|sed 's/ //g'>>"\$RUN_LOG"
/bin/su - grid -c "/home/grid/checkRAC/checkASM.sh">>"\$RUN_LOG"
/bin/echo -e ''\$_{1..161}'#'|sed 's/ //g'>>"\$RUN_LOG"
/bin/echo -e ''\$_{1..7}'\n'|sed 's/ //g'>>"\$RUN_LOG"
# 检查库表空间状态
# checkDBtablespace
/bin/echo -e ''\$_{1..161}'#'|sed 's/ //g'>>"\$RUN_LOG"
/bin/su - oracle -c "/home/oracle/checkRAC/checkDB.sh">>"\$RUN_LOG"
/bin/echo -e ''\$_{1..161}'#'|sed 's/ //g'>>"\$RUN_LOG"
/bin/echo -e ''\$_{1..7}'\n'|sed 's/ //g'>>"\$RUN_LOG"
# 调用备份函数,备份日志
# backupAlertLog
/bin/echo -e ''\$_{1..161}'#'|sed 's/ //g'>>"\$RUN_LOG"
BACKUP "\$RAC_ALERT"
/bin/echo -e ''\$_{1..161}'#'|sed 's/ //g'>>"\$RUN_LOG"
/bin/echo -e ''\$_{1..3}'\n'|sed 's/ //g'>>"\$RUN_LOG"
/bin/echo -e ''\$_{1..161}'#'|sed 's/ //g'>>"\$RUN_LOG"
BACKUP "\$ASM_ALERT"
/bin/echo -e ''\$_{1..161}'#'|sed 's/ //g'>>"\$RUN_LOG"
/bin/echo -e ''\$_{1..3}'\n'|sed 's/ //g'>>"\$RUN_LOG"
/bin/echo -e ''\$_{1..161}'#'|sed 's/ //g'>>"\$RUN_LOG"
BACKUP "\$ORACLE_ALERT"
/bin/echo -e ''\$_{1..161}'#'|sed 's/ //g'>>"\$RUN_LOG"
/bin/echo -e ''\$_{1..3}'\n'|sed 's/ //g'>>"\$RUN_LOG"
/bin/echo -e ''\$_{1..161}'#'|sed 's/ //g'>>"\$RUN_LOG"
BACKUP "\$LISTENER_LOG"
/bin/echo -e ''\$_{1..161}'#'|sed 's/ //g'>>"\$RUN_LOG"
/bin/echo -e ''\$_{1..7}'\n'|sed 's/ //g'>>"\$RUN_LOG"
# 检查RMAN备份
# checkRMAN
/bin/echo -e ''\$_{1..161}'#'|sed 's/ //g'>>"\$RUN_LOG"
cat /backup/rman/script/rman_run.log_\$(date +%Y_%m_%d)*|grep 'RMAN-'>>"\$RUN_LOG"
cp /backup/rman/script/rman_run.log_\$(date +%Y_%m_%d)* "\$SOURCE_LOG_PATH"/
/bin/su - oracle -c "echo 'list backup summary;'|rman target /">>"\$RUN_LOG"
# 检查OGG状态
# checkOGG
/bin/echo -e ''\$_{1..161}'#'|sed 's/ //g'>>"\$RUN_LOG"
/bin/su - oracle -c "/home/oracle/checkRAC/checkOGG.sh">>"\$RUN_LOG"
mv /ggs/log_history/* \$SOURCE_LOG_PATH/
EOFALL
chmod +x checkRAC.sh
# 部署自动任务,脚本加锁
crontab -l>/tmp/crontab.tmp
echo -e '\n# RAC Daily Check'>>/tmp/crontab.tmp
echo '0 0 * * * /bin/bash /root/checkOS/checkRAC/checkRAC.sh'>>/tmp/crontab.tmp
cat /tmp/crontab.tmp |crontab
rm -rf /tmp/crontab.tmp
chattr +i /root/checkOS/checkRAC/checkRAC.sh
find /home/*/checkRAC -name "*.sh" -exec chattr +i {} \;
[TOC]