Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2397622
  • 博文数量: 328
  • 博客积分: 4302
  • 博客等级: 上校
  • 技术积分: 5486
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-01 11:14
个人简介

悲剧,绝对的悲剧,悲剧中的悲剧。

文章分类

全部博文(328)

文章存档

2017年(6)

2016年(18)

2015年(28)

2014年(73)

2013年(62)

2012年(58)

2011年(55)

2010年(28)

分类: Oracle

2013-08-06 12:25:04

使用shell脚本实现对Oracle数据库的监控与管理将大大简化DBA的工作负担,如常见的对实例的监控,监听的监控,告警日志的监控,以及数据库的备份,AWR report的自动邮件等。本文给出Linux 下使用 shell 脚本来监控 Oracle 实例。
 
1、监控Oracle实例shell脚本
  1. robin@SZDB:~/dba_scripts/custom/bin> more ck_inst.sh
  2. # +-------------------------------------------------------+
  3. # + CHECK INSTANCE STATUS AND SEND MAIL |
  4. # + Author : Robinson |
  5. # + Blog : http://blog.csdn.net/robinson_0612 |
  6. # + Desc: |
  7. # + variable X_DB use to exclude some instance |
  8. # +-------------------------------------------------------+
  9.   
  10. #!/bin/bash
  11.   
  12. # --------------------------------------------
  13. # Set environment vairable and define variable
  14. # --------------------------------------------
  15.   
  16. if [ -f ~/.bash_profile ]; then
  17.     . ~/.bash_profile
  18. fi
  19.   
  20. ORATAB=/etc/oratab
  21. TIMESTAMP=`date +%Y%m%d%H%M`
  22. MAILPATH=/users/robin/dba_scripts/sendEmail-v1.56
  23. LOG_DIR=/users/robin/dba_scripts/custom/log
  24. LOG_FILE=${LOG_DIR}/ck_inst_$TIMESTAMP.log
  25. DBALIST="robinson.cheng@12306.com;robinson_0612@12306.com"
  26. X_DB='SYBO2SZ|CNQDII|CNFO'
  27. RETENTION=1
  28.   
  29. # ----------------------
  30. # Check instance status
  31. # ----------------------
  32.   
  33. if [ -z "$X_DB" ]; then
  34.     X_DB='DUMMY'
  35. fi
  36. {
  37. echo "`date` "
  38. echo "Oracle Database(s) Status on `hostname`"
  39. echo "-----------------------------------------"
  40. db=`egrep -i ":Y|:N" $ORATAB | cut -d":" -f1 | grep -v "\#" | grep -v "\*"`
  41. pslist=`ps -ef | grep pmon | grep -v grep`
  42. dblist=`for i in $db; do echo $i; done | grep -vP $X_DB`
  43. for i in $dblist; do
  44.     echo "$pslist" | grep "[oa]*_pmon_$i" > /dev/null 2>&1
  45.     if (( $? )); then
  46.         echo "Oracle Instance - $i: Down"
  47.     else
  48.         echo "Oracle Instance - $i: Up"
  49.     fi
  50. done;
  51. }|tee -a ${LOG_FILE} 2>&1
  52.   
  53. # ------------------------
  54. # Send Email
  55. # ------------------------
  56.   
  57. cnt=`cat $LOG_FILE | grep Down | wc -l`
  58. if [ "$cnt" -gt 0 ]; then
  59.     $MAILPATH/sendEmail -f szdb@2gotrade.com -t $DBALIST -u "Instance status on `hostname`" -o message-file=$LOG_FILE
  60. fi
  61.   
  62. # ------------------------------------------------
  63. # Removing files older than $RETENTION parameter
  64. # ------------------------------------------------
  65.   
  66. find ${LOG_DIR} -name "ck_inst*.*" -mtime +$RETENTION -exec rm {} \;
  67.   
  68. 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/robinson_0612/article/details/8563115
阅读(1946) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~