您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“Oracle慢SQL監(jiān)控腳本代碼分享”,感興趣的朋友不妨來看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“Oracle慢SQL監(jiān)控腳本代碼分享”吧!
線上Oracle準(zhǔn)備實(shí)現(xiàn)類似MySQL slow query的監(jiān)控腳本,把查詢時(shí)間超出定值的SQL定時(shí)的發(fā)送郵件告警,實(shí)現(xiàn)過程記錄如下:
主要思路是通過DBA_HIST的幾個(gè)視圖來獲取每小時(shí)快照中慢SQL的情況,為了不影響線上環(huán)境,這里把腳本部署在了自己的監(jiān)控端,通過DBLINK定期的抓取線上生產(chǎn)庫的數(shù)據(jù)到監(jiān)控?cái)?shù)據(jù)庫,并簡(jiǎn)單的處理后獲得csv格式的報(bào)表,發(fā)送報(bào)表至郵箱。
定時(shí)腳本 每小時(shí)查詢一次
00 * * * * /opt/scripts/oracle/get_slow_query.sh
腳本內(nèi)容如下
[oracle@59-Mysql-Test ~]$ cat /opt/scripts/oracle/get_slow_query.sh
#!/bin/bash
errlog="/opt/scripts/oracle/sqlerror.log"
sq_data="/opt/scripts/oracle/slow_query_data.xls"
check_file="/opt/scripts/oracle/slowsql_check.log"
send_mail_check="/opt/scripts/oracle/send_mail.chk"
export ORACLE_BASE=/u01/app/oracle
export ORACLE_HOME=/u01/app/oracle/product/11.2.0/db_1
export ORACLE_SID=oramon
export PATH=/usr/sbin:$PATH
export PATH=/u01/app/oracle/product/11.2.0/db_1/bin:$PATH
export LD_LIBRARY_PATH=$ORACLE_HOME/lib:/lib:/usr/lib
export CLASSPATH=/u01/app/oracle/product/11.2.0/db_1/JRE:/u01/app/oracle/product/11.2.0/db_1/jlib:/u01/app/oracle/product/11.2.0/db_1/rdbms/jlib
cd /opt/scripts/oracle/
$ORACLE_HOME/bin/sqlplus -S sqmon/oracle @main > ${errlog}
cat ${errlog} | grep -v 'Call completed.' | grep -v '' > ${check_file}
[ -s ${check_file} ] && /bin/mail -s "Oracle slow query check error" xxx@xxx.com < ${check_file}
cat ${sq_data} | grep -v '<' >${send_mail_check}
[ -s ${send_mail_check} ] && /bin/mail -a ${sq_data} -s "OracleDB find slow query,please check" xxx@xxx.com,xxx@xxx.com
[oracle@59-Mysql-Test oracle]$ cat main.sql
call pro_get_slow_query();
set linesize 5000
set term off verify off feedback off pagesize 999
set markup html on entmap ON spool on preformat off
spool slow_query_data.xls
@get_tables.sql
spool off
exit
[oracle@59-Mysql-Test oracle]$ cat get_tables.sql
select sql_id,elapsed_time,cpu_time,iowait_time,gets,reads,rws,clwait_time,execs,elpe,machine,username,dbms_lob.substr(sqt,4000) from DBA_ORA_SLOW_QUERY where elpe > 10 and machine not in ('rac01','rac02');
存儲(chǔ)過程pro_get_slow_query內(nèi)容如下
CREATE OR REPLACE PROCEDURE SQMON.pro_get_slow_query
AS
BEGIN
/**********delete old data on sqltext*************/
delete from local_dba_hist_sqltextas;
commit;
insert into local_dba_hist_sqltextas select * from dba_hist_sqltext@dg2;
commit;
insert into DBA_ORA_SLOW_QUERY_HISTORY select a.*,sysdate from DBA_ORA_SLOW_QUERY;
commit;
delete from DBA_ORA_SLOW_QUERY;
commit;
/*
select * from DBA_ORA_SLOW_QUERY;
select * from DBA_ORA_SLOW_QUERY_HISTORY;
*/
/************insert new date ********************/
insert into DBA_ORA_SLOW_QUERY
select v_1.sql_id,
v_1.elapsed_time,
v_1.cpu_time,
v_1.iowait_time,
v_1.gets,
v_1.reads,
v_1.rws,
v_1.clwait_time,
v_1.execs,
v_1.elpe,
v_2.machine,
v_2.username,
v_1.sqt
from (select s.sql_id,
elapsed_time / 1000000 elapsed_time,
cpu_time / 1000000 cpu_time,
iowait_time / 1000000 iowait_time,
gets,
reads,
rws,
clwait_time / 1000000 clwait_time,
execs,
st.sql_text sqt,
elapsed_time / 1000000 / decode(execs, 0, null, execs) elpe
from (select *
from (select sql_id,
sum(executions_delta) execs,
sum(buffer_gets_delta) gets,
sum(disk_reads_delta) reads,
sum(rows_processed_delta) rws,
sum(cpu_time_delta) cpu_time,
sum(elapsed_time_delta) elapsed_time,
sum(clwait_delta) clwait_time,
sum(iowait_delta) iowait_time
from dba_hist_sqlstat@HUBSDG2
where snap_id >=
(select max(snap_id) - 1
from dba_hist_snapshot@DG2)
and snap_id <=
(select max(snap_id)
from dba_hist_snapshot@DG2)
group by sql_id
order by sum(elapsed_time_delta) desc)
where rownum <= 20) s,
local_dba_hist_sqltextas st
where st.sql_id = s.sql_id) v_1
left join (select distinct a.sql_id, a.machine, b.username
from dba_hist_active_sess_history@DG2 a
left join dba_users@DG2 b
on a.user_id = b.user_id
where a.snap_id >=
(select max(snap_id) - 1 from dba_hist_snapshot@DG2)
and a.snap_id <=
(select max(snap_id) from dba_hist_snapshot@DG2)) v_2
on v_1.sql_id = v_2.sql_id
order by elpe desc;
commit;
END;
/
上面標(biāo)綠加粗部分的表不再列出,可以直接通過CTAS格式去創(chuàng)建即可。
到此,相信大家對(duì)“Oracle慢SQL監(jiān)控腳本代碼分享”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。