This script is to find out long running sql statement in database. It is also returning values 0,1 and 2 to use with Nagios monitoring tools to show normal,warning, critical alerts.
set long 5000
set pages 1000
select case when max_sql_time is null then 0
when max_sql_time < 1800 then 0
when max_sql_time between 1800 and 3600 then 1
else 2 end
as return_code, 'OpName is : ' || opname||
'. The max long running SQL in v$session_longops ran for '|| max_sql_time ||
' seconds. Message is ' || message ||
'. Username is ' || l.username ||
'. OSuser is ' || sess.osuser||
'. Program is '|| sess.program ||
'. Machine is ' || sess.machine ||
'. SQL Text is ' || s.sql_fulltext
as return_statement
from (select max(elapsed_seconds)
as max_sql_time,
username,
opname,
message,
sql_address
from v$session_longops
where
last_update_time > sysdate - interval '15' minute
and message not like 'RMAN: aggregate input: backup%'
and message not like 'RMAN: aggregate output: backup%'
and message not like 'RMAN: incremental datafile backup%'
group by username, opname, message,sql_address) l,v$sql s,v$session sess
where l.sql_address=s.address
AND sess.sql_address=s.address
UNION ALL
SELECT 0 as return_code ,to_clob('There are no rows returned') as return_statement from dual
set long 5000
set pages 1000
select case when max_sql_time is null then 0
when max_sql_time < 1800 then 0
when max_sql_time between 1800 and 3600 then 1
else 2 end
as return_code, 'OpName is : ' || opname||
'. The max long running SQL in v$session_longops ran for '|| max_sql_time ||
' seconds. Message is ' || message ||
'. Username is ' || l.username ||
'. OSuser is ' || sess.osuser||
'. Program is '|| sess.program ||
'. Machine is ' || sess.machine ||
'. SQL Text is ' || s.sql_fulltext
as return_statement
from (select max(elapsed_seconds)
as max_sql_time,
username,
opname,
message,
sql_address
from v$session_longops
where
last_update_time > sysdate - interval '15' minute
and message not like 'RMAN: aggregate input: backup%'
and message not like 'RMAN: aggregate output: backup%'
and message not like 'RMAN: incremental datafile backup%'
group by username, opname, message,sql_address) l,v$sql s,v$session sess
where l.sql_address=s.address
AND sess.sql_address=s.address
UNION ALL
SELECT 0 as return_code ,to_clob('There are no rows returned') as return_statement from dual
No comments:
Post a Comment