create procedure sys.sp_MSinit_replication_perfmon
as
begin
declare @return_code int,
@agent_type int,
@agent_name nvarchar(100),
@status int,
@all_but_misc_agents int
select @all_but_misc_agents = -1
-- drop the temp table
if object_id('tempdb.dbo.#tmp_replication_status') is not null
begin
drop table #tmp_replication_status
end
-- create the temp table
create table #tmp_replication_status
(
publisher sysname,
publisher_db sysname,
publication sysname,
publication_type int,
agent_type int,
status int,
agent_id int,
agent_name sysname,
job_id uniqueidentifier null,
time_stamp datetime null,
publisher_srvid int null
)
create clustered index ctmpreplicationstatus on #tmp_replication_status (publication, publisher_db, publisher)
create index nc1tmpreplicationstatus on #tmp_replication_status (publisher, publisher_db)
create index nc2tmpreplicationstatus on #tmp_replication_status (agent_type)
-- Remove all existing instances
dbcc deleteinstance ("SQL Replication Agents", "%")
dbcc deleteinstance ("SQL Replication Snapshot", "%")
dbcc deleteinstance ("SQL Replication Logreader", "%")
dbcc deleteinstance ("SQL Replication Distribution", "%")
dbcc deleteinstance ("SQL Replication Merge", "%")
dbcc deleteinstance ("SQL Replication Queuereader", "%")
-- Add and initialize Perfmon SQL Replication Agents instances
dbcc addinstance ("SQL Replication Agents", "Snapshot")
dbcc addinstance ("SQL Replication Agents", "Logreader")
dbcc addinstance ("SQL Replication Agents", "Distribution")
dbcc addinstance ("SQL Replication Agents", "Merge")
dbcc addinstance ("SQL Replication Agents", "Queuereader")
dbcc setinstance ("SQL Replication Agents", "Running", "Snapshot", 0)
dbcc setinstance ("SQL Replication Agents", "Running", "Logreader", 0)
dbcc setinstance ("SQL Replication Agents", "Running", "Distribution", 0)
dbcc setinstance ("SQL Replication Agents", "Running", "Merge", 0)
dbcc setinstance ("SQL Replication Agents", "Running", "Queuereader", 0)
-- load tmp replication_status table
exec @return_code = sys.sp_MSload_tmp_replication_status @agent_type = @all_but_misc_agents
if @@error <> 0 or @return_code <> 0
return 1
-- Add instances for each agent currently in the status table
declare hC CURSOR LOCAL FAST_FORWARD for
select agent_type,
agent_name,
status
from #tmp_replication_status for read only
open hC
fetch hC into @agent_type, @agent_name, @status
while (@@fetch_status <> -1)
begin
if @agent_type = 1
begin
dbcc addinstance ("SQL Replication Snapshot", @agent_name)
if @status = 1 or @status = 3 or @status = 4
dbcc incrementinstance ("SQL Replication Agents", "Running", "Snapshot", 1)
end
else if @agent_type = 2
begin
dbcc addinstance ("SQL Replication Logreader", @agent_name)
if @status = 1 or @status = 3 or @status = 4
dbcc incrementinstance ("SQL Replication Agents", "Running", "Logreader", 1)
end
else if @agent_type = 3
begin
dbcc addinstance ("SQL Replication Distribution", @agent_name)
if @status = 1 or @status = 3 or @status = 4
dbcc incrementinstance ("SQL Replication Agents", "Running", "Distribution", 1)
end
else if @agent_type = 4
begin
dbcc addinstance ("SQL Replication Merge", @agent_name)
if @status = 1 or @status = 3 or @status = 4
dbcc incrementinstance ("SQL Replication Agents", "Running", "Merge", 1)
end
else if @agent_type = 9
begin
dbcc addinstance ("SQL Replication Queuereader", @agent_name)
if @status = 1 or @status = 3 or @status = 4
dbcc incrementinstance ("SQL Replication Agents", "Running", "Queuereader", 1)
end
fetch hC into @agent_type, @agent_name, @status
end
close hC
deallocate hC
end