forked from mtikoian/SQLDBA-SSMS-Solution
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathdbo.usp_replication_agent_checkup .sql
66 lines (59 loc) · 1.88 KB
/
dbo.usp_replication_agent_checkup .sql
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
use DBA
GO
IF OBJECT_ID('dbo.usp_replication_agent_checkup') IS NULL
EXECUTE ('create procedure dbo.usp_replication_agent_checkup as select 1 as dummy')
GO
ALTER procedure dbo.usp_replication_agent_checkup
@heartbeat_interval int = 10 -- minutes
,@recepients varchar(2000) = 'ajay.dwivedi@contso.com'
,@verbose bit = 0
,@perform_agent_restart bit = 0
as
set nocount on;
declare @distribution_db sysname
declare @retstatus int
declare @proc nvarchar(255)
declare @retcode int
/*
** Security Check: require sysadmin
*/
if @verbose = 1
print 'Checking if current executor is Sysadmin';
IF (ISNULL(IS_SRVROLEMEMBER('sysadmin'),0) = 0)
BEGIN
RAISERROR(21089,16,-1)
RETURN (1)
END
declare hCdistdbs CURSOR LOCAL FAST_FORWARD for
select name from msdb..MSdistributiondbs where
has_dbaccess(name) = 1
for read only
open hCdistdbs
fetch hCdistdbs into @distribution_db
while @@fetch_status <> -1
begin
select @proc = QUOTENAME(@distribution_db) + '.sys.sp_MSagent_retry_stethoscope'
if @verbose = 1
print 'execute '+@proc+';';
execute @retcode = @proc
if @@error <> 0 or @retcode <> 0
begin
select @retstatus = 1
goto UNDO
end
select @proc = QUOTENAME(@distribution_db) + '.sys.sp_MSagent_stethoscope'
if @verbose = 1
print 'execute '+@proc+' '+cast(@heartbeat_interval as varchar(10))+';';
execute @retcode = @proc @heartbeat_interval
if @@error <> 0 or @retcode <> 0
begin
select @retstatus = 1
goto UNDO
end
fetch hCdistdbs into @distribution_db
end
set @retstatus = 0
UNDO:
close hCdistdbs
deallocate hCdistdbs
return (@retstatus)