-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmonBloqueos_collector_DB1.sql
More file actions
354 lines (295 loc) · 17.7 KB
/
Copy pathmonBloqueos_collector_DB1.sql
File metadata and controls
354 lines (295 loc) · 17.7 KB
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
USE mon_db
go
IF OBJECT_ID('dbo.monBloqueos_collector_DB1') IS NOT NULL
BEGIN
DROP PROCEDURE dbo.monBloqueos_collector_DB1
IF OBJECT_ID('dbo.monBloqueos_collector_DB1') IS NOT NULL
PRINT '<<< FAILED DROPPING PROCEDURE dbo.monBloqueos_collector_DB1 >>>'
ELSE
PRINT '<<< DROPPED PROCEDURE dbo.monBloqueos_collector_DB1 >>>'
END
go
CREATE PROCEDURE dbo.monBloqueos_collector_DB1
AS
BEGIN
set nocount on
declare @var INT
create table #sysproc
(
Fecha DATETIME not null,
Spid int null,
Kpid int null,
WaitEventID int null,
Suid int null,
Id int null,
Hostname varchar(30) null,
Program_name varchar(30) null,
Cmd varchar(30) null,
Blocked int null,
Dbid smallint null,
Tran_name varchar(64) null,
Time_blocked int null,
Linenum int null,
Loggedindatetime datetime null,
Ipaddr varchar(64) null,
)
create table #bloquados_bloqueantes
(
SPID int not null,
kpid int not null,
linenumber int not null,
blocked int null,
blockingKPID int null,
)
create table #sqltext(
SPID int not null,
KPID int not null,
ServerUserID int not null,
LineNumber int not null,
SQLText varchar(255) null
)
create table #monBloqueos_dba_db1_sqltext(
Fecha datetime not null,
SPID int not null,
KPID int not null,
ServerUserID int not null,
LineNumber int not null,
SQLText varchar(255) null
)
while (1=1)
begin
--CARGO TABLA
declare @fecha DATETIME
select @fecha = getdate()
insert into #sysproc
select
@fecha as Fecha,
mp.spid,
mp.kpid,
mp.WaitEventID,
mp.serveruserid,
mps.ProcedureID as id,
mp.hostname,
mp.application,
mp.command,
case when isnull(mp.blockingspid,0) = 0
then 0
else mp.blockingspid
end as blockingspid,
mp.dbid,
mp.MasterTransactionID,
case when isnull(mp.blockingspid,0) = 0
then null
else mp.SecondsWaiting
end as SecWaiting,
mp.LineNumber,
DATEADD(ss, -mp.SecondsConnected, getdate()) loggedindatetime,
mpl.clientip as ipaddr
from master..monprocess mp
inner join master..monprocesslookup mpl
on mp.spid = mpl.spid
left join master..monprocessstatement mps
on mp.spid = mps.spid
left join (select
bk_process.blockingspid,
max(bk_process.SecondsWaiting) time_blocked
from master..monprocess bk_process
where bk_process.blockingspid > 0
and bk_process.WaitEventID = 150
group by bk_process.blockingspid
) bk_process
on bk_process.blockingspid = mp.spid
where (mp.blockingspid > 0
and mp.SecondsWaiting > 0
and mp.WaitEventID = 150
)
or bk_process.blockingspid is not null
while exists(Select 1 from #sysproc )
begin
select @fecha = getdate()
insert into #sqltext
select SPID,
kpid,
ServerUserID,
LineNumber,
SQLText
from master..monProcessSQLtext
--CARGO TABLA #BLOQUEADOS_BLOQUEANTES
--cargo bloqueantes
insert into #bloquados_bloqueantes
select
blocked as SPID,
1 as kpid,
2 as linenumber,
blocked = null,
blockingKPID = null
from #sysproc
where blocked > 0
group by blocked
--updateo bloqueado kpid
update #bloquados_bloqueantes
set kpid = procs.kpid
from #sysproc procs
where procs.spid = #bloquados_bloqueantes.spid
--updateo linenumber
update #bloquados_bloqueantes
set linenumber = procs.linenum
from #sysproc procs
where procs.spid = #bloquados_bloqueantes.spid
--cargo bloqueados
insert into #bloquados_bloqueantes
select
spid,kpid,linenum,blocked,null
from #sysproc
where blocked in (select SPID from #bloquados_bloqueantes)
--FIN CARGO TABLA #BLOQUEADOS_BLOQUEANTES
-- update blocked KPID
update #bloquados_bloqueantes
set blockingKPID = #sysproc.kpid
from #sysproc
where #sysproc.spid = #bloquados_bloqueantes.blocked
-- update tiempo
update mon_db..monBloqueos_dba_db1
set time_blocked=procs.time_blocked
from #bloquados_bloqueantes block
inner join #sysproc procs
on procs.spid = block.spid
and procs.kpid = block.kpid
and procs.Linenum = block.linenumber
and procs.time_blocked > 0
where block.spid = mon_db..monBloqueos_dba_db1.spid
and block.kpid = mon_db..monBloqueos_dba_db1.kpid
and block.linenumber = mon_db..monBloqueos_dba_db1.linenum
and block.blocked = mon_db..monBloqueos_dba_db1.blocked
-- insert
insert into mon_db..monBloqueos_dba_db1
select
sysproc.Fecha,
datepart(yy,sysproc.Fecha),
datepart(mm,sysproc.Fecha),
datepart(wk,sysproc.Fecha),
datepart(dd,sysproc.Fecha),
datepart(hh,sysproc.Fecha),
datepart(mi,sysproc.Fecha),
sysproc.spid,
sysproc.kpid,
sysproc.WaitEventID,
sysproc.suid,
sysproc.hostname,
sysproc.program_name,
sysproc.cmd,
sysproc.blocked,
bloq.blockingKPID,
sysproc.dbid,
sysproc.tran_name,
sysproc.time_blocked,
sysproc.linenum,
sysproc.loggedindatetime,
sysproc.ipaddr,
suser_name(sysproc.suid) as 'Usser',
db_name(sysproc.dbid) as 'Base',
object_name(sysproc.id,sysproc.dbid) as 'SP'
from #sysproc sysproc
inner join #bloquados_bloqueantes bloq
on bloq.SPID = sysproc.spid
and bloq.KPID = sysproc.kpid
and bloq.linenumber = sysproc.linenum
left join mon_db..monBloqueos_dba_db1 monblock
on monblock.spid = sysproc.spid
and monblock.kpid = sysproc.kpid
and monblock.linenum = sysproc.linenum
and monblock.blocked = sysproc.blocked
where monblock.spid is null
--insert monbloqueos_sqltext
insert into #monBloqueos_dba_db1_sqltext
select
@fecha as Fecha,
a.SPID,
a.kpid,
a.ServerUserID,
a.LineNumber,
a.SQLText
from #sqltext a
inner join #bloquados_bloqueantes b
on b.spid = a.SPID
and b.kpid = a.KPID
order by a.spid asc
insert into monBloqueos_dba_db1_sqltext
select
@fecha as Fecha,
a.SPID,
a.kpid,
a.ServerUserID,
a.LineNumber,
a.SQLText
from #monBloqueos_dba_db1_sqltext a
left join monBloqueos_dba_db1_sqltext b
on b.spid = a.SPID
and b.kpid = a.KPID
where b.spid is null
truncate table #sysproc
waitfor delay "00:00:02"
--CARGO TABLA
select @fecha = getdate()
insert into #sysproc
select
@fecha as Fecha,
mp.spid,
mp.kpid,
mp.WaitEventID,
mp.serveruserid,
mps.ProcedureID as id,
mp.hostname,
mp.application,
mp.command,
case when isnull(mp.blockingspid,0) = 0
then 0
else mp.blockingspid
end as blockingspid,
mp.dbid,
mp.MasterTransactionID,
case when isnull(mp.blockingspid,0) = 0
then null
else mp.SecondsWaiting
end as SecWaiting,
mp.LineNumber,
DATEADD(ss, -mp.SecondsConnected, getdate()) loggedindatetime,
mpl.clientip as ipaddr
from master..monprocess mp
inner join master..monprocesslookup mpl
on mp.spid = mpl.spid
left join master..monprocessstatement mps
on mp.spid = mps.spid
left join (select
bk_process.blockingspid,
max(bk_process.SecondsWaiting) time_blocked
from master..monprocess bk_process
where bk_process.blockingspid > 0
and bk_process.WaitEventID = 150
group by bk_process.blockingspid
) bk_process
on bk_process.blockingspid = mp.spid
where (mp.blockingspid > 0
and mp.SecondsWaiting > 0
and mp.WaitEventID = 150
)
or bk_process.blockingspid is not null
truncate table #bloquados_bloqueantes
truncate table #monBloqueos_dba_db1_sqltext
truncate table #sqltext
END
if ( datepart(hh,getdate()) = 05)
begin
break
end
waitfor delay "00:05:00"
end
END
go
EXEC sp_procxmode 'dbo.monBloqueos_collector_DB1', 'unchained'
go
IF OBJECT_ID('dbo.monBloqueos_collector_DB1') IS NOT NULL
PRINT '<<< CREATED PROCEDURE dbo.monBloqueos_collector_DB1 >>>'
ELSE
PRINT '<<< FAILED CREATING PROCEDURE dbo.monBloqueos_collector_DB1 >>>'
go