费话不多说,直接上代码。
代码有风险,执行须谨慎。以下代码可随意取用,造成任何损失,本人概不负责。
#!/bin/bash
echo -e "\033[32;49m## ####################################################### ##\033[0m"
echo -e "\033[32;49m## File Name: oracle_log_auto_maitainer.sh ##\033[0m"
echo -e "\033[32;49m## Description: this script will auto maintain oracle ##\033[0m"
echo -e "\033[32;49m## logs. ##\033[0m"
echo -e "\033[32;49m## Version: 2.1.0 ##\033[0m"
echo -e "\033[32;49m## ##\033[0m"
echo -e "\033[32;49m## Designed by \033[0m\033[33;49;5mJackson Chen\033[0m \033[32;49m##\033[0m"
echo -e "\033[32;49m## 2017.01.24 ##\033[0m"
echo -e "\033[32;49m## ####################################################### ##\033[0m"
# data structure
# user:group:path:keyword[:edition]
# get software environment
find_path=`which find`
xargs_path=`which xargs`
# outputing of the running log
log_file=`echo "\`pwd\`/running.log"`
# _user_: using for creating different script file for different user
script_file_pre='/home/${_user_}/dnt/${_user_}_clear.sh'
alias log-writer="echo `date` >> ${log_file}; echo"
if [[ $(id oracle | awk -F '=' '{ print $1 }') == "uid" ]]; then
ORA_EXIST=1
ORA_BASE=`su - oracle -c 'echo $ORACLE_BASE'`
ORA_HOME=`su - oracle -c 'echo $ORACLE_HOME'`
else
ORA_EXIST=0
# log-writer "The oracle user is not exist in $HOSTNAME." >> $log_file
fi
if [[ $(id grid | awk -F '=' '{ print $1 }') == "uid" ]]; then
GRID_EXIST=1
CRS_BASE=`su - grid -c 'echo $ORACLE_BASE'`
CRS_HOME=`su - grid -c 'echo $ORACLE_HOME'`
else
GRID_EXIST=0
# log-writer "The grid user is not exist in $HOSTNAME." >> $log_file
fi
# create directory
if [[ $ORA_EXIST && -d /home/oracle ]]; then
mkdir /home/oracle/dnt
fi
if [[ $GRID_EXIST && -d /home/grid ]]; then
mkdir /home/grid/dnt
fi
# get listener owner
#lsnr_user='oracle'
if [[ "$((`ps -ef | grep tnslsnr | grep -v grep | awk '{ print $1 }' | grep oracle | wc -l`))" -gt 0 ]]; then
lsnr_user='oracle'
elif [[ -n "$GRID_EXIST" ]]; then
lsnr_user='grid'
else
lsnr_user='oracle'
fi
log-writer "The listener owner is $lsnr_user." >> $log_file
# get oracle sid list
#ORASIDS=`ps -ef | grep ora_smon_ | grep -v +ASM | grep -v grep | awk '{ print $8 }' | awk -F '_' '{ print $3 }'`
# edition
# initialize the edition of oracle software
edition=`su - oracle -c "sqlplus -V "| grep Release | awk '{ print $3 }'`
#edition='12.1.0.2.0'
ed1=`echo $edition | awk -F '.' '{ print $1 }'`
ed2=`echo $edition | awk -F '.' '{ print $2 }'`
ed3=`echo $edition | awk -F '.' '{ print $3 }'`
ed4=`echo $edition | awk -F '.' '{ print $4 }'`
ed5=`echo $edition | awk -F '.' '{ print $5 }'`
echo -e "\033[32;49mThe Oracle Software Version is [\033[0m\033[31;49;5m${edition}\033[0m\033[32;49m].\033[0m"
# collect the data about path.
# data structure # user:group:path:keyword:edition #
# user: the file's owner
# group: the group of the file's owner
# path: the directroy contain logs which need to be mantained
# keyword: same as _keyword_ below
# _target_: the directory you want to scan
# _keyword: the [type] of the directories
# type: audit # audit log file #
# alert-trace # db alert log and trace file #
# alert-xml # xml type of the alert log #
# listener # listner log #
# listener-trace # listener log and trace file #
# listener-xml # xml type of the listener log #
collect_path_data() {
if [[ "$ed1" = "9" ]]; then
_target_="$ORA_HOME/rdbms/audit"
_keyword_="audit"
path_9i_adump=`init_path_data`
_target_="$ORA_BASE/admin/*/bdump"
_keyword_="alert-trace"
path_9i_bdump=`init_path_data`
_target_="$ORA_HOME/network/log"
_keyword_="listener"
path_9i_lsnr="init_path_data"
echo "$path_9i_adump $path_9i_bdump $path_9i_lsnr"
elif [[ "$ed1" = "10" ]]; then
_target_="$ORA_BASE/admin/*/adump"
_keyword_="audit"
path_10g_adump=`init_path_data`
_target_="$ORA_BASE/admin/*/bdump"
_keyword_="alert-trace"
path_10g_bdump=`init_path_data`
_target_="$ORA_HOME/network/log"
_keyword_="listener"
path_10g_lsnr=`init_path_data`
#path_10g_lsnr=`su - $lsnr_user -c "lsnrctl status | grep "Listener Log File" | awk '{ print $4 }'`
echo "$path_10g_adump $path_10g_bdump $path_10g_lsnr"
elif [[ "$ed1" = "11" && "$ed2" = "1" ]]; then
_target_="$ORA_BASE/admin/*/adump"
_keyword_="audit"
path_11g_adump=`init_path_data`
_target_="$ORA_BASE/diag/rdbms/*/*/alert"
_keyword_="alert-xml"
path_11g_alert=`init_path_data`
_target_="$ORA_BASE/diag/rdbms/*/*/trace"
_keyword_="alert-trace"
path_11g_trace=`init_path_data`
_target_="$ORA_BASE/diag/tnslsnr/*/*/trace"
_keyword_="listener-trace"
path_11g_lsnr=`init_path_data`
_target_="$ORA_BASE/diag/tnslsnr/*/*/alert"
_keyword_="listener-xml"
path_11g_lsnr_alert=`init_path_data`
echo "$path_11g_adump $path_11g_alert $path_11g_trace $path_11g_lsnr $path_11g_lsnr_alert"
elif [[ "$ed1" = "11" && "$ed2" = "2" ]]; then
_target_="$ORA_BASE/admin/*/adump"
_keyword_="audit"
path_11g2_adump=`init_path_data`
_target_="$ORA_BASE/diag/rdbms/*/*/alert"
_keyword_="alert-xml"
path_11g2_alert=`init_path_data`
_target_="$ORA_BASE/diag/rdbms/*/*/trace"
_keyword_="alert-trace"
path_11g2_trace=`init_path_data`
if [[ "${lsnr_user}" = "oracle" ]]; then
_target_="$ORA_BASE/diag/tnslsnr/*/*/trace"
_keyword_="listener-trace"
path_11g2_lsnr=`init_path_data`
_target_="$ORA_BASE/diag/tnslsnr/*/*/alert"
_keyword_="listener-xml"
path_11g2_lsnr_alert=`init_path_data`
fi
if [[ -n $GRID_EXIST ]]; then
_target_="$CRS_BASE/diag/tnslsnr/*/*/trace"
_keyword_="listener-trace"
path_11g2_lsnr="`init_path_data` $path_11g2_lsnr"
_target_="$CRS_BASE/diag/tnslsnr/*/*/alert"
_keyword_="listener-xml"
path_11g2_lsnr_alert="`init_path_data` $path_11g2_lsnr_alert"
fi
echo "$path_11g2_adump $path_11g2_alert $path_11g2_trace $path_11g2_lsnr $path_11g2_lsnr_alert"
elif [[ "$ed1" = "12" ]]; then
_target_="$ORA_BASE/admin/*/adump"
_keyword_="audit"
path_12c_adump=`init_path_data`
_target_="$ORA_BASE/diag/rdbms/*/*/alert"
_keyword_="alert-xml"
path_12c_alert=`init_path_data`
_target_="$ORA_BASE/diag/rdbms/*/*/trace"
_keyword_="alert-trace"
path_12c_trace=`init_path_data`
if [[ "${lsnr_user}" = "oracle" ]]; then
_target_="$ORA_BASE/diag/tnslsnr/*/*/trace"
_keyword_="listener-trace"
path_12c_lsnr=`init_path_data`
_target_="$ORA_BASE/diag/tnslsnr/*/*/alert"
_keyword_="listener-xml"
path_12c_lsnr_alert=`init_path_data`
fi
if [[ -n $GRID_EXIST ]]; then
_target_="$CRS_BASE/diag/tnslsnr/*/*/trace"
_keyword_="listener-trace"
path_12c_lsnr="`init_path_data` $path_12c_lsnr"
_target_="$CRS_BASE/diag/tnslsnr/*/*/alert"
_keyword_="listener-xml"
path_12c_lsnr_alert="`init_path_data` $path_12c_lsnr_alert"
_target_="$CRS_BASE/diag/crs/*/crs/trace"
_keyword_="alert-trace"
path_12c_crs=`init_path_data`
_target_="$CRS_BASE/diag/crs/*/crs/alert"
_keyword_="alert-xml"
path_12c_crs_alert=`init_path_data`
fi
echo "$path_12c_adump $path_12c_alert $path_12c_trace $path_12c_lsnr $path_12c_lsnr_alert $path_12c_crs $path_12c_crs_alert"
else
log-writer "unknown edition [${edition}] of oracle software found on [${HOSTNAME}]." >> $log_file
fi
}
# initialize the data structure with absolute path
# data structure # user:group:path:keyword:edition #
# _target_: absolute path, set it before using this function.
# _keyword_: type of the path, set it before using this function.
init_path_data() {
ls -dl ${_target_} | while read line
do
echo $edition" ${_keyword_} "$line | awk '{ print $5":"$6":"$11":"$2":"$1 }'
done
}
# get the owner of the path data
# set it before using this function.
# _path_data_: one line of path_data list(user:group:path:keyword:edition)
get_path_user() {
echo ${_path_data_} | awk -F ':' '{ print $1 }';
}
# get the group of the path's owner
# set it before using this function.
get_path_group() {
echo ${_path_data_} | awk -F ':' '{ print $2 }';
}
# get the path
# set it before using this function.
get_path_path() {
echo ${_path_data_} | awk -F ':' '{ print $3 }';
}
# get the keywork(type) of the path
# set it before using this function.
get_path_keyword() {
echo ${_path_data_} | awk -F ':' '{ print $4 }';
}
# maintain listener or alert log
# _path_data_: should be set before using this function
# _path_: the path value in the path_data should be set before using this function
maintain_log_main() {
main_log=`ls -1 ${_path_}/*.log`
echo "cp ${main_log}{,-\`date +%Y%m%d\`.bak}" >> ${_script_file_}
echo "echo \`date\` > ${main_log}" >> ${_script_file_}
echo "echo 'Backuped and modified by ${_script_file_}.' >> ${main_log}" >> ${_script_file_}
echo "${find_path} ${_path_} -name \"*.log-*bak\" -type f -mtime +28 | ${xargs_path} rm -rf;" >> ${_script_file_}
}
maintain_log_audit() {
echo "$find_path ${_path_} -name \"*.aud\" -type f -mtime +14 | $xargs_path rm -rf;" >> ${_script_file_}
}
maintain_log_xml() {
echo "$find_path ${_path_} -name \"log_*.xml\" -type f -mtime +14 | $xargs_path rm -rf;" >> ${_script_file_}
}
maintain_log_trace() {
echo "$find_path ${_path_} -name \"*.tr*\" -type f -mtime +14 | $xargs_path rm -rf;" >> ${_script_file_}
}
# create local maintaining script
echo -e "\033[32;49mCollecting logs' path data...\033[0m"
for _path_data_ in `collect_path_data`; do
echo -e "\033[32;49mProcessing path data [\033[0m\033[33;49m${_path_data_}\033[0m\033[32;49m]...\033[0m"
if [[ ! -n ${_path_data_} ]]; then
log-writer "WANNING: none path_data collected." >> $log_file
break;
fi
_user_=`get_path_user`
_group_=`get_path_group`
_path_=`get_path_path`
keyword=`get_path_keyword`
if [[ "${_user_}" = "root" ]]; then
_user_="grid"
fi
_script_file_=`eval echo ${script_file_pre}`
case ${keyword} in
"audit" )
`maintain_log_audit`
;;
"alert-trace" )
`maintain_log_main`
`maintain_log_trace`
;;
"alert-xml" )
`maintain_log_xml`
;;
"listener" )
`maintain_log_main`
;;
"listener-xml" )
`maintain_log_xml`
;;
"listener-trace" )
`maintain_log_main`
`maintain_log_trace`
;;
* )
log-writer "WARNNING: unknown type of path [${_path_data_}] found on [${HOSTNAME}]." >> ${log_file}
;;
esac
done
# configure crontab
ORA_CRON=`crontab -u oracle -l 2>/dev/null | grep "oracle_clear"`
GRID_CRON=`crontab -u grid -l 2>/dev/null | grep "grid_clear"`
## for grid user
if [[ $GRID_EXIST && -d /home/grid && ! -n "$GRID_CRON" ]]; then
echo '3 3 * * 3 /bin/sh /home/grid/dnt/grid_clear.sh >/dev/null 2>&1' >> /var/spool/cron/grid
elif [ -n "$GRID_CRON" ]; then
log-writer "WARNNING: crontab line [${GRID_CRON}] for grid was exist on [${HOSTNAME}]." >> $log_file
fi
## for oracle user
if [[ $ORA_EXIST && -d /home/oracle && ! -n "$ORA_CRON" ]]; then
echo '3 3 * * 3 /bin/sh /home/oracle/dnt/oracle_clear.sh >/dev/null 2>&1' >> /var/spool/cron/oracle
elif [ -n "$ORA_CRON" ]; then
log-writer "WARNNING: crontab line [${GRID_CRON}] for oracle was exist on [${HOSTNAME}]." >> $log_file
fi
# repair the permission
if [ $ORA_EXIST ]; then
ora_owner_ship=`su - oracle -c "ls -ld ~" | awk '{ print $3":"$4 }'`
fi
if [ $GRID_EXIST ]; then
grid_owner_ship=`su - grid -c "ls -ld ~" | awk '{ print $3":"$4 }'`
fi
if [[ -f /home/grid/dnt/grid_clear.sh || -n $grid_owner_ship ]]; then
chown $grid_owner_ship /home/grid/dnt
chown $grid_owner_ship /home/grid/dnt/grid_clear.sh
fi
if [[ -f /home/oracle/dnt/oracle_clear.sh || -n $ora_owner_ship ]]; then
chown $ora_owner_ship /home/oracle/dnt
chown $ora_owner_ship /home/oracle/dnt/oracle_clear.sh
fi
echo -e "\033[32;49mThe end...\033[0m"
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/25521690/viewspace-2134107/
,如需转载,请注明出处,否则将追究法律责任。