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

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

Linux Shell的相关参考:

1、监控Oracle监听shell脚本

robin@SZDB:~/dba_scripts/custom/bin> more ck_lsnr.sh
# +-------------------------------------------------------+
# +    CHECK LISTENER STATUS AND RESTART IT WHEN FAILED  |
# +    Author : Robinson                                  |
# +    Blog  :http://blog.csdn,net/robinson_0612        |
# +    Parameter : No                                    |
# +-------------------------------------------------------+

#!/bin/bash
# --------------------
# Define variable
# --------------------

if [ -f ~/.bash_profile ]; then
. ~/.bash_profile
fi
 
TIMESTAMP=`date +%Y%m%d%H%M`;  export TIMESTAMP
DBALIST="[email protected]"; export DBALIST
MAILPATH=/users/robin/dba_scripts/sendEmail-v1.56
LOG_DIR=/users/robin/dba_scripts/custom/log
LOG_FILE=${LOG_DIR}/lsnr_status_$TIMESTAMP.log
RETENTION=2

# -----------------------------------------
# Define how many listeners need to monitor
# -----------------------------------------

DB_COUNT=6
DB[1]=CNBO1
DB[2]=CNBOTST
DB[3]=CNMMBO
DB[4]=MMBOTST
DB[5]=SYBO2SZ
DB[6]=CNBO2

# -------------------------
# Begin to check listener
# -------------------------

touch $LOG_FILE
echo "`date` " >>$LOG_FILE
echo " The following listeners are down on `hostname`" >>$LOG_FILE
echo "-----------------------------------------------" >>$LOG_FILE

COUNT=1
while [ $COUNT -le $DB_COUNT ];
do
    for db in ${DB[$COUNT]};
    do
    lsnr_flag=`ps -ef | grep -i listener_${DB[$COUNT]} | grep -v grep`
        if [ -z "$lsnr_flag" ]; then
            echo "The listener for the database ${DB[$COUNT]} is down." >>$LOG_FILE
            echo "=======> restart listener for the database ${DB[$COUNT]}" >>$LOG_FILE
            lsnrctl start listener_${DB[$COUNT]} >>$LOG_FILE
            echo -e "------------------------------------------------------------------\n" >>$LOG_FILE
        fi
    done;
    COUNT=`expr $COUNT + 1`
done;

# --------------------------
# Send Email
# --------------------------

cnt=`grep "restart listener" $LOG_FILE |wc -l`
if [ "$cnt" -gt 0 ];then
    $MAILPATH/sendEmail -f [email protected] -t $DBALIST -u "Listener crashed on `hostname`" -o message-file=$LOG_FILE
else
    rm -rf $LOG_FILE
fi

# ------------------------------------------------
# Removing files older than $RETENTION parameter
# ------------------------------------------------

find ${LOG_DIR} -name "*lsnr_status*" -mtime +$RETENTION -exec rm {} \;

exit

oracle@SZDB:/users/robin/dba_scripts/custom/bin> ./ck_lsnr.sh
Feb 01 17:16:34 szdb sendEmail[18611]: Email was sent successfully!
Fri Feb  1 17:16:33 CST 2013      #下面是测试脚本邮件发送包含的内容
 The following listeners are down on SZDB
-----------------------------------------------
The listener for the database CNBO1 is down.
=======> restart listener for the database CNBO1

LSNRCTL for Linux: Version 10.2.0.3.0 - Production on 01-FEB-2013 17:16:33

Copyright (c) 1991, 2006, Oracle.  All rights reserved.

Starting /users/oracle/OraHome10g/bin/tnslsnr: please wait...

TNSLSNR for Linux: Version 10.2.0.3.0 - Production System parameter file is
/users/oracle/OraHome10g/network/admin/listener.ora
Log messages written to /users/oracle/OraHome10g/network/log/listener_cnbo1.log
Listening on: (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=192.101.7.2)(PORT=1901)))

Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=192.101.7.2)(PORT=1901)))
STATUS of the LISTENER
------------------------
Alias                    listener_CNBO1
Version                  TNSLSNR for Linux: Version 10.2.0.3.0 - Production
Start Date                01-FEB-2013 17:16:33
Uptime                    0 days 0 hr. 0 min. 0 sec
Trace Level              off
Security                  ON: Local OS Authentication
SNMP                      OFF
Listener Parameter File  /users/oracle/OraHome10g/network/admin/listener.ora
Listener Log File        /users/oracle/OraHome10g/network/log/listener_cnbo1.log
Listening Endpoints Summary...
  (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=192.101.7.2)(PORT=1901)))
Services Summary...
Service "CNBO1" has 1 instance(s).
  Instance "CNBO1", status UNKNOWN, has 1 handler(s) for this service...
The command completed successfully
------------------------------------------------------------------

2、补充

a、上面的监控监听脚本可以监控多个监听器。

b、监听器的名字的定义格式为LISTENER_$ORACLE_SID,未考虑缺省监听器的情形,如使用缺省监听器请做相应更改。

b、使用了数组的方式来定义实例名,每一个对应一个监听器,确保DB_COUNT的值与需要监控的监听器个数相符。

c、数组的每一个元素使用的是ORACLE_SID,如果是RAC,可以将其改为主机名。

d、如果检测到监听器宕掉的情形则会自动重启监听并发送邮件。

e、使用了sendEmail邮件发送程序来发送邮件。参阅:不可或缺的 sendEmail

f、通过crontab来部署该脚本。另,Oracle 10g测试可用,Oracle 11g待测。

相关推荐