整理的一个清理Oracle数据库监听日志的脚本,可以部署到监听进程的owner用户的crontab中(RAC通常是grid, 单实例通常是oracle), 实现的是监听日志大于1GB时,归档监听日志文本格式的文件如listener.log ,自动压缩保存, 后期循环自动覆盖, 11G 引入的ADR, XML格式的文件19c前也无法自动清理,这个shell 目前是自动rm 7前天的, 关于19c 的日志自动清理后期会分享
清理数据库监听日志的脚本
- 清理多个监听日志
- 清理文本和XML格式,自动判断
- 根据文本格式大小默认1GB
- 根据XML格式生成日期默认7天前
- 支持ORACLE用户同时安装DB 和GRID,listener in GRID HOME
- 机器名带域名
Tip: 在hpux 11.31 环境中shell 中的if -eq 比对数值大小时,如果数值大小超过Integer 类型的上限时使用的是String的ASCII码值比对,容易出现错误判断。
```
# #!/bin/sh # file: lsnr_log_clear.sh # author: weejar(anbob.com) # date: 2018/10/22 # desc: automatic to wrap listener log if filesize bigger than xx # version: 1.6 # note: to add crontab in listener owner # # I tested 11g, AIX 6, HPUX IA 11.31, EXADATA LINUX # update history # 0.1 2016/1/4 created # ... # 0.6 fix bug hpux 11.31 filesize over interger 2147483647 , do not work # 0.7 fix bug hpux listener two processes, exclude "-inherit" one # 0.8 fix bug hpux listener shadow processes get listener name wroung # 0.9 fix bug filesize float type compare issue, to integer # 1.0 fix LSNRRUNNING # 1.1 fix not remove listener xml log file in GRID HOME # 1.2 fix LSNR LSNR_BIN path awk cut date or time # 1.3 fix exclude another process contain "lsnr", et.. ETL tail listener log # 1.5 fix get listener name, get owner OH FOR each listener # 1.6 check rm xml error echo $0 echo `date "+%Y-%m-%d %H:%M:%S"` echo "lsnr_log_clear.sh V1.5" echo "To clear listener log file begin..." WorkDir="/tmp" listeners="${WorkDir}/listeners" # unit Mb FILESIZELIMIT=1024 # 0 not have xml, 1 have XMLFLAG=0 # set env if [[ -f ~/.profile ]] then . ~/.profile fi if [[ -f ~/.bash_profile ]] then . ~/.bash_profile fi if [[ ! -d $WorkDir ]] then mkdir -p $WorkDir fi OSV=`uname -s` echo "Current OS:${OSV}" LSNRRUNNING=`ps -ef | grep tnslsnr |grep -v log| grep -v grep` if [ -n "${LSNRRUNNING}" ] then # Get listener names ps -ef|grep lsnr|grep -v log|grep -v grep|sed 's/^.*\///'|awk '{print $2}'|sort |uniq > ${listeners} # Check user is listener owner USERID=`/usr/bin/id -u -nr` if [ $? -ne 0 ] then echo "ERROR: unable to determine uid" exit 99 fi echo "current user: ${USERID}" if [ -s "${listeners}" ] then LSNROWNER=`ps -ef | grep tnslsnr |grep -v log| grep -v grep | grep -v -i scan|head -n 1|awk '{print $1}'` echo "listener owner: ${LSNROWNER}" if [ "${USERID}" != "${LSNROWNER}" ] then echo "ERROR: This script must be run as listener owner: ${LSNROWNER}" exit 98 fi fi if [ -s "${listeners}" ] then for i in `cat ${listeners}|grep -v inherit` do echo " " echo "listener name: ${i}" LSNROWNER=`ps -ef |grep "${i} "|grep -v grep|head -n 1|awk '{print $1}'` echo "listener owner: ${LSNROWNER}" if [ "${USERID}" != "${LSNROWNER}" ] then echo "ERROR: This script must be run as listener owner: ${LSNROWNER}, Skip!" continue fi # if lsnrctl ORACLE HOME NOT CURRENT, i.e. grid and oracle soft all use oracle user LSNR_BIN=`ps -ef|grep "${i} "|grep -v log|grep -v grep|head -n 1|awk '{print substr($0,index($0,"/"))}'` echo "Listener File Path: ${LSNR_BIN}" if [ -n "${LSNR_BIN}" ] then ORACLE_HOME=${LSNR_BIN%/bin*} fi echo "Oracle Home: $ORACLE_HOME" LSNR_LOG=`${ORACLE_HOME}/bin/lsnrctl status ${i} | grep "Listener Log" | awk '{print $4}'` if [ ! -f "${LSNR_LOG}" ] then echo "${LSNR_LOG} get failed!" continue fi echo "listener log file: ${LSNR_LOG}" FILENAME=${LSNR_LOG##*/} echo "listener filename: ${FILENAME}" FILEEXT=${FILENAME#*.} echo "listener file ext: ${FILEEXT}" if [[ "log" = "${FILEEXT}" ]] then FILEPATH=`dirname ${LSNR_LOG}` echo "listener file path: ${FILEPATH}" FILESIZE=`ls -l ${LSNR_LOG}|awk '{ printf "%.0f", $5/1024/1024 }'` echo "listener file size(MB):${FILESIZE}" if [ ${FILESIZE} -gt ${FILESIZELIMIT} ];then echo "listener log is bigger than ${FILESIZELIMIT} MB" # cut listener log file ${ORACLE_HOME}/bin/lsnrctl <<EOF set cur listener ${i} set log_status off exit EOF echo "cd ${FILEPATH}" cd ${FILEPATH} echo "mv -f ${FILENAME} ${FILENAME}_old" mv -f ${FILENAME} ${FILENAME}_old ${ORACLE_HOME}/bin/lsnrctl <<EOF set cur listener ${i} set log_status on exit EOF echo "gzip -f ${FILENAME}_old" gzip -f ${FILENAME}_old else echo "listener log is litter than ${FILESIZELIMIT} MB" fi fi if [[ "xml" = "$FILEEXT" ]] then LOGTXT="`echo ${i}|tr 'A-Z' 'a-z'`.log" LOGTXTPATH=`dirname ${LSNR_LOG} |sed 's/alert$/trace/'` LOGTXTFILE="$LOGTXTPATH/$LOGTXT" echo "listener txt log file: ${LOGTXTFILE}" FILESIZE=`ls -l ${LOGTXTFILE}|awk '{ printf "%.0f", $5/1024/1024 }'` echo "listener file size(MB):${FILESIZE}" LOGXMLPATH=`dirname ${LSNR_LOG}` echo "listener xml log Home: ${LOGXMLPATH}" # curt txt file large than $FILESIZELIMIT if [ ${FILESIZE} -gt ${FILESIZELIMIT} ] then echo "listener log is bigger than ${FILESIZELIMIT} MB" # cut listener log file echo "mv -f ${LOGTXTFILE} ${LOGTXTFILE}_old " mv -f ${LOGTXTFILE} ${LOGTXTFILE}_old echo "cd ${LOGTXTPATH}" cd ${LOGTXTPATH} echo "gzip -f ${LOGTXT}_old" gzip -f ${LOGTXT}_old else echo "listener log is litter than ${FILESIZELIMIT} MB" fi # curt xml logfile longer than 7 days. echo "To remove listener file for xml format longer than 7 days..." cd $LOGXMLPATH find ./ -name "log*.xml" -mtime +7 -print -exec rm {} \; if [ $? -ne 0 ] then echo "ERROR: unable to rm xml log" exit 94 fi #set flag XMLFLAG=1 fi done fi else printf "Unable to swap listener log files - Listener down\n" echo "${HOST} Unable to swap listener log files - Listener down" fi echo `date "+%Y-%m-%d %H:%M:%S"` echo "To clear listener log file completed!" echo "" echo ""
```复制
最后修改时间:2021-08-19 16:17:05
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。
评论
相关阅读
【专家有话说第五期】在不同年龄段,DBA应该怎样规划自己的职业发展?
墨天轮编辑部
1321次阅读
2025-03-13 11:40:53
Oracle RAC ASM 磁盘组满了,无法扩容怎么在线处理?
Lucifer三思而后行
790次阅读
2025-03-17 11:33:53
Oracle+Deepseek+Dify 实现数据库数据实时分析
bicewow
722次阅读
2025-03-06 09:41:49
Oracle避坑指南|同名表导出难题:如何精准排除指定用户下的表?
szrsu
557次阅读
2025-03-05 00:42:34
2月“墨力原创作者计划”获奖名单公布
墨天轮编辑部
463次阅读
2025-03-13 14:38:19
Oracle 如何修改 db_unique_name?强迫症福音!
Lucifer三思而后行
352次阅读
2025-03-12 21:27:56
Oracle DataGuard高可用性解决方案详解
孙莹
314次阅读
2025-03-26 23:27:33
Oracle分区和执行计划相关的几个问题
听见风的声音
307次阅读
2025-03-07 08:51:42
数据库管理-第299期 数据库是否需要定期重启(20250306)
胖头鱼的鱼缸
250次阅读
2025-03-06 09:09:35
切换Oracle归档路径后,不能正常删除原归档路径上的归档文件
dbaking
246次阅读
2025-03-19 14:41:51
热门文章
移除DataGuard Standby配置导致Primary启动失败
2023-08-17 21244浏览
使用dblink产生的”SELECT /*+ FULL(P) +*/ * FROM XXXXX P ” 解析
2023-06-20 20873浏览
Troubleshooting 'ORA-28041: Authentication protocol internal error' change password 12c R2 DB
2020-04-08 13573浏览
浅谈ORACLE免费数据库Oracle Database XE (Express Edition) 版
2018-10-31 7476浏览
High wait event ‘row cache mutex’ in 12cR2、19c
2020-08-14 5518浏览