使用shell脚本实现对Oracle数据库的监控与管理将大大简化DBA的工作负担,如常见的对实例的监控,监听的监控,告警日志的监控,以及数据库的备份,AWR report的自动邮件等。本文给出Linux 下使用 shell 脚本来监控 Oracle 实例。
Linux Shell的相关参考:
Linux/Unix shell 脚本中调用SQL,RMAN脚本
Linux/Unix shell sql 之间传递变量
Linux/Unix shell 调用 PL/SQL
1、监控Oracle实例shell脚本
- robin@SZDB:~/dba_scripts/custom/bin> more ck_inst.sh
- # +-------------------------------------------------------+
- # + CHECK INSTANCE STATUS AND SEND MAIL |
- # + Author : Robinson |
- # + Blog : http://blog.csdn.net/robinson_0612 |
- # + Desc: |
- # + variable X_DB use to exclude some instance |
- # +-------------------------------------------------------+
- #!/bin/bash
- # --------------------------------------------
- # Set environment vairable and define variable
- # --------------------------------------------
- if [ -f ~/.bash_profile ]; then
- . ~/.bash_profile
- fi
- ORATAB=/etc/oratab
- TIMESTAMP=`date +%Y%m%d%H%M`
- MAILPATH=/users/robin/dba_scripts/sendEmail-v1.56
- LOG_DIR=/users/robin/dba_scripts/custom/log
- LOG_FILE=${LOG_DIR}/ck_inst_$TIMESTAMP.log
- DBALIST="robinson.cheng@12306.com;robinson_0612@12306.com"
- X_DB='SYBO2SZ|CNQDII|CNFO'
- RETENTION=1
- # ----------------------
- # Check instance status
- # ----------------------
- if [ -z "$X_DB" ]; then
- X_DB='DUMMY'
- fi
- {
- echo "`date` "
- echo "Oracle Database(s) Status on `hostname`"
- echo "-----------------------------------------"
- db=`egrep -i ":Y|:N" $ORATAB | cut -d":" -f1 | grep -v "#" | grep -v "*"`
- pslist=`ps -ef | grep pmon | grep -v grep`
- dblist=`for i in $db; do echo $i; done | grep -vP $X_DB`
- for i in $dblist; do
- echo "$pslist" | grep "[oa]*_pmon_$i" > /dev/null 2>&1
- if (( $? )); then
- echo "Oracle Instance - $i: Down"
- else
- echo "Oracle Instance - $i: Up"
- fi
- done;
- }|tee -a ${LOG_FILE} 2>&1
- # ------------------------
- # Send Email
- # ------------------------
- cnt=`cat $LOG_FILE | grep Down | wc -l`
- if [ "$cnt" -gt 0 ]; then
- $MAILPATH/sendEmail -f szdb@2gotrade.com -t $DBALIST -u "Instance status on `hostname`" -o message-file=$LOG_FILE
- fi
- # ------------------------------------------------
- # Removing files older than $RETENTION parameter
- # ------------------------------------------------
- find ${LOG_DIR} -name "ck_inst*.*" -mtime +$RETENTION -exec rm {} ;
- exit
- robin@SZDB:~/dba_scripts/custom/bin> ./ck_inst.sh
- Fri Feb 1 15:10:41 CST 2013
- Oracle Database(s) Status on SZDB
- -----------------------------------------
- Oracle Instance - CNBO1: Up
- Oracle Instance - CNBOTST: Down
- Oracle Instance - CNMMBO: Up
- Oracle Instance - MMBOTST: Up
- Oracle Instance - CNMMBOBK: Down
- Oracle Instance - CI8960U: Up
- Oracle Instance - CNBO2: Up
- Feb 01 15:10:41 szdb sendEmail[16024]: Email was sent successfully!
2、补充
a、上面的脚本根据/etc/oratab中列出的实例进行监控,可以监控多个实例。
b、变量X_DB用于排除那些不需要监控的实例,如脚本中排出了3个实例。也可以将该变量置空。
c、如果X_DB的值为空时,我们赋予了DUMMY,确保你的数据库实例名没有使用DUMMY,否则过滤不掉。
d、监控脚本在监控过程中只要有一个实例宕掉,则发送整个监控报告。
d、使用了sendEmail邮件发送程序来发送邮件。参阅:不可或缺的 sendEmail
e、尾部清除监控过程中产生的保留日期之前的日志。
转:http://blog.csdn.net/leshami/article/details/8563115