forked from CloudDBM/mentor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Autoridade_SQL_Server_implanta_GerenciaBD_Monitora_Bloqueio_script_unico.txt
603 lines (454 loc) · 20.2 KB
/
Autoridade_SQL_Server_implanta_GerenciaBD_Monitora_Bloqueio_script_unico.txt
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
sp_configure 'show advanced options',1
go
RECONFIGURE with override
go
sp_configure 'xp_cmdshell',1
go
RECONFIGURE with override
go
USE [msdb]
GO
/****** Object: Table [dbo].[blocktable] Script Date: 26/08/2016 14:39:47 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[blocktable](
[blockedsid] [int] NULL,
[blockingsid] [int] NULL,
[eventinfo1] [varchar](300) NULL,
[eventinfo2] [varchar](300) NULL,
[waittime] [int] NULL,
[hostname] [varchar](20) NULL,
[dat_bloqueio] [datetime] NULL,
[program_name1] [varchar](30) NULL,
[program_name2] [varchar](30) NULL,
[host_blocking] [char](20) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object: Table [dbo].[trace_dia_geral] Script Date: 26/08/2016 14:39:47 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[trace_dia_geral](
[cod_servidor] [varchar](255) NULL,
[TextData] [ntext] NULL,
[DatabaseID] [int] NULL,
[NTUserName] [nvarchar](128) NULL,
[HostName] [nvarchar](128) NULL,
[ApplicationName] [nvarchar](128) NULL,
[LoginName] [nvarchar](128) NULL,
[Duration] [bigint] NULL,
[StartTime] [datetime] NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
USE [msdb]
GO
USE [msdb]
GO
/****** Object: StoredProcedure [dbo].[spu_GerenciaBD] Script Date: 12/18/2018 11:57:14 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create procedure [dbo].[spu_GerenciaBD] as
-- Evitando exibição de registros
set nocount on
-- Declarando variável para multi-servidor
DECLARE @SERVIDOR_MONITORANDO VARCHAR(100)
DECLARE @BANCO_MONITORANDO VARCHAR(100)
DECLARE @TABELA_TABELA_MONITORANDO VARCHAR(100)
DECLARE @SERVIDOR_MONITORADO nvarchar(128)
-- Atribuindo variáveis para configuração do GerenciaBD GACO
SET @SERVIDOR_MONITORANDO = (select @@servername)
SET @BANCO_MONITORANDO = 'msdb'
SET @TABELA_TABELA_MONITORANDO = 'TRACE_DIA_GERAL'
DECLARE
@ChkSrvName varchar(128) /*Stores Server Name*/
,@PhysicalSrvName VARCHAR(128) /*Stores physical name*/
,@TrueSrvName varchar(128) /*Stores Full Name*/
SET @PhysicalSrvName = CAST(SERVERPROPERTY('MachineName') AS VARCHAR(128))
set @ChkSrvName = CAST(SERVERPROPERTY('INSTANCENAME') AS VARCHAR(128))
IF @ChkSrvName IS NULL /*Detect default or named instance*/
BEGIN
SET @TrueSrvName = @PhysicalSrvName
END
ELSE
BEGIN
SET @TrueSrvName = @PhysicalSrvName +'\' + @ChkSrvName
END
print @TrueSrvName
SET @SERVIDOR_MONITORADO = @TrueSrvName
-- Coletando caminho do SQL Server para gravar trace file otimizado, via server trace
declare @rc2 int, @dir nvarchar(4000)
exec @rc2 = master.dbo.xp_instance_regread
N'HKEY_LOCAL_MACHINE',
N'Software\Microsoft\MSSQLServer\Setup',
N'SQLPath',
@dir output, 'no_output'
--select @dir AS InstallationDirectory
--PRINT @dir + '\indicador_performance_dia'
-- Localizando e finalizando server trace anterior somente do GerenciaBD
declare @traceid2 int
declare @caminho varchar(1000)
declare @caminho2 varchar(1000)
set @caminho = 'c:\scripts' + '\indicador_performance_dia'
set @caminho2 = 'c:\scripts' + '\indicador_performance_dia.trc'
SELECT top 1 @traceid2 = traceid FROM :: fn_trace_getinfo(default) where value = @caminho
if @traceid2 is null
SELECT top 1 @traceid2 = traceid FROM :: fn_trace_getinfo(default) where value = @caminho2
select @traceid2
if @traceid2 is not null
begin
EXEC sp_trace_setstatus @traceid2, 0
EXEC sp_trace_setstatus @traceid2, 2
-- Definindo comando para contabilização de indicadores do GerenciaBD
DECLARE @COMANDO_INSERT_SERVIDOR_MONITORANDO VARCHAR(8000)
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'prodver') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table prodver
create table prodver ([index] int, Name nvarchar(50),Internal_value int, Charcater_Value nvarchar(50))
insert into prodver exec master.dbo.xp_msver 'ProductVersion'
--if (select substring(Charcater_Value,1,1)from prodver)<= 8 -- Verificando a versão o server trace
IF 10 <= 8
SET @COMANDO_INSERT_SERVIDOR_MONITORANDO = 'insert into [' +@SERVIDOR_MONITORANDO + ']' + '.' + @BANCO_MONITORANDO + '.DBO.' + @TABELA_TABELA_MONITORANDO +
' SELECT '+ '''' +@SERVIDOR_MONITORADO +''',textdata, databaseid, ntusername, hostname, applicationname, loginname, duration/1000, starttime
from ::fn_trace_gettable(''' + 'c:\scripts' + '\indicador_performance_dia.trc''' + ', default)'
else
SET @COMANDO_INSERT_SERVIDOR_MONITORANDO = 'insert into [' +@SERVIDOR_MONITORANDO + ']' + '.' + @BANCO_MONITORANDO + '.DBO.' + @TABELA_TABELA_MONITORANDO +
' SELECT '+ '''' +@SERVIDOR_MONITORADO +''',textdata, databaseid, ntusername, hostname, applicationname, loginname, duration/1000000, starttime
from ::fn_trace_gettable(''' + 'c:\scripts' + '\indicador_performance_dia.trc''' + ', default)'
/*
USE [msdb]
GO
drop table trace_dia_geral
go
CREATE TABLE [dbo].[trace_dia_geral](
[cod_servidor] [varchar](255) NULL,
[TextData] [ntext] NULL,
[DatabaseID] [int] NULL,
[NTUserName] [nvarchar](128) NULL,
[HostName] [nvarchar](128) NULL,
[ApplicationName] [nvarchar](128) NULL,
[LoginName] [nvarchar](128) NULL,
[Duration] [bigint] NULL,
[StartTime] [datetime] NULL)
[CPU] [int] NULL,
[Reads] [bigint] NULL,
[Writes] [bigint] NULL,
[ClientProcessID] [int] NULL,
[SPID] [int] NULL,
[EndTime] [datetime] NULL,
[BinaryData] [image] NULL,
[DatabaseName] [nvarchar](128) NULL,
[Error] [int] NULL,
[EventSequence] [bigint] NULL,
[GroupID] [int] NULL,
[IntegerData] [int] NULL,
[IsSystem] [int] NULL,
[LoginSid] [image] NULL,
[NTDomainName] [nvarchar](128) NULL,
[ObjectName] [nvarchar](128) NULL,
[RequestID] [int] NULL,
[RowCounts] [bigint] NULL,
[SessionLoginName] [nvarchar](128) NULL,
[TransactionID] [bigint] NULL,
[XactSequence] [bigint] NULL,
PRIMARY KEY CLUSTERED
(
[RowNumber] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
*/
-- Executando comando para contabilização de indicadores do GerenciaBD
exec (@COMANDO_INSERT_SERVIDOR_MONITORANDO)
end
-- Definindo comando para deleção de arquivos de trace já contabilizado
DECLARE @COMANDO_DELECAO VARCHAR (1000)
SET @COMANDO_DELECAO = 'del "'+ 'c:\scripts' + '\indicador_performance_dia.trc"'
print @COMANDO_DELECAO
-- Executando comando para deleção de arquivo de trace já contabilizado
exec master..xp_cmdshell @COMANDO_DELECAO
-- Gerando server trace otimizado do GerenciaBD
/****************************************************/
/* Created by: SQL Profiler */
/* Date: 27/04/2005 16:16:01 */
/****************************************************/
-- Create a Queue
declare @rc int
declare @TraceID int
declare @maxfilesize bigint
declare @datetime datetime
--declare @caminho varchar(50)
set @datetime = (select substring (convert (varchar(11),getdate(),120),1,11) + '23:59:00.000')
set @maxfilesize = 10000
--print @dir
declare @arquivo_trace nvarchar(128)
set @arquivo_trace = 'c:\scripts' + '\indicador_performance_dia'
print @arquivo_trace
-- Please replace the text InsertFileNameHere, with an appropriate
-- filename prefixed by a path, e.g., c:\MyFolder\MyTrace. The .trc extension
-- will be appended to the filename automatically. If you are writing from
-- remote server to local drive, please use UNC path and make sure server has
-- write access to your network share
--set @caminho = 'D:\SQLADMIN\MSSQL\LOG\' + convert (varchar(50),getdate(),112)
--select @caminho
exec @rc = sp_trace_create @TraceID output, 0, @arquivo_trace, @maxfilesize, @datetime
if (@rc != 0) goto error
-- Client side File and Table cannot be scripted
-- Writing to a table is not supported through the SP's
-- Set the events
declare @on bit
set @on = 1
exec sp_trace_setevent @TraceID, 10, 1, @on
exec sp_trace_setevent @TraceID, 10, 3, @on
exec sp_trace_setevent @TraceID, 10, 6, @on
exec sp_trace_setevent @TraceID, 10, 8, @on
exec sp_trace_setevent @TraceID, 10, 9, @on
exec sp_trace_setevent @TraceID, 10, 10, @on
exec sp_trace_setevent @TraceID, 10, 11, @on
exec sp_trace_setevent @TraceID, 10, 12, @on
exec sp_trace_setevent @TraceID, 10, 13, @on
exec sp_trace_setevent @TraceID, 10, 14, @on
exec sp_trace_setevent @TraceID, 10, 16, @on
exec sp_trace_setevent @TraceID, 10, 17, @on
exec sp_trace_setevent @TraceID, 10, 18, @on
exec sp_trace_setevent @TraceID, 12, 1, @on
exec sp_trace_setevent @TraceID, 12, 3, @on
exec sp_trace_setevent @TraceID, 12, 6, @on
exec sp_trace_setevent @TraceID, 12, 8, @on
exec sp_trace_setevent @TraceID, 12, 9, @on
exec sp_trace_setevent @TraceID, 12, 10, @on
exec sp_trace_setevent @TraceID, 12, 11, @on
exec sp_trace_setevent @TraceID, 12, 12, @on
exec sp_trace_setevent @TraceID, 12, 13, @on
exec sp_trace_setevent @TraceID, 12, 14, @on
exec sp_trace_setevent @TraceID, 12, 16, @on
exec sp_trace_setevent @TraceID, 12, 17, @on
exec sp_trace_setevent @TraceID, 12, 18, @on
-- Set the Filters
declare @intfilter int
declare @bigintfilter bigint
-- Verifica qual a versão do SQL Server para filtrar o server trace
IF 10 <= 8
--if (select substring(Charcater_Value,1,1) from prodver)<= 8
begin
set @bigintfilter = 3000
end
else
begin
set @bigintfilter = 3000000
end
exec sp_trace_setfilter @TraceID, 13, 0, 4, @bigintfilter
exec sp_trace_setfilter @TraceID, 8, 0, 7, @SERVIDOR_MONITORADO
exec sp_trace_setfilter @TraceID, 10, 0, 7, N'SQL Profiler'
exec sp_trace_setfilter @TraceID, 10, 0, 7, N'SQLAgent%'
set @intfilter = 100
exec sp_trace_setfilter @TraceID, 22, 0, 4, @intfilter
-- Set the trace status to start
exec sp_trace_setstatus @TraceID, 1
-- display trace id for future references
select TraceID=@TraceID
goto finish
error:
select ErrorCode=@rc
finish:
--EXEC sp_trace_setstatus 1, 0
--EXEC sp_trace_setstatus 1, 2
--EXEC sp_trace_setstatus 2, 0
--EXEC sp_trace_setstatus 2, 2
--SELECT * FROM :: fn_trace_getinfo(default)
--select * into Trace_Table from ::fn_trace_gettable('c:\ind_perf\sicla_indicador_perform
-- Contabilizando informações de Bloqueios coletadas pelo job schedulado Alerta - Monitora Processos Bloqueados
GO
USE [msdb]
GO
/****** Object: StoredProcedure [dbo].[spu_checkblocking] Script Date: 12/18/2018 11:57:51 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
create procedure [dbo].[spu_checkblocking]
as
declare @spid int,@blocked int,@waittime int,@dbccstmt varchar(100)
declare @eventtype1 varchar(300),@eventtype2 varchar(300), @hostname varchar(20), @program_name varchar(30), @program_name2 varchar(30), @host_blocking varchar(20)
declare cur_sp cursor for select spid,blocked,waittime,hostname,program_name from master.dbo.sysprocesses (nolock) where blocked > 1
create table #dbcc_output (
eventtype varchar(30),
parameters varchar(30),
eventinfo varchar(300)
)
open cur_sp
fetch next from cur_sp into @spid,@blocked,@waittime,@hostname,@program_name
while (@@fetch_status = 0)
begin
select @program_name2 = program_name from master.dbo.sysprocesses (nolock) where spid=@blocked
select @host_blocking = hostname from master.dbo.sysprocesses (nolock) where spid=@blocked
set @dbccstmt = 'dbcc inputbuffer ('+convert(char(3),@spid)+')'
print @dbccstmt
insert into #dbcc_output exec (@dbccstmt)
select @eventtype1 = eventinfo from #dbcc_output
truncate table #dbcc_output
set @dbccstmt = 'dbcc inputbuffer ('+convert(char(3),@blocked)+')'
insert into #dbcc_output exec (@dbccstmt)
select @eventtype2 = eventinfo from #dbcc_output
truncate table #dbcc_output
if @spid <> @blocked
insert into blocktable values (@spid,@blocked,@eventtype1,@eventtype2,@waittime,@hostname,getdate(),@program_name, @program_name2,@host_blocking)
/*
if @waittime > 90000 exec master.dbo.xp_cmdshell 'net send "E0404019" "ACIARIA - PROCESSO BLOQUEADO A MAIS DE 2 MINUTOS - VERIFIQUE NO PROCESS INFO"'
if @waittime > 90000 exec master.dbo.xp_cmdshell 'net send "E0404009" "ACIARIA - PROCESSO BLOQUEADO A MAIS DE 2 MINUTOS - VERIFIQUE NO PROCESS INFO"'
if @waittime > 90000 exec master.dbo.xp_cmdshell 'net send "M2216" "ACIARIA - PROCESSO BLOQUEADO A MAIS DE 2 MINUTOS - VERIFIQUE NO PROCESS INFO"'
if @waittime > 90000 exec master.dbo.xp_cmdshell 'net send "console" "ACIARIA - PROCESSO BLOQUEADO A MAIS DE 2 MINUTOS - VERIFIQUE NO PROCESS INFO"'
if @waittime > 90000 exec master.dbo.xp_cmdshell 'net send "m2953" "ACIARIA - PROCESSO BLOQUEADO A MAIS DE 2 MINUTOS - VERIFIQUE NO PROCESS INFO"'
if @waittime > 90000 exec master.dbo.xp_cmdshell 'net send "m2954" "ACIARIA - PROCESSO BLOQUEADO A MAIS DE 2 MINUTOS - VERIFIQUE NO PROCESS INFO"'
if @waittime > 90000 exec master.dbo.xp_cmdshell 'net send "GILBERTR" "ACIARIA - PROCESSO BLOQUEADO A MAIS DE 2 MINUTOS - VERIFIQUE NO PROCESS INFO"'
if @waittime > 600000 exec master.dbo.xp_cmdshell 'net send "uf104730" "ACIARIA - PROCESSO BLOQUEADO A MAIS DE 10 MINUTOS - UTILIZE A SP_QUEM"'
if @waittime > 600000 exec master.dbo.xp_cmdshell 'net send "uf025055" "ACIARIA - PROCESSO BLOQUEADO A MAIS DE 10 MINUTOS - UTILIZE A SP_QUEM"'
if @waittime > 600000 exec master.dbo.xp_cmdshell 'net send "uf015930" "ACIARIA - PROCESSO BLOQUEADO A MAIS DE 10 MINUTOS - UTILIZE A SP_QUEM"'
if @waittime > 600000 exec master.dbo.xp_cmdshell 'net send "uf100160" "ACIARIA - PROCESSO BLOQUEADO A MAIS DE 10 MINUTOS - UTILIZE A SP_QUEM"'
if @waittime > 600000 exec master.dbo.xp_cmdshell 'net send "uf100445" "ACIARIA - PROCESSO BLOQUEADO A MAIS DE 10 MINUTOS - UTILIZE A SP_QUEM"'
if @waittime > 600000 exec master.dbo.xp_cmdshell 'net send "m2953" "ACIARIA - PROCESSO BLOQUEADO A MAIS DE 10 MINUTOS - VERIFIQUE NO PROCESS INFO"'
if @waittime > 90000 exec master.dbo.xp_cmdshell 'net send "m2954" "ACIARIA - PROCESSO BLOQUEADO A MAIS DE 2 MINUTOS - VERIFIQUE NO PROCESS INFO"'
*/
fetch next from cur_sp into @spid,@blocked,@waittime,@hostname,@program_name
end
close cur_sp
deallocate cur_sp
drop table #dbcc_output
GO
GO
USE [msdb]
GO
/****** Object: Job [GerenciaBD] Script Date: 26/08/2016 14:42:11 ******/
BEGIN TRANSACTION
DECLARE @ReturnCode INT
SELECT @ReturnCode = 0
/****** Object: JobCategory [[Uncategorized (Local)]] Script Date: 26/08/2016 14:42:12 ******/
IF NOT EXISTS (SELECT name FROM msdb.dbo.syscategories WHERE name=N'[Uncategorized (Local)]' AND category_class=1)
BEGIN
EXEC @ReturnCode = msdb.dbo.sp_add_category @class=N'JOB', @type=N'LOCAL', @name=N'[Uncategorized (Local)]'
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
END
DECLARE @jobId BINARY(16)
EXEC @ReturnCode = msdb.dbo.sp_add_job @job_name=N'GerenciaBD',
@enabled=1,
@notify_level_eventlog=2,
@notify_level_email=0,
@notify_level_netsend=0,
@notify_level_page=0,
@delete_level=0,
@description=N'No description available.',
@category_name=N'[Uncategorized (Local)]',
@owner_login_name=N'sa', @job_id = @jobId OUTPUT
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
/****** Object: Step [spu_GerenciaBD] Script Date: 26/08/2016 14:42:12 ******/
EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'spu_GerenciaBD',
@step_id=1,
@cmdexec_success_code=0,
@on_success_action=1,
@on_success_step_id=0,
@on_fail_action=2,
@on_fail_step_id=0,
@retry_attempts=10,
@retry_interval=3,
@os_run_priority=0, @subsystem=N'TSQL',
@command=N'Exec spu_GerenciaBD',
@database_name=N'msdb',
@flags=0
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_update_job @job_id = @jobId, @start_step_id = 1
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_add_jobschedule @job_id=@jobId, @name=N'A cada 3 Minutos',
@enabled=1,
@freq_type=4,
@freq_interval=1,
@freq_subday_type=4,
@freq_subday_interval=3,
@freq_relative_interval=0,
@freq_recurrence_factor=0,
@active_start_date=20100928,
@active_end_date=99991231,
@active_start_time=0,
@active_end_time=235959,
@schedule_uid=N'431ed25e-1597-45f5-a3c4-18a8896450f8'
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_add_jobserver @job_id = @jobId, @server_name = N'(local)'
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
COMMIT TRANSACTION
GOTO EndSave
QuitWithRollback:
IF (@@TRANCOUNT > 0) ROLLBACK TRANSACTION
EndSave:
GO
/****** Object: Job [Alerta - Monitora Processos Bloqueados] Script Date: 26/08/2016 14:42:12 ******/
BEGIN TRANSACTION
DECLARE @ReturnCode INT
SELECT @ReturnCode = 0
/****** Object: JobCategory [REPL-Checkup] Script Date: 26/08/2016 14:42:12 ******/
IF NOT EXISTS (SELECT name FROM msdb.dbo.syscategories WHERE name=N'REPL-Checkup' AND category_class=1)
BEGIN
EXEC @ReturnCode = msdb.dbo.sp_add_category @class=N'JOB', @type=N'LOCAL', @name=N'REPL-Checkup'
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
END
DECLARE @jobId BINARY(16)
EXEC @ReturnCode = msdb.dbo.sp_add_job @job_name=N'Alerta - Monitora Processos Bloqueados',
@enabled=1,
@notify_level_eventlog=0,
@notify_level_email=0,
@notify_level_netsend=0,
@notify_level_page=0,
@delete_level=0,
@description=N'Esse Job executa a stored procedure spu_checkblocking, que foi avaliada durante 30 dias. Como os resultados foram satisfatórios, foi implementada a funcionalidade de envio de mensagem para a Operação intervir em processos bloqueados a mais de 2 minutos.',
@category_name=N'REPL-Checkup',
@owner_login_name=N'sa', @job_id = @jobId OUTPUT
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
/****** Object: Step [spu_checkblocking] Script Date: 26/08/2016 14:42:12 ******/
EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'spu_checkblocking',
@step_id=1,
@cmdexec_success_code=0,
@on_success_action=1,
@on_success_step_id=0,
@on_fail_action=2,
@on_fail_step_id=0,
@retry_attempts=10,
@retry_interval=1,
@os_run_priority=0, @subsystem=N'TSQL',
@command=N'SET DEADLOCK_PRIORITY LOW
exec spu_checkblocking',
@database_name=N'msdb',
@flags=0
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_update_job @job_id = @jobId, @start_step_id = 1
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_add_jobschedule @job_id=@jobId, @name=N'schedule 2',
@enabled=1,
@freq_type=4,
@freq_interval=1,
@freq_subday_type=4,
@freq_subday_interval=1,
@freq_relative_interval=0,
@freq_recurrence_factor=0,
@active_start_date=20030429,
@active_end_date=99991231,
@active_start_time=30,
@active_end_time=235959,
@schedule_uid=N'edcbf2b4-61fa-4b8d-8cf1-97880d613bde'
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_add_jobserver @job_id = @jobId, @server_name = N'(local)'
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
COMMIT TRANSACTION
GOTO EndSave
QuitWithRollback:
IF (@@TRANCOUNT > 0) ROLLBACK TRANSACTION
EndSave:
GO