标签:style c class blog code java
实现如下目标
1、监控Oracle数据库的监听和实例的状态,如果监听或者实例挂了,则启动,并邮件告知,脚本尝试启动对应的服务
2、如果服务启动成功,则邮件通知刚才挂了,现在已经启动成功;如果启动失败,则邮件提示,刚才挂了,现在依然不能启动;
脚本并不复杂,比较初级,但是实现了需求,在测试环境下测试通过,不过还缺少一些挂死的状态监测,有待继续增强:
[oracle@sdk31 ~]$ cat /home/oracle/scripts/check_db_lis.sh #!/bin/bash #set -x
#这里导入环境变量,避免Crontab执行时不能执行Oracle下的一些命令 export ORACLE_HOME=/oracle/product/10.2.0/db_1 export NLS_LANG=AMERICAN_AMERICA.ZHS16GBK export ORACLE_BASE=/oracle export PATH=$ORACLE_HOME/bin:$PATH export ORACLE_SID=dbcc #这是操作系统安装mailx后的mailx的配置,这是我申请的发送邮箱 #/etc/mail/rc #set smtp=smtp.126.com #set from=dbabio@126.com #set smtp-auth-user=dbabio@126.com smtp-auth-password=xxxxxx smtp-auth=login
#配置发件人和相关系统变量 mail_receive="xxxxxxxxxxx@qq.com"time=`date +%Y%m%d_%H:%M:%S` hostname="192.168.0.31" time=`date +‘%Y-%m-%d %H:%M:%S‘` ORATAB=/etc/
#或者数据库实例名以及pmon进程和监听进程,可以检查进程数量判断服务是否终止 sid=`cat /etc/oratab | grep -v "^#"| grep -v "^$"|cut -f1 -d ":"` pmon_pid=`ps -ef | grep pmon| grep -v grep` listener_num=`ps -ef | grep LISTENER|grep -v grep | wc -l`
#定义一些邮件的标题、内容等信息,比较繁琐,有待简化 title_dbs="Oracle_${hostname}_${time}_Instance_Down!!Alert!!" title_dbsok="Oracle_${hostname}_${time}_Instance_RecoverOK!!" title_dbsno="Oracle_${hostname}_${time}_Instance_RecoverFail!!" title_lis="Oracle_${hostname}_${time}_Listener_Down!!Alert!!" title_lisok="Oracle_${hostname}_${time}_Listener_RecoverOK!!" title_lisno="Oracle_${hostname}_${time}_Listener_RecoverFail!!" contents_db="Oracle Instance Down!! Please check!! Script will try to startup-- [[`date`]]" contents_dbok="Minutes before,Oracle Instance Down,But now it works ok Please check!! -- [[`date`]]" contents_dbno="Minutes before,Oracle Instance Down,But now it can‘t start remained!##! Please check!! -- [[`date`]]" contents_lis="Oracle Listener Down!! Please check!! Script will try to startup-- [[`date`]]" contents_lisok="Minutes before,Oracle Listener Down,But now it works ok Please check!!-- [[`date`]]" contents_lisno="Minutes before,Oracle Listener Down,But now it can‘t start remained!##! Please check!!-- [[`date`]]" #定义主要的日志文件,可作为邮件的附件发送 alert_log="/oracle/admin/dbcc/bdump/alert_dbcc.log" listener_log="/oracle/product/10.2.0/db_1/network/log/listener.log"
#监听监控函数 listener_monit() { if [ $listener_num = 0 ] then echo ${contents_lis} | mailx -v -s "${title_lis}" ${mail_receive} >/dev/null 2>&1 echo "${time}|${hostip}|oracle|down:listener" echo "Listener down! Now will start it ............" lsnrctl start >/dev/null sleep 2 echo "Listener had started ..........." listener_num_2=`ps -ef | grep LISTENER|grep -v grep | wc -l` if [ $listener_num_2 = 1 ] then echo ${contents_lisok} | mailx -v -s "${title_lisok}" ${mail_receive} >/dev/null 2>&1 else echo ${contents_lisno} | mailx -v -s "${title_lisno}" ${mail_receive} >/dev/null 2>&1 fi elif [ $listener_num = 1 ] then echo "${time}|${hostip}|oracle|running:listener" fi } #数据库实例监控函数 instance_monit() { for dbname in $sid;do echo "$pmon_pid" | grep "ora_pmon_$sid" >/dev/null 2>&1 if (( $? )) then echo "${time}|${hostip}|oracle|down:$dbname" echo ${contents_db} | mailx -v -a ${alert_log} -s "${title_dbs}" ${mail_receive} >/dev/null 2>&1 echo "Instance down! Now will start it ............" sqlplus /nolog<<EOF connect / as sysdba startup EOF sleep 5 echo "Instance had started ..........." db_number=`ps -ef | grep ‘ora_pmon_dbcc‘ | grep -v grep| wc -l` if [ $db_number -ne 0 ] then echo ${contents_dbok} | mailx -v -a ${alert_log} -s "${title_dbsok}" ${mail_receive} >/dev/null 2>&1 else echo ${contents_dbno} | mailx -v -a ${alert_log} -s "${title_dbsno}" ${mail_receive} >/dev/null 2>&1 fi else echo "${time}|${hostip}|oracle|running:$dbname" fi done } listener_monit echo "--------------------------------------------------------" instance_monit
crontab定时器部署:
*/5 * * * * /bin/sh /home/oracle/scripts/check_db_lis.sh >> /home/oracle/logs/check_db_lis.log
当然要发邮件的前提是安装mailx等邮件发送程序,并保证系统可以连接到外网系统
例如实例和监听全部停止,脚本自动检测并执行,收到4封邮件的效果如下:
实例挂了,通知:
实例恢复后通知:
Oracle数据库启动与停止监控shell脚本,布布扣,bubuko.com
标签:style c class blog code java
原文地址:http://www.cnblogs.com/dbtodaysoftware/p/3751773.html