使用shell脚本实现对Oracle数据库的监控与管理将大大简化DBA的工作负担,如常见的对实例的监控,监听的监控,告警日志的监控,以及数据库的备份,AWR report的自动邮件等。本文给出Linux 下使用 shell 脚本来监控 Oracle 告警日志(monitor alter log file)。

    Linux Shell的相关参考:
        Linux/Unix shell 脚本中调用SQL,RMAN脚本
        Linux/Unix shell sql 之间传递变量
        Linux/Unix shell 调用 PL/SQL
        Linux/Unix shell 监控Oracle实例(monitor instance)
        Linux/Unix shell 监控Oracle监听器(monitor listener)

 

1、监控Oracle告警日志脚本

[python] view plain copy print?

  1. robin@SZDB:~/dba_scripts/custom/bin> more ck_alert.sh  

  2. #!/bin/bash  

  3. # --------------------------------------------------------------------------+  

  4. #                  CHECK ALERT LOG FILE                                     |  

  5. #   Filename: ck_alert.sh                                                   |  

  6. #   Desc:                                                                   |  

  7. #       The script use to check alert log file.                             |  

  8. #       Once any error was caught, a mail alert will be sent.               |     

  9. #       Deploy it by crontab. e.g. per 15 min                               |    

  10. #   Usage:                                                                  |  

  11. #       ./ck_alert.sh $ORACLE_SID                                           |    

  12. #                                                                           |  

  13. #   Author : Robinson                                                       |   

  14. #   Blog   : http://blog.csdn.net/robinson_0612                             |  

  15. # --------------------------------------------------------------------------+  

  16. #  

  17. # --------------------------  

  18. #   Check SID  

  19. # --------------------------  

  20.   

  21. if [ -z "${1}" ];then  

  22.     echo "Usage: "  

  23.     echo "      `basename $0` ORACLE_SID"  

  24.     exit 1  

  25. fi  

  26.   

  27. # -------------------------------  

  28. #  Set environment here   

  29. # ------------------------------  

  30.   

  31. if [ -f ~/.bash_profile ]; then  

  32.     . ~/.bash_profile  

  33. fi  

  34.   

  35. export ORACLE_SID=$1  

  36. export MACHINE=`hostname`  

  37. export MAIL_DIR=/users/robin/dba_scripts/sendEmail-v1.56  

  38. export MAIL_LIST='Robinson.cheng@12306.com'  

  39. export MAIL_FM='oracle@szdb.com'  

  40.   

  41. # ----------------------------------------------  

  42. # check the database is running, if not exit  

  43. # ----------------------------------------------  

  44.   

  45. db_stat=`ps -ef | grep pmon_$ORACLE_SID | grep -v grep| cut -f3 -d_`  

  46. if [ -z "$db_stat" ]; then  

  47.     date >/tmp/db_${ORACLE_SID}_stauts.log  

  48.     echo " $ORACLE_SID is not available on ${MACHINE} !!!" >>/tmp/db_${ORACLE_SID}_stauts.log   

  49.     MAIL_SUB=" $ORACLE_SID is not available on ${MACHINE} !!!"  

  50.     $MAIL_DIR/sendEmail -u $MAIL_SUB -f $MAIL_FM -t $MAIL_LIST -o message-file=/tmp/db_${ORACLE_SID}_stauts.log  

  51.     exit 1  

  52. fi;  

  53.   

  54. # --------------------------------------  

  55. #  Get the location of alert log file  

  56. # --------------------------------------  

  57.   

  58. sqlplus '/ as sysdba' << EOF > /tmp/${ORACLE_SID}_monitor_temp.txt  

  59. column xxxx format a10  

  60. column value format a80  

  61. set lines 132  

  62. SELECT 'xxxx' ,value FROM  v\$parameter WHERE  name = 'background_dump_dest'  

  63. /  

  64. exit  

  65. EOF  

  66.   

  67. cat /tmp/${ORACLE_SID}_monitor_temp.txt | awk '$1 ~ /xxxx/ {print $2}' > /tmp/${ORACLE_SID}_monitor_location.txt  

  68. read ALERT_DIR < /tmp/${ORACLE_SID}_monitor_location.txt  

  69. rm /tmp/${ORACLE_SID}_monitor_temp.txt 2>/dev/null  

  70.   

  71. # ----------------------------------------  

  72. #  Define archive directory and log file  

  73. # ----------------------------------------  

  74.   

  75. DT=`date +%Y%m%d`  

  76. DT_DIR=`date +%Y%m`  

  77. ARCH_DIR=${ALERT_DIR}/${DT_DIR}  

  78.   

  79. if [ ! -d "${ARCH_DIR}" ] ; then  

  80.     mkdir $ARCH_DIR  

  81. fi  

  82.   

  83. ORIG_ALERT_LOG=${ALERT_DIR}/alert_${ORACLE_SID}.log  

  84. NEW_ALERT_LOG=${ARCH_DIR}/alert_${ORACLE_SID}.log.${DT}  

  85. TEMP_ALERT_LOG=${ORIG_ALERT_LOG}.temp  

  86. AWK_DIR=/users/robin/dba_scripts/custom/bin  

  87.   

  88. # -------------------------------------  

  89. #  Check alert log file and send email  

  90. # -------------------------------------  

  91. cat ${ORIG_ALERT_LOG} | awk -f $AWK_DIR/check_alert.awk > /tmp/${ORACLE_SID}_check_monitor_log.log  

  92. if [ -s "/tmp/${ORACLE_SID}_check_monitor_log.log" ];  

  93.    then   

  94.      echo "Found errors in sid ${ORACLE_SID}, mailed errors"  

  95.      echo -e "The following errors were found in the alert log for ${ORACLE_SID} \n" > /tmp/${ORACLE_SID}_check_monitor_log.mail  

  96.      echo -e "Alert log was copied into ${NEW_ALERT_LOG} \n">> /tmp/${ORACLE_SID}_check_monitor_log.mail  

  97.      date >> /tmp/${ORACLE_SID}_check_monitor_log.mail   

  98.      echo "--------------------------------------------------------------">>/tmp/${ORACLE_SID}_check_monitor_log.mail  

  99.      echo " "  

  100.      echo " " >> /tmp/${ORACLE_SID}_check_monitor_log.mail   

  101.      echo " " >> /tmp/${ORACLE_SID}_check_monitor_log.mail   

  102.      cat /tmp/${ORACLE_SID}_check_monitor_log.log >>  /tmp/${ORACLE_SID}_check_monitor_log.mail  

  103.      MAIL_SUB="Found errors in ${ORACLE_SID} on ${MACHINE}"  

  104.      $MAIL_DIR/sendEmail -u $MAIL_SUB -f $MAIL_FM -t $MAIL_LIST -o message-file=/tmp/${ORACLE_SID}_check_monitor_log.mail  

  105.   

  106. # --------------------------------  

  107. #  Backup current alert log file  

  108. # --------------------------------  

  109.     mv ${ORIG_ALERT_LOG} ${TEMP_ALERT_LOG}  

  110.     cat ${TEMP_ALERT_LOG} >> ${NEW_ALERT_LOG}  

  111.     #touch ${ORIG_ALERT_LOG}  

  112.     cat /dev/null > ${ORIG_ALERT_LOG}  

  113.        rm /tmp/${ORACLE_SID}_check_monitor_log.log   

  114.        rm /tmp/${ORACLE_SID}_check_monitor_log.mail  

  115.        rm ${TEMP_ALERT_LOG} > /dev/null   

  116.     exit  

  117. fi  

  118.   

  119. rm /tmp/${ORACLE_SID}_check_monitor_log.log > /dev/null  

  120. rm /tmp/${ORACLE_SID}_monitor_location.txt > /dev/null  

  121.   

  122. exit  

2、过滤Oracle告警日志错误信息

[python] view plain copy print?

  1. robin@SZDB:~/dba_scripts/custom/bin> more check_alert.awk  

  2. $0 ~ /Errors in file/ {print $0}  

  3. $0 ~ /PMON: terminating instance due to error 600/ {print $0}  

  4. $0 ~ /Started recovery/{print $0}  

  5. $0 ~ /Archival required/{print $0}  

  6. $0 ~ /Instance terminated/ {print $0}  

  7. $0 ~ /Checkpoint not complete/ {print $0}  

  8. $1 ~ /ORA-/ { print $0; flag=1 }  

  9. $0 !~ /ORA-/ {if (flag==1){print $0; flag=0;print " "} }  

  10. $0 ~ /ERROR_AUDIT/ {print $0}  

3、老化Oracle告警日志脚本

[python] view plain copy print?

  1. robin@SZDB:~/dba_scripts/custom/bin> more age_alert.sh  

  2. #!/bin/bash  

  3. # ------------------------------------------------------------+  

  4. #                 Age the alert log file                      |  

  5. #   FileName: age_alert.sh                                    |  

  6. #   Desc:                                                     |   

  7. #        The script use to age the alert log file             |  

  8. #   Usage:                                                    |  

  9. #        ./age_alert.sh $ORACLE_SID                           |  

  10. #                                                             |  

  11. #   Authror : Robinson                                        |  

  12. #   Blog    : http://blog.csdn.net/robinson_0612              |  

  13. # ------------------------------------------------------------+  

  14.   

  15. # --------------------------  

  16. #   Check SID  

  17. # --------------------------  

  18.   

  19. if [ -z "${1}" ];then  

  20.     echo "Usage: "  

  21.     echo "      `basename $0` ORACLE_SID"  

  22.     exit 1  

  23. fi  

  24.   

  25. # -------------------------------  

  26. #  Set environment here  

  27. # ------------------------------  

  28.   

  29. if [ -f ~/.bash_profile ]; then  

  30.     . ~/.bash_profile  

  31. fi  

  32.   

  33. export MAIL_DIR=/users/robin/dba_scripts/sendEmail-v1.56  

  34. export MAIL_LIST='Robinson.cheng@12306.com'  

  35. export MAIL_FM='oracle@szdb.com'  

  36. ORACLE_SID=$1;  export ORACLE_SID  

  37.   

  38. # ----------------------------------------------  

  39. # check if the database is running, if not exit  

  40. # ----------------------------------------------  

  41.   

  42. db_stat=`ps -ef | grep pmon_$ORACLE_SID | grep -v grep| cut -f3 -d_`  

  43. if [ -z "$db_stat" ]; then  

  44.     echo " $ORACLE_SID is not available on `hostname` !!!"    

  45.     MAIL_SUB=" $ORACLE_SID is not available on `hostname` !!!"  

  46.     MAIL_MSG="$ORACLE_SID is not available on `hostname` before age alert log file, exit, please check !"  

  47.     $MAIL_DIR/sendEmail -u $MAIL_SUB -f $MAIL_FM -t $MAIL_LIST -m $MAIL_MSG  

  48.     exit 1  

  49. fi  

  50.   

  51. # -----------------------------------  

  52. # Find bdump directory for database  

  53. # -----------------------------------  

  54.   

  55. DUMP_DIR=`sqlplus -S '/ as sysdba' << EOF  

  56. set pagesize 0 feedback off verify off heading off echo off  

  57. SELECT value FROM  v\\$parameter WHERE  name = 'background_dump_dest';  

  58. exit  

  59. EOF`  

  60. if [ -z ${DUMP_DIR} ]; then  

  61.     echo "The bdump directory was not found for ${ORACLE_SID}"  

  62.     MAIL_SUB="The bdump directory was not found for ${ORACLE_SID}"  

  63.     MAIL_MSG="The bdump directory was not found for ${ORACLE_SID} on `hostname` before age log file,exit,please check !"  

  64.     $MAIL_DIR/sendEmail -u $MAIL_SUB -f $MAIL_FM -t $MAIL_LIST -m $MAIL_MSG  

  65.     exit 1  

  66. else  

  67.     echo ${DUMP_DIR}  

  68. fi  

  69.   

  70. # -------------------------------  

  71. #  Archive alert log file  

  72. # -------------------------------  

  73.   

  74. DT=`date +%Y%m%d -d '-1 day'`  

  75. OLD_DIR=${DT:0:6}  

  76. NEW_DIR=`date +%Y%m`  

  77. ORIG_ALERT_LOG=${DUMP_DIR}/alert_${ORACLE_SID}.log  

  78. OLD_ARC_DIR=${DUMP_DIR}/${OLD_DIR}  

  79. NEW_ARC_DIR=${DUMP_DIR}/${NEW_DIR}  

  80.   

  81. if [ ! -d "${NEW_ARC_DIR}" ] ; then  

  82.     mkdir ${NEW_ARC_DIR}  

  83. fi  

  84.   

  85. if [ "${OLD_DIR}" \< "${NEW_DIR}" ];then  

  86.     ARC_LOG=${OLD_ARC_DIR}/alert_${ORACLE_SID}.log.${DT}  

  87. else  

  88.     ARC_LOG=${NEW_ARC_DIR}/alert_${ORACLE_SID}.log.${DT}  

  89. fi  

  90.   

  91. cat ${ORIG_ALERT_LOG} >>${ARC_LOG}  

  92. cat /dev/null>${ORIG_ALERT_LOG}  

  93. exit  

4、部署脚本到crontab

[python] view plain copy print?

  1. */15 * * * * /users/robin/dba_scripts/custom/bin/ck_alert.sh MMBOTST  

  2. 0 0 * * * /users/robin/dba_scripts/custom/bin/age_alert.sh MMBOTST  


5、补充
  a、上面脚本用于实时监控Oracle告警日志,一旦检测到错误,将发送邮件。
  b、对于已经检查过且发现错误的日志将被移动作为归档,也就是Oracle错误不会被重复检测。
  c、所有有关错误检测的过滤条件被放置到过滤文件check_alert.awk中。
  d、第3个脚本用于老化告警日志,建议设置老化的时间为每天0点,这样子,每天将会保留当天的告警日志。
  e、对于老化的告警日值,按年月来存放,也即是以年月命名文件夹,当天告警日志会存放在当月文件夹。
  f、使用了sendEmail邮件发送程序来发送邮件。参阅:不可或缺的 sendEmail
  g、该脚本仅在Oracle 10g测试可用,Oracle 11g应做相应修改。



原文:http://blog.csdn.net/leshami/article/details/8569759