join the MSSQLTips community

Today's Site Sponsor


 

SQL diagnostic manager gives us analysis, monitoring, alerting and reporting that would satisfy even the pickiest DBA!
 



Ever feel like a human database, trying to remember...

Monitoring SQL Server Database Mirroring with Email Alerts

Written By: Alan Cranfield -- 10/15/2009 -- read/post comments -- print -- Bookmark and Share

Rating: (not rated yet) Rate

Problem
The number of Database Mirror pairs in our enterprise is increasing at an alarming rate. With the ease of setup, resilience and ability to seamlessly failover and failback, database mirroring has taken over from log shipping as the DR solution of choice. We need a basic script that we can execute on all servers participating in mirroring that will alert us if any Principals or Mirrors are in an abnormal state. The script needs to work on both Principal and Mirror server.

Solution
The Catalog View sys.database_mirroring contains one row for each database in the instance of SQL Server and also contains state information of all mirrored databases.  We'll query this Catalog View and raise an email alert for each mirrored database that we find in an abnormal state. We don't utilize a witness server in any of our mirrored pairs so we rely on manual failover.

Prerequisites

  1. A valid Database mail profile
  2. A valid login that has permission to send email i.e. a member of the DatabaseMailUserRole role in the msdb database
  3. At least one pair of mirrored databases to monitor

The Script

Substitute in the below script your Database Mail profile and a suitable email address to receive the alerts.

DECLARE @state VARCHAR(30)

DECLARE @DbMirrored INT

DECLARE @DbId INT

DECLARE @String VARCHAR(100)

DECLARE @databases TABLE (DBid INT, mirroring_state_desc VARCHAR(30))

 

-- get status for mirrored databases

INSERT @databases

SELECT database_id, mirroring_state_desc

FROM sys.database_mirroring

WHERE mirroring_role_desc IN ('PRINCIPAL','MIRROR')

AND mirroring_state_desc NOT IN ('SYNCHRONIZED','SYNCHRONIZING')

 

-- iterate through mirrored databases and send email alert

WHILE EXISTS (SELECT TOP 1 DBid FROM @databases WHERE mirroring_state_desc IS NOT NULL)

BEGIN

SELECT TOP 1 @DbId = DBid, @State = mirroring_state_desc

FROM @databases

SET @string = 'Host: '+@@servername+'.'+CAST(DB_NAME(@DbId) AS VARCHAR)+ ' - DB Mirroring is '+@state +' - notify DBA'

EXEC msdb.dbo.sp_send_dbmail 'valid_mail_profile', 'DBA@mssqltips.com', @body = @string, @subject = @string

DELETE FROM @databases WHERE DBid = @DbId

END

 

--also alert if there is no mirroring just in case there should be mirroring :)

SELECT @DbMirrored = COUNT(*)

FROM sys.database_mirroring

WHERE mirroring_state IS NOT NULL

IF @DbMirrored = 0

BEGIN

SET @string = 'Host: '+@@servername+' - No databases are mirrored on this server - notify DBA'

EXEC msdb.dbo.sp_send_dbmail 'valid_mail_profile', 'DBA@mssqltips.com', @body = @string, @subject = @string

END

Practice

To verify the alert let's pause mirroring through the SQL Server Management Studio (SSMS). In Object Explorer right click your mirrored database -> Properties -> Mirroring option -> Pause button as follows:

Now, if the query finds a mirrored database in an abnormal state as shown below, it will send out an alert email alert:

The script will also output the following information to notify you that an alert has been sent to be processed by Database Mail.

Mirroring Alert Message

-----------------------

Host: SERVERXXX.MirrorTest - DB Mirroring is Suspended - notify DBA

 

Mail queued.

 

 

Mirroring Alert Message

-----------------------

Host: SERVERXXX.sp_config_ssp2 - DB Mirroring is DISCONNECTED - notify DBA

 

Mail queued.

 

Email received that clearly shows the host name, database name and the abnormal state of the mirroring so that the DBA team can investigate further.

Next Steps

Readers Who Read This Tip Also Read Free Live Webcast Comment or Ask Questions About This Tip


Sponsor Information
Free SQL Server performance monitoring dashboard – Idera SQL check

Increase your SQL speed and accuracy with code completion from SQL Prompt.

You don't know, what you don't know about SQL Server... Customized Consulting and Training

Join the MSSQLTips LinkedIn Group

Free whitepaper - Developing Something for Nothing with SQL Server: A Closer Look at SQL Server Express


Get Our Tips Newsletter

We keep 50,000+ SQL Server professionals informed.

Red Gate Software - SQL Compare

Quickly and accurately deploy database changes with Red Gate’s SQL Compare. 2/3 of people who use a SQL comparison tool use Red Gate’s SQL Compare – the industry standard database comparison and deployment tool. “We rely on SQL Compare for every deployment.” Paul Tebbut, Technical Lead, Universal Music Group

Download now!



More SQL Server Tools
SQL defrag manager

SQL diagnostic manager

SQL Compare

SQL Prompt

SQL compliance manager




Copyright (c) 2006-2010 Edgewood Solutions, LLC All rights reserved
privacy statement | disclaimer | copyright | advertise | write for mssqltips | feedback | about
Some names and products listed are the registered trademarks of their respective owners.


CareerQandA.com | MSSharePointTips.com | MSSQLTips.com