forked from 1newstar/oracle-dba-scripts-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DB_WAIT_CHECK.sh
228 lines (212 loc) · 7.22 KB
/
DB_WAIT_CHECK.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#!/bin/bash
# SCRIPT WILL BE TRIGGER AFTER 60 SECONDS
source ~/.bashrc
. /home/oracle/nethome/dba/config/config.env
export DB_WAIT1=/home/<Location>/logs/dbwait1.sql
export DB_WAIT2=/home/<Location>/logs/dbwait2.sql
#export DB_WAIT_LIST='test01'
cd /home/<Location>/logs/
#-- Loop through databases and check if wait time is longer that 600 seconds (10 minutes)
for i in $PROD_DB_LIST; do
function wait_time_test(){
$ORACLE_HOME/bin/sqlplus -s $ZABBIXPASS@$i <<EOF_1
set head off pages 0
set feedback off
select seconds_in_wait from gv\$session
WHERE username is not null
and status='ACTIVE'
and event not like '%PX%'
and event not like '%SQL%'
and event not like '%Stream%'
and event not like '%broadcast%'
and event not like '%DIAG%'
and event not like '%RMAN backup%'
and event not like '%latch%';
exit
EOF_1
}
WAIT_TIME=`wait_time_test`
done
#-- Chech if variable is null
if [ -z $WAIT_TIME ]; then
echo $WAIT_TIME
echo "No Wait "
exit 1
else
echo "Wait wait wait there is a wait"
if [ $WAIT_TIME -gt 600 ]; then
for i in $PROD_DB_LIST; do
export ORACLE_HOME
function user_name(){
$ORACLE_HOME/bin/sqlplus -s $ZABBIXPASS@$i <<EOF
set head off pages 0
select username from gv\$session
WHERE username is not null
and status='ACTIVE'
and event not like '%PX%'
and event not like '%SQL%'
and event not like '%Stream%'
and event not like '%broadcast%'
and event not like '%DIAG%'
and event not like '%RMAN backup%'
and event not like '%latch%';
exit;
EOF
}
USER_NAME=`user_name`
echo $USER_NAME
function sid(){
$ORACLE_HOME/bin/sqlplus -s $ZABBIXPASS@$i <<EOF
set head off pages 0
select sid from gv\$session
WHERE username is not null
and status='ACTIVE'
and event not like '%PX%'
and event not like '%SQL%'
and event not like '%Stream%'
and event not like '%broadcast%'
and event not like '%DIAG%'
and event not like '%RMAN backup%'
and event not like '%latch%';
exit;
EOF
}
SID=`sid`
function object_name(){
$ORACLE_HOME/bin/sqlplus -s $ZABBIXPASS@$i <<EOF
set head off pages 0
select do.object_name from v\$session s, dba_objects do where sid=${SID} and s.ROW_WAIT_OBJ# = do.OBJECT_ID;
exit
EOF
}
OBJECT_NAME=`object_name`
function rowid(){
$ORACLE_HOME/bin/sqlplus -s $ZABBIXPASS@$i <<EOF
set head off pages 0
select dbms_rowid.rowid_create ( 1, ROW_WAIT_OBJ#,ROW_WAIT_FILE#,ROW_WAIT_BLOCK#,ROW_WAIT_ROW#) from v\$session s, dba_objects do where sid=${SID} and s.ROW_WAIT_OBJ#=do.OBJECT_ID;
exit;
EOF
}
ROW_ID=`rowid`
if [ $ROW_ID = "no rows selected" ]; then
$ROW_ID=''
fi
function sql_id(){
$ORACLE_HOME/bin/sqlplus -s $ZABBIXPASS@$i <<EOF
set head off pages 0
select sql_id from gv\$session
WHERE username is not null
and status='ACTIVE'
and event not like '%PX%'
and event not like '%SQL%'
and event not like '%Stream%'
and event not like '%broadcast%'
and event not like '%DIAG%'
and event not like '%RMAN backup%'
and event not like '%latch%';
exit;
EOF
}
SQL_ID=`sql_id`
function sql_id2(){
$ORACLE_HOME/bin/sqlplus -s $ZABBIXPASS@$i <<EOF
set head off pages 0
select b.prev_sql_id
FROM GV\$session a LEFT OUTER JOIN GV\$session b ON b.sid=a.blocking_session
AND b.inst_id = a.blocking_instance
WHERE a.username is not null
and a.status='ACTIVE'
and a.event not like '%PX%'
and a.event not like '%SQL%'
and a.event not like '%Stream%'
and a.event not like '%broadcast%'
and a.event not like '%DIAG%'
and a.event not like '%RMAN backup%';
exit;
EOF
}
SQL_ID2=`sql_id2`
function wait_time(){
$ORACLE_HOME/bin/sqlplus -s $ZABBIXPASS@$i <<EOF_1
set head off pages 0
select seconds_in_wait from gv\$session
WHERE username is not null
and status='ACTIVE'
and event not like '%PX%'
and event not like '%SQL%'
and event not like '%Stream%'
and event not like '%broadcast%'
and event not like '%DIAG%'
and event not like '%RMAN backup%'
and event not like '%latch%';
exit
EOF_1
}
WAIT_TIME=`wait_time`
if [ $WAIT_TIME -gt 60 ]; then
echo "--- A WAIT EVENT HAVE BEEN DETECTED ---" > $DB_WAIT2
echo "Date____:`date`" >> $DB_WAIT2
echo "Host____: $node" >> $DB_WAIT2
echo "Database: $i" >> $DB_WAIT2
echo "IP______: $IP " >> $DB_WAIT2
echo "Time spent waiting $WAIT_TIME Seconds" >> $DB_WAIT2
echo "Please notify the appropiate personal" >> $DB_WAIT2
$ORACLE_HOME/bin/sqlplus -s $ZABBIXPASS@$i << EOF1 >> $DB_WAIT2
SET LINES 150
col username for a30
col machine for a30
col sid for 99999
col blocking_status for a300
select s1.username || '@' || s1.machine || ' ( SID=' || s1.sid ||' serial#='|| s1.serial# || ' ) is blocking ' || s2.username || '@' || s2.machine || ' ( SID=' || s2.sid ||' serial#='|| s2.serial# || ' ) ' AS blocking_status
from v\$lock l1, v\$session s1, v\$lock l2, v\$session s2
where s1.sid=l1.sid
and s2.sid =l2.sid
and l1.BLOCK=1
and l2.request > 0
and l1.id1 = l2.id1
and l2.id2 = l2.id2 ;
set lines 150
--
TTITLE LEFT 'QUERY IN QUESTION'
select * from ${USER_NAME}.${OBJECT_NAME} where rowid='${ROW_ID}';
--
TTITLE LEFT ''
select DISTINCT sql_text as "First blocking sql" from v\$sql where sql_id='${SQL_ID}';
--
TTITLE LEFT ''
select DISTINCT sql_text as "Second blocked sql" from v\$sql where sql_id='${SQL_ID2}';
--
TTITLE LEFT '--KILL THE OFFENDING SQL--'
select 'alter system kill session '||''''||sid||','||serial#||''''||' immediate;' as "CAUTION"
from v\$session where username='${USER_NAME}' and status='ACTIVE';
--
set lines 150
col username for a18
col event for a40
col sid for 999
col seconds_in_wait for 99999
--
TTITLE LEFT 'DETAILS'
select a.seconds_in_wait as "Wait_in_Seconds", a.username, a.sid,a.serial#, a.event,a.sql_id, b.sid,b.prev_sql_id,b.serial#
FROM GV\$session a LEFT OUTER JOIN GV\$session b ON b.sid=a.blocking_session
AND b.inst_id = a.blocking_instance
WHERE a.username is not null
and a.status='ACTIVE'
and a.event not like '%PX%'
and a.event not like '%SQL%'
and a.event not like '%Stream%'
and a.event not like '%broadcast%'
and a.event not like '%DIAG%'
and a.event not like '%RMAN backup%'
and a.seconds_in_wait > 20;
exit
EOF1
mailx -s "DB_Wait Alert has been detected $i $node " $EMAIL_LIST < $DB_WAIT2
fi
done
fi
fi
rm -rf $FUN
rm -rf $DB_WAIT2
rm -rf $DB_WAIT1
exit 0