0% found this document useful (0 votes)
46 views4 pages

Query

This document defines a SQL Server Agent job that checks the health and synchronization status of AlwaysOn availability groups. It consists of two steps: 1. The first step queries database replica states and sends an email alert if any replicas are not healthy or synchronized. 2. The second step triggers additional alerts to operations if the first step has fired continuously for over 30 minutes. The job is configured to run every minute and send alerts to database and operations teams if issues are detected.

Uploaded by

Jagathis BigBoy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views4 pages

Query

This document defines a SQL Server Agent job that checks the health and synchronization status of AlwaysOn availability groups. It consists of two steps: 1. The first step queries database replica states and sends an email alert if any replicas are not healthy or synchronized. 2. The second step triggers additional alerts to operations if the first step has fired continuously for over 30 minutes. The job is configured to run every minute and send alerts to database and operations teams if issues are detected.

Uploaded by

Jagathis BigBoy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

USE [msdb]

GO

/****** Object: Job [DBAdmin - AlwaysOn_Synchronization Health] Script Date:


3/29/2022 5:56:31 PM ******/
BEGIN TRANSACTION
DECLARE @ReturnCode INT
SELECT @ReturnCode = 0
/****** Object: JobCategory [[Uncategorized (Local)]] Script Date: 3/29/2022
5:56:31 PM ******/
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'DBAdmin -
AlwaysOn_Synchronization Health',
@enabled=1,
@notify_level_eventlog=0,
@notify_level_email=2,
@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'sqladmin',
@notify_email_operator_name=N'DBA', @job_id = @jobId OUTPUT
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
/****** Object: Step [Check_AOAG_Synchronization Health] Script Date: 3/29/2022
5:56:31 PM ******/
EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId,
@step_name=N'Check_AOAG_Synchronization Health',
@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'declare @cnt int;
declare @cnt2 int;
declare @stop nvarchar(200);
declare @disable nvarchar(200);
declare @JobName nvarchar(200);
declare @servername nvarchar(200);
set @servername = @@SERVERNAME;
declare @subject nvarchar(200);
set @subject = ''AOAG_Production SYNCHRONIZATION HEALTH Alert on : ''+@servername;

DECLARE @HADRSERVERNAME VARCHAR(25)


SET @HADRSERVERNAME = @@SERVERNAME
SELECT CLUSTERNODES.GROUP_NAME AS [AVAILABILITY GROUP NAME],
CLUSTERNODES.REPLICA_SERVER_NAME AS [AVAILABILITY REPLICA NAME],
CLUSTERNODES.NODE_NAME AS [AVAILABILITY NODE],
RS.ROLE_DESC AS [ROLE],
DB_NAME(DRS.DATABASE_ID) AS [AVAILABILITY DATABASE],
DRS.SYNCHRONIZATION_STATE_DESC AS [SYNCHRONIZATION STATUS],
DRS.SYNCHRONIZATION_HEALTH_DESC AS [SYNCHRONIZATION HEALTH]
into #AOAGTemp
FROM SYS.DM_HADR_AVAILABILITY_REPLICA_CLUSTER_NODES CLUSTERNODES
full JOIN SYS.DM_HADR_AVAILABILITY_REPLICA_CLUSTER_STATES CLUSTERSTATS
ON CLUSTERNODES.REPLICA_SERVER_NAME = CLUSTERSTATS.REPLICA_SERVER_NAME
full JOIN SYS.DM_HADR_AVAILABILITY_REPLICA_STATES RS
ON RS.REPLICA_ID = CLUSTERSTATS.REPLICA_ID
full JOIN SYS.DM_HADR_DATABASE_REPLICA_STATES DRS
ON RS.REPLICA_ID = DRS.REPLICA_ID
where DRS.SYNCHRONIZATION_HEALTH_DESC != ''HEALTHY'' or
DRS.SYNCHRONIZATION_STATE_DESC not in(''SYNCHRONIZED'',''SYNCHRONIZING'')
order by DRS.SYNCHRONIZATION_HEALTH_DESC, CLUSTERNODES.GROUP_NAME ,
RS.ROLE_DESC

select @cnt=count(1)from #AOAGTemp

declare @tableHTML2 nvarchar(MAX)


SET @tableHTML2 =
N''<H1 style="color:red;">AOAG Synchronization Health Alert! Please check!</H1>'' +
N''<table border="1">'' +
N''<tr><th width= "25%">AvailabilityGroupName</th><th width=
"25%">AvailabilityReplicaName</th><th width= "25%">AvailabilityReplicaRole</th><th
width= "25%">AvailabilityDBName</th>
<th width= "25%">SynchronizationStatus</th><th width=
"30%">SynchronizationHealth</th>'' +
CAST ( ( SELECT td = [AVAILABILITY GROUP NAME], '''',
td = [AVAILABILITY REPLICA NAME], '''',
td = [ROLE], '''',
td = [AVAILABILITY DATABASE] , '''',
td = [SYNCHRONIZATION STATUS], '''',
td = [SYNCHRONIZATION HEALTH] ,''''
FROM (select * from #AOAGTemp ) a
FOR XML PATH(''tr''), TYPE
) AS NVARCHAR(MAX) ) +
N''</table>'' ;

if @cnt > 0
begin -- Here are the long running jobs
exec msdb.dbo.sp_send_dbmail

@recipients=''db.team@ogglobal.net;hw.foo@ogglobal.net'',
--@recipients= ''hw.foo@ogglobal.net'',
@subject=@subject,
@body=@tableHTML2,
@body_format =''HTML''
end

drop table #AOAGTemp',


@database_name=N'master',
@flags=0
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
/****** Object: Step [Trigger PS for AOAG_Synchronization Health] Script Date:
3/29/2022 5:56:31 PM ******/
EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'Trigger PS
for AOAG_Synchronization Health',
@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'Declare @sql varchar(150)

IF (select count(1) from msdb.dbo.sysmail_sentitems


where SUBJECT like ''%AOAG_Production Synchronization Health%'' and
sent_date>=DATEADD(minute,-30,getdate())
and sent_date<=getdate() ) > 5

begin

select @sql=@@servername
set @sql =''Continuous AlwaysOn SynchronizationHealth alert on Production Server
'' + @sql

EXEC msdb.dbo.sp_send_dbmail
@recipients=''db.team@ogglobal.net;ps@ogglobal.net;lf.noc@ogglobal.net;sql-
server-alert@funpodium.pagerduty.com'',
@body=''Continuous AlwaysOn SynchronizationHealth alert. Please check.'',
@subject =@sql
--@profile_name =''DBA monitor''
end
',
@database_name=N'master',
@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'Every 1
minutes',
@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=20210608,
@active_end_date=99991231,
@active_start_time=0,
@active_end_time=235959,
@schedule_uid=N'0e4ede3e-52ac-40c4-9c94-d6f6b3285592'
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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy