USE [system]
GO
CREATE TABLE [dbo].[DBA_Repl_Error_Log](
 [ID] [int] IDENTITY(1,1) NOT NULL,
 [SerName] [varchar](100) NULL,
 [agent_id] [int] NULL,
 [publisher] [varchar](200) NULL,
 [publisher_db] [varchar](200) NULL,
 [publication] [varchar](200) NULL,
 [subscriber] [varchar](200) NULL,
 [subscriber_db] [varchar](200) NULL,
 [runstatus] [varchar](200) NULL,
 [undlv_trans] [int] NULL,
 [undlv_cmds] [int] NULL,
 [prcs_time] [varchar](200) NULL,
 [comments] [varchar](2000) NULL,
 [cur_cmds/s] [int] NULL,
 [avg_cmds/s] [int] NULL,
 [cur_latency] [int] NULL,
 [avg_latency] [int] NULL,
 [start_time] [datetime] NULL,
 [run_duration] [varchar](200) NULL,
 [dlv_trans] [int] NULL,
 [dlv_cmds] [int] NULL,
 [cmds/tran] [int] NULL,
 [total_dlv_cmds] [bigint] NULL,
 [error_id] [int] NULL,
 [update_time] [datetime] NULL,
 [Active_Time] [datetime] NULL,
PRIMARY KEY CLUSTERED
(
 [ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
CREATE TABLE [dbo].[DBA_Repl_Usability](
 [ID] [int] IDENTITY(1,1) NOT NULL,
 [SerName] [varchar](100) NULL,
 [Error_Con] [int] NULL,
 [All_Con] [int] NULL,
 [AVG_Latency] [numeric](18, 2) NULL,
 [Active_Time] [datetime] NULL,
PRIMARY KEY CLUSTERED
(
 [ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO


Use distribution
GO
create   FUNCTION [dbo].[udf_Duration_IntToChar]
   (@duration int)
RETURNS varchar(50)
AS
BEGIN
 declare @duration_char varchar(50)

 set @duration_char=case when (@duration/3600)>9 then ltrim(str(@duration/3600))
       else right('0'+ltrim(str(@duration/3600)),2) end +':'
  +right('0'+ltrim(str((@duration%3600)/60)),2)+':'
  +right('0'+ltrim(str((@duration%3600)%60)),2)
 RETURN @duration_char
END

GO


USE [msdb]
GO

BEGIN TRANSACTION
DECLARE @ReturnCode INT
SELECT @ReturnCode = 0
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'DBA1@replication_monitor',
  @enabled=1,
  @notify_level_eventlog=0,
  @notify_level_email=0,
  @notify_level_netsend=0,
  @notify_level_page=0,
  @delete_level=0,
  @description=N'',
  @category_name=N'[Uncategorized (Local)]',
  @owner_login_name=N'sasa', @job_id = @jobId OUTPUT
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'JGJ复制可用性记录',
  @step_id=1,
  @cmdexec_success_code=0,
  @on_success_action=3,
  @on_success_step_id=0,
  @on_fail_action=3,
  @on_fail_step_id=0,
  @retry_attempts=0,
  @retry_interval=0,
  @os_run_priority=0, @subsystem=N'TSQL',
  @command=N'Use distribution
GO
declare @Error_Con int,@All_Con int,@Getdate datetime,@AVG_Latency numeric(18, 2)
set @Getdate=getdate()
exec sp_repl_syn
insert System.dbo.DBA_Repl_Error_Log(SerName,agent_id,publisher,publisher_db,publication,subscriber,subscriber_db,runstatus,undlv_trans,undlv_cmds,prcs_time,comments,[cur_cmds/s],[avg_cmds/s],cur_latency,avg_latency,start_time,run_duration,dlv_trans,dlv_cmds,[cmds/tran],total_dlv_cmds,error_id,update_time,Active_Time)
select ''JGJ'',agent_id,publisher,publisher_db,publication,subscriber,subscriber_db,runstatus,undlv_trans,undlv_cmds,prcs_time,comments,[cur_cmds/s],[avg_cmds/s],cur_latency,avg_latency,start_time,run_duration,dlv_trans,dlv_cmds,[cmds/tran],total_dlv_cmds,error_id,update_time,@Getdate
from ##out where comments not like ''%transaction%'' --失败的代理
set @Error_Con= @@rowcount
select @All_Con=count(1),@AVG_Latency=AVG(AVG_Latency)
from ##out
insert system.dbo.DBA_Repl_Usability(SerName,Error_Con,All_Con,AVG_Latency,Active_Time)
select ''JGJ'',@Error_Con,@All_Con,@AVG_Latency,@GetDate',
  @database_name=N'distribution',
  @flags=0
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'JGJ',
  @step_id=2,
  @cmdexec_success_code=0,
  @on_success_action=1,
  @on_success_step_id=0,
  @on_fail_action=2,
  @on_fail_step_id=0,
  @retry_attempts=0,
  @retry_interval=0,
  @os_run_priority=0, @subsystem=N'TSQL',
  @command=N'exec sp_repl_alert
@ToMail = ''guijun.jia@corp.elong.com'',
@ToPhone = ''15321755775''',
  @database_name=N'distribution',
  @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'1',
  @enabled=1,
  @freq_type=4,
  @freq_interval=1,
  @freq_subday_type=4,
  @freq_subday_interval=5,
  @freq_relative_interval=0,
  @freq_recurrence_factor=0,
  @active_start_date=20090813,
  @active_end_date=99991231,
  @active_start_time=100,
  @active_end_time=235959
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