Oracle alert监控

为了保障数据库运行稳定,提高巡检效率,通过此脚本可自动巡检数据库后台日志是否出现异常,通过企业微信告知各主机节点数据库后台日志状态。
所有节点的alert信息归集到dw3(132.35.224.36)
自动巡检每个主机oracle的alert日志。
方法:
每个主机自己收集好当前时间点之前两个小时的日志信息,,如果有保存到`hostname`_alert_mon.log

--由于aix的date没有-d选项,当前这个脚本是有瑕疵的,crontab的时候,不能在每天的凌晨2点前执行。
--每个需要监控alert日志的主机公钥传给dw3

su - oracle
cat note  
/u01/app/oracle/diag/rdbms/dssdwe/dssdwe1/trace/alert_dssdwe1.log     <--内容是alertlog的绝对路径

cat .profile |grep -iE "oracle_base|oracle_home|sid|path" > /home/oracle/alert_mon.sh
vi temp
hostname=`hostname`
time_id=`date +%Y%m%d%H`
alertlog=`cat note`
hourtime=`echo $(($(date +%H) - 2)) |awk '{printf("%02d\n",$0)}'`
checktime=`date "+%a %b %d "`$hourtime
alert_text=`tail -3000 $alertlog |sed -n "/${checktime}/,/--end-df/p" | egrep '^ORA-' |awk -F ":" '{print $1}' |sort |uniq |grep -Ev "12012|00942|03113|02063|01555|02050|20011|3233|27369|03150|27369|03150|02050|03135|02391|02391|00028|06502|06512|01830"`
echo `hostname` > `hostname`_alert.log
echo $alert_text >> `hostname`_alert.log
scp `hostname`_alert.log 132.35.224.36:/home/oracle/alert_mon
rm -rf `hostname`_alert.log
exit
cat temp >> alert_mon.sh
--下面是快速部署脚本
su - oracle
cat .profile |grep -iE "oracle_base|oracle_home|sid|path" > /home/oracle/alert_mon.sh
echo 'hostname=`hostname`' >> alert_mon.sh
echo 'time_id=`cat note`' >> alert_mon.sh
echo 'alertlog=`cat note`' >> alert_mon.sh
echo 'hourtime=`echo $(($(date +%H) - 2)) |awk '"'"'{printf("%02d\n",$0)}'"'"'`' >> alert_mon.sh
echo 'checktime=`date "+%a %b %d "`$hourtime' >> alert_mon.sh
echo 'alert_text=`tail -3000 $alertlog |sed -n "/${checktime}/,/--end-df/p" | egrep '"'"'^ORA-'"'"' |awk -F ":" '"'"'{print $1}'"'"' |sort |uniq |grep -Ev "12012|00942|03113|02063|01555|02050|20011|3233|27369|03150|27369|03150|02050|03135|02391|02391|00028|06502|06512|01830"`' >> alert_mon.sh
echo 'echo `hostname` > `hostname`_alert.log' >> alert_mon.sh
echo '$alert_text >> `hostname`_alert.log' >> alert_mon.sh
echo 'scp `hostname`_alert.log 132.35.224.36:/home/oracle/alert_mon' >> alert_mon.sh
echo 'rm -rf `hostname`_alert.log' >> alert_mon.sh
echo 'exit' >> alert_mon.sh


--每个主机创建crontab
--20180616更新,由于aix的date命令没有-d选项,每天2点前执行脚本会有异常
--另外alert_mon.sh的结果使用scp传输,由于目标端接收scp的上限为10,所以各个节点的alert_mon.sh的crontab应该避免在同时间点。
--比如需要监控的1-10号主机设置的alert_mon.sh的crontab为每隔两小时的第1分钟,则11-20号主机就应该设置成每隔两小时的第2分钟,以此类推。
--把 224.36 上的公钥传给所有需要监控日志的主机即可解决这个问题,相当于 224.36 可以免密登录其他主机,存在安全隐患
--解决方法:dw3主机/etc/ssh/sshd_config的MaxStartups参数值增加,并重启ssh服务,暂时未更改
1 8,10,12,14,16,18,20,22 * * * sh /home/oracle/alert_mon.sh
------------------------------

--dw3库创建汇总表
create table system.total_alert_mon (
hostname varchar2(30),
alert_text  varchar2(4000),
timeid date) 
tablespace users;

--dw3库部署短信告警存储过程
--20180616更新,由于经分的短信告警有延迟,暂时废弃
create or replace procedure system.p_alert_mon IS
  V_RETCODE     VARCHAR2(200);
  V_RETINFO     VARCHAR2(200);
  V_CONTENT VARCHAR(2000);
cursor c1 is (select wm_concat(hostname) hostname,status,timeid,count(*) cnt from (
select l.hostname,timeid,
case
when alert_text is not null and a.hostn is not null then 'error'
when alert_text is null and a.hostn is not null then 'ok'
when a.hostn is null and alert_text is null then 'unknown' end status
from (select hostname hostn,timeid,alert_text from system.total_alert_mon where timeid=(select max(timeid) from system.total_alert_mon)) a,
system.check_list l
where l.hostname=a.hostn(+)
)
group by status,timeid
);
BEGIN
for i in c1 loop
--if i.status in ('unknown','error') then
V_CONTENT := to_char(SYSDATE,'yyyy-MM-dd HH24:mi:ss')||'数据库:'||i.hostname||'总数('||i.cnt||')'||'后台日志状态:'||i.status||' 巡检时间:'||i.timeid;
zb_src.P_SMS_SEND('15600009663', V_CONTENT ,V_RETCODE ,V_RETINFO);
--zb_src.P_SMS_SEND('18653106291', V_CONTENT ,V_RETCODE ,V_RETINFO);
--zb_src.P_SMS_SEND('18601383148', V_CONTENT ,V_RETCODE ,V_RETINFO);
--zb_src.P_SMS_SEND('18686671129', V_CONTENT ,V_RETCODE ,V_RETINFO);
--ZB_SRC.P_SMS_SEND_CBSS_JC(V_CONTENT, V_RETCODE, V_RETINFO);
--dbms_output.put_line(v_content);
--end if;
end loop;
commit;
END p_alert_mon;
/

--dw3库创建微信告警存储过程
create or replace procedure p_alert_mon_wx IS
  V_RETCODE     VARCHAR2(200);
  V_RETINFO     VARCHAR2(200);
  V_CONTENT VARCHAR(2000);
cursor c1 is (select wm_concat(hostname) hostname,status,nvl(timeid,'unknown') timeid,count(*) cnt from (
select l.hostname,to_char(timeid,'yyyymmdd_hh24:mi:ss') timeid,
case
when alert_text is not null and a.hostn is not null then 'error'
when alert_text is null and a.hostn is not null then 'ok'
when a.hostn is null and alert_text is null then 'unknown' end status
from (select hostname hostn,timeid,alert_text from system.total_alert_mon where timeid=(select max(timeid) from system.total_alert_mon)) a,
system.check_list l
where l.hostname=a.hostn(+)
)
group by status,timeid
);
BEGIN
for i in c1 loop
--if i.status in ('unknown','error') then
V_CONTENT := to_char(SYSDATE,'yyyy-MM-dd-HH24:mi:ss')||'数据库:'||i.hostname||'.巡检总数:('||i.cnt||')'||'.数据库日志状态:'||i.status||'.巡检时间:'||i.timeid;
--zb_src.P_SMS_SEND('15600009663', V_CONTENT ,V_RETCODE ,V_RETINFO);
--zb_src.P_SMS_SEND('18653106291', V_CONTENT ,V_RETCODE ,V_RETINFO);
--zb_src.P_SMS_SEND('18601383148', V_CONTENT ,V_RETCODE ,V_RETINFO);
--zb_src.P_SMS_SEND('18686671129', V_CONTENT ,V_RETCODE ,V_RETINFO);
--ZB_SRC.P_SMS_SEND_CBSS_JC(V_CONTENT, V_RETCODE, V_RETINFO);
--dbms_output.put_line(v_content);
--end if;
dbms_output.put_line(v_content);
end loop;
commit;
END p_alert_mon_wx;
/

--------------- windows 2008 server 创建ftp脚本以及任务计划 ---------------
type get_alert_mon.bat
set Path=c:\windows\system32;
ftp.exe -i -s:C:\alert_mon\ftpinfo.txt

type ftpinfo.txt
open 132.35.224.36
oracle
f0605vY#i1
lcd x:
cd /home/oracle/alert_mon
get total_alert_mon.log
close
bye

--
创建windows任务计划,分别在8,10,12,14,16,18,20,22点的零八分执行此ftp的脚本
------------------------------

--dw库 224.36 创建微信监控的crontab
mkdir /home/oracle/alert_mon/old

echo '#!/bin/bash' > /home/oracle/alert_mon/total_alert_mon.sh
cat .profile |grep -iE "oracle_base|oracle_home|sid|path" >> /home/oracle/alert_mon/total_alert_mon.sh

vi total_alert_mon.sh

filepath=/home/oracle/alert_mon/
cd $filepath
rm -rf $filepath/old/*.log
mv $filepath/*.log $filepath/old/
cd old
rm -rf total
for i in `ls *.log` ;do cat ${i} |xargs;done |awk '{print $1,",",$2,",","'"`date +%Y%m%d_%H%M%S`"'"}' > total_input.dat
sqlldr userid=system/nM8krdTV control=/home/oracle/alert_mon/old/total_input.ctl data=/home/oracle/alert_mon/old/total_input log=/home/oracle/alert_mon/old/log_total_input
#rm -rf total_input.dat
sleep 10
sqlplus -S / as sysdba << EOF > /home/oracle/alert_mon/total_alert_mon.log
set linesize 300
SET TERMOUT OFF
set feedback off
set echo off
set serveroutput on
exec system.p_alert_mon_wx
exit
EOF
awk -F "fff" '{print $NF"\\n"}' ${filepath}/total_alert_mon.log |awk BEGIN{RS=EOF}'{gsub(/\n/," ");print}' |sed 's/ //g'

crontab -l
5 8,10,12,14,16,18,20,22 * * * sh /home/oracle/total_alert_mon.sh

------------------------------
--sqlldr控制文件
cat /home/oracle/alert_mon/old/total_input.ctl
load data
truncate into table system.total_alert_mon
fields terminated by ','
trailing nullcols 
(
hostname "trim(:hostname)",
alert_text "trim(:alert_text)",
timeid DATE "YYYYMMDD_HH24:MI:SS"
)

------------------------------
注册企业微信获取相关ID:
Secret    
zroZvMQgtw60S3t6RPYzkA312OVGY0uMk0mpa8GYVV8
CorpID
ww62642410c4882fe8

--企业微信发送信息脚本,创建crontab时需在wexin.sh添加上系统的环境变量
cat wexin.sh
#!/bin/bash
export PATH="/Users/wittzhang/anaconda/bin:$PATH"
# Setting PATH for Python 2.7
# The original version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"
export PATH
file_path=/Users/wittzhang/Downloads/samba_folder/
file_name=total_alert_mon.log
#my id
CropID="ww62642410c4882fe8"
Secret="zroZvMQgtw60S3t6RPYzkA312OVGY0uMk0mpa8GYVV8"
#get access_token  
GURL="https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$CropID&corpsecret=$Secret"  
Gtoken=$(/usr/bin/curl -s -G $GURL | awk -F\" '{print $10}')  
PURL="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=$Gtoken"  
iconv -f gbk -t utf-8 $file_path$file_name > $file_path$file_name.convert
awk -F "fff" '{print $NF"\\n"}' $file_path$file_name.convert |awk BEGIN{RS=EOF}'{gsub(/\n/," ");print}' |sed 's/ //g' > $file_path$file_name.convert.n
Content=`cat $file_path$file_name.convert.n`
echo $Content  
#send message
/usr/bin/curl --data-ascii '{"touser":"", "toparty": "2","msgtype": "text","agentid": "1000002","text": {"content": "'${Content}'"},"safe":"0"}' $PURL
mv $file_path$file_name $file_path${file_name}.bk


------------------------------
--有外网的主机配置crontab
crontab -e
12 8,10,12,14,16,18,20,22 * * * xxx/wexin.sh

------------------------------

增加监控新节点的方法:
需要部署:
alert_mon.sh
crontab中创建执行计划
把新节点的公钥拷贝到dw3

由于 224.36 主机的限制,无法同时接收远端10台主机以上同时scp文件,所以新节点的crontab的scp时间,不能跟其他主机的时间相同。
比如:已经有10台主机crontab的时间为每隔两个小时的01分钟scp文件,则新主机的crontab需要把crontab的时间设置成02分钟。

--224.36 上需要增加新节点的主机名
system.check_list表增加新节点的主机名

猜你喜欢

转载自blog.csdn.net/wittzhang/article/details/81283123
今日推荐