Configure SQL Server Alerts and Notifications for AlwaysOn Availability Groups

By:   |   Comments (6)   |   Related: > Availability Groups


Problem

The next step after you have deployed and successfully configured an AlwaysOn Availability Group is to maintain it. As a DBA you would want to know immediately if something critical happens. But what is the most important alert that you would like be notified first and foremost? You would probably want to be alerted if a failover happens to your AlwaysOn production setup right?

Solution

There are a lot of monitoring alerts that you can setup for AlwaysOn Availability Groups, but the most critical alerts that you need to setup are for these 3 scenarios below:

  1. When the replica changes role or a failover to secondary replica happens
  2. When data movement is suspended
  3. When data movement resumed

So lets proceed to create the alerts for each of the scenarios above. By now creating alerts should be a breeze for you. But if this the first time you are creating one, don't fret. You have an option of doing it via the SQL Server Agent GUI or by scripting.

The easiest way to do this is via scripting, but you may choose to do this using SQL Server Management Studio (SSMS). In SSMS, open Object Explorer, go to SQL Server Agent > Alerts > New Alerts. This should open up a new alert dialog window for you.

But in this tip, I am going to show how to do this via scripting.

Create SQL Server Alert for AlwaysOn Role Change

EXEC msdb.dbo.sp_add_alert
@name=N'AlwaysOn - Role Change',
@message_id=1480,
@severity=0,
@enabled=1,
@delay_between_responses=0,
@include_event_description_in=0,
@category_name=N'[Uncategorized]', 
@job_id=N'00000000-0000-0000-0000-000000000000'
GO

The message_id is the error id for the role change or failover event, for this event the value is 1480.

For the job_id value, you have an option to create a SQL Server Agent Job that will run when this alert is fired.  The job_id would be the job_id for the SQL Server Agent Job.  You can run sp_help_job to get a list of SQL Agent jobs and the job_id value.

Create SQL Server Alert for AlwaysOn Data Movement Suspended

To setup an alert for the data movement suspended, you can run this code, but note we are looking for message_id 35264.

EXEC msdb.dbo.sp_add_alert
@name=N'AlwaysOn - Data Movement Suspended',
@message_id=35264,
@severity=0,
@enabled=1,
@delay_between_responses=0,
@include_event_description_in=0, 
@category_name=N'[Uncategorized]',
@job_id=N'00000000-0000-0000-0000-000000000000'
GO

Create SQL Server Alert for AlwaysOn Data Movement Resumed

To setup an alert for the data movement resumed, you can run this code, but note we are looking for message_id 35265.

EXEC msdb.dbo.sp_add_alert
@name=N'AlwaysOn - Data Movement Resumed',
@message_id=35265,
@severity=0,
@enabled=1,
@delay_between_responses=0,
@include_event_description_in=0,
@category_name=N'[Uncategorized]',
@job_id=N'00000000-0000-0000-0000-000000000000'
GO

Setup SQL Server Alert Notifications

After you have created the alerts, the next thing to do is to add notifications to your alerts, so you get alerted if the scenario described above happens. You may again do this via the SSMS or alternatively update and execute the script below based on the values in your environment. This would need to be done for each alert that you create. Also, you will need to setup Database Mail and Operators.

EXEC msdb.dbo.sp_add_notification
@alert_name = N'AlwaysOn - Role Change',
@operator_name = N'[email protected]',
@notification_method = <Notification,INT,1>;
GO

When doing this via the SSMS, open your newly created Alerts, go to Response on the left menu and check the Notify Operators checkbox. On the name of your Operator, check the correct email checkbox.

Summary

As I have mentioned, it is best to script and deploy these alerts for your other servers where AlwaysOn is setup and configured, I advise you to create and save a script for the alert creation and adding of notifications. To easily create the scripts for existing alerts, from within SSMS just right click on the Alert and select the Script option.

Next Steps


sql server categories

sql server webinars

subscribe to mssqltips

sql server tutorials

sql server white papers

next tip



About the author
MSSQLTips author Carla Abanes Carla Abanes is a certified Microsoft SQL Server Database Administrator with extensive experience in data architecture and supporting mission critical databases in production environments.

This author pledges the content of this article is based on professional experience and not AI generated.

View all my tips



Comments For This Article




Monday, February 26, 2024 - 11:29:32 PM - Ata Back To Top (92012)
Hi,

Thank you for the script. However, I recommend changing the @delay_between_responses to at least 120 (which is equal to 2 minutes). Otherwise, you will receive thousands of emails for a simple restart on one of the servers.

Thanks,

Ata

Tuesday, November 1, 2022 - 2:48:29 PM - Mir Back To Top (90654)
Alert not working. Even I tested manually suspending the database but no luck

Tuesday, April 6, 2021 - 12:13:50 AM - Syed Abul Farhan Back To Top (88493)
Thanks for the Alerts, its working good for me. I am experiencing some different issue. my Cloud witness goes down frequently. i need to generate alert on this. any idea about message id.

Wednesday, July 11, 2018 - 9:21:16 AM - Annie Back To Top (76610)

Hi Carla,

Thanks for your alert tips for the alwayson error logs. I am new to the MSSQL DB and have a  questions in viweing these alerts.

I have setup these alerts through the scripts  and tested the Alsways Group by shutting down or testing the failover to get these errors.

But how can view these particular error_ids through command. I normallt view all the errors in error logs.

Thanks


Monday, February 9, 2015 - 10:25:24 PM - Carla Abanes Back To Top (36189)

Thanks for sharing this too, Danilo :)


Friday, January 23, 2015 - 3:03:16 PM - Danilo Braga Back To Top (36043)

Carla Abanes, 

Thank you for sharing your experience.

I've been reading all of your posts and everyone are very good, mainly because they are focused on AlwaysOn Availability Group.

I think you can also include more two alerts to know if occur timeout between primary and secondary replicas. It's useful if your environment has multiple subnets in different datacenters where you secondary replica is configured for asynchronous mode.

 

-- 35206 - AG Timeout to Secondary Replica

EXEC msdb .dbo . sp_add_alert

        @name = N'AG Timeout to Secondary Replica',

        @message_id = 35206,

    @severity = 0,

    @enabled = 1,

    @delay_between_responses = 0,

    @include_event_description_in = 1;

GO

-- 35202 - AG Timeout to Secondary Replica

EXEC msdb .dbo . sp_add_alert

        @name = N'AG Connection has been successfully established',

        @message_id = 35202,

    @severity = 0,

    @enabled = 1,

    @delay_between_responses = 0,

    @include_event_description_in = 1;

 

 

Danilo Cunha Braga

https://twitter.com/danilocbraga

 

https://br.linkedin.com/pub/danilo-cunha-braga/21/324/805















get free sql tips
agree to terms