Linux/Unix shell 监控Oracle监听器(monitor listener)

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

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

 

1、监控Oracle监听shell脚本

[python] view plain copy
 
 print?
  1. robin@SZDB:~/dba_scripts/custom/bin> more ck_lsnr.sh   
  2. # +-------------------------------------------------------+  
  3. # +    CHECK LISTENER STATUS AND RESTART IT WHEN FAILED   |  
  4. # +    Author : Robinson                                  |   
  5. # +    Blog   :http://blog.csdn.net/robinson_0612        |  
  6. # +    Parameter : No                                     |   
  7. # +-------------------------------------------------------+  
  8.   
  9. #!/bin/bash   
  10. # --------------------  
  11. # Define variable  
  12. # --------------------  
  13.   
  14. if [ -f ~/.bash_profile ]; then  
  15. . ~/.bash_profile  
  16. fi  
  17.    
  18. TIMESTAMP=`date +%Y%m%d%H%M`;  export TIMESTAMP  
  19. DBALIST="robinson.cheng@12306.com"; export DBALIST  
  20. MAILPATH=/users/robin/dba_scripts/sendEmail-v1.56  
  21. LOG_DIR=/users/robin/dba_scripts/custom/log  
  22. LOG_FILE=${LOG_DIR}/lsnr_status_$TIMESTAMP.log  
  23. RETENTION=2  
  24.   
  25. # -----------------------------------------  
  26. # Define how many listeners need to monitor  
  27. # -----------------------------------------  
  28.   
  29. DB_COUNT=6  
  30. DB[1]=CNBO1  
  31. DB[2]=CNBOTST  
  32. DB[3]=CNMMBO  
  33. DB[4]=MMBOTST  
  34. DB[5]=SYBO2SZ  
  35. DB[6]=CNBO2  
  36.   
  37. # -------------------------  
  38. # Begin to check listener  
  39. # -------------------------  
  40.   
  41. touch $LOG_FILE  
  42. echo "`date` " >>$LOG_FILE  
  43. echo " The following listeners are down on `hostname`" >>$LOG_FILE  
  44. echo "-----------------------------------------------" >>$LOG_FILE  
  45.   
  46. COUNT=1  
  47. while [ $COUNT -le $DB_COUNT ];  
  48. do  
  49.     for db in ${DB[$COUNT]};  
  50.     do  
  51.     lsnr_flag=`ps -ef | grep -i listener_${DB[$COUNT]} | grep -v grep`  
  52.         if [ -z "$lsnr_flag" ]; then  
  53.             echo "The listener for the database ${DB[$COUNT]} is down." >>$LOG_FILE  
  54.             echo "=======> restart listener for the database ${DB[$COUNT]}" >>$LOG_FILE  
  55.             lsnrctl start listener_${DB[$COUNT]} >>$LOG_FILE  
  56.             echo -e "------------------------------------------------------------------\n" >>$LOG_FILE  
  57.         fi  
  58.     done;  
  59.     COUNT=`expr $COUNT + 1`  
  60. done;  
  61.   
  62. # --------------------------  
  63. # Send Email  
  64. # --------------------------  
  65.   
  66. cnt=`grep "restart listener" $LOG_FILE |wc -l`  
  67. if [ "$cnt" -gt 0 ];then  
  68.     $MAILPATH/sendEmail -f szdb@2gotrade.com -t $DBALIST -u "Listener crashed on `hostname`" -o message-file=$LOG_FILE  
  69. else  
  70.     rm -rf $LOG_FILE  
  71. fi  
  72.   
  73. # ------------------------------------------------  
  74. # Removing files older than $RETENTION parameter   
  75. # ------------------------------------------------  
  76.   
  77. find ${LOG_DIR} -name "*lsnr_status*" -mtime +$RETENTION -exec rm {} \;  
  78.   
  79. exit  
  80.   
  81. oracle@SZDB:/users/robin/dba_scripts/custom/bin> ./ck_lsnr.sh   
  82. Feb 01 17:16:34 szdb sendEmail[18611]: Email was sent successfully!  
  83. Fri Feb  17:16:33 CST 2013      #下面是测试脚本邮件发送包含的内容  
  84.  The following listeners are down on SZDB  
  85. -----------------------------------------------  
  86. The listener for the database CNBO1 is down.  
  87. =======> restart listener for the database CNBO1  
  88.   
  89. LSNRCTL for Linux: Version 10.2.0.3.0 - Production on 01-FEB-2013 17:16:33  
  90.   
  91. Copyright (c) 1991, 2006, Oracle.  All rights reserved.  
  92.   
  93. Starting /users/oracle/OraHome10g/bin/tnslsnr: please wait...  
  94.   
  95. TNSLSNR for Linux: Version 10.2.0.3.0 - Production System parameter file is   
  96. /users/oracle/OraHome10g/network/admin/listener.ora  
  97. Log messages written to /users/oracle/OraHome10g/network/log/listener_cnbo1.log  
  98. Listening on: (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=192.101.7.2)(PORT=1901)))  
  99.   
  100. Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=192.101.7.2)(PORT=1901)))  
  101. STATUS of the LISTENER  
  102. ------------------------  
  103. Alias                     listener_CNBO1  
  104. Version                   TNSLSNR for Linux: Version 10.2.0.3.0 - Production  
  105. Start Date                01-FEB-2013 17:16:33  
  106. Uptime                    0 days 0 hr. 0 min. 0 sec  
  107. Trace Level               off  
  108. Security                  ON: Local OS Authentication  
  109. SNMP                      OFF  
  110. Listener Parameter File   /users/oracle/OraHome10g/network/admin/listener.ora  
  111. Listener Log File         /users/oracle/OraHome10g/network/log/listener_cnbo1.log  
  112. Listening Endpoints Summary...  
  113.   (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=192.101.7.2)(PORT=1901)))  
  114. Services Summary...  
  115. Service "CNBO1" has 1 instance(s).  
  116.   Instance "CNBO1", status UNKNOWN, has 1 handler(s) for this service...  
  117. The command completed successfully  
  118. ------------------------------------------------------------------  

2、补充
   a、上面的监控监听脚本可以监控多个监听器。
   b、监听器的名字的定义格式为LISTENER_$ORACLE_SID,未考虑缺省监听器的情形,如使用缺省监听器请做相应更改。
   c、使用了数组的方式来定义实例名,每一个对应一个监听器,确保DB_COUNT的值与需要监控的监听器个数相符。
   d、数组的每一个元素使用的是ORACLE_SID,如果是RAC,可以将其改为主机名。
   e、如果检测到监听器宕掉的情形则会自动重启监听并发送邮件。
   f、使用了sendEmail邮件发送程序来发送邮件。参阅:不可或缺的 sendEmail
   g、通过crontab来部署该脚本。另,Oracle 10g测试可用,Oracle 11g待测。

转:http://blog.csdn.net/leshami/article/details/8563744

posted on 2016-09-16 22:49  张冲andy  阅读(1103)  评论(0编辑  收藏  举报

导航