Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Data Management

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « Series of "Bad habits to kick" by Aaron BertrandSQL University and why you should be attending »
    comments

    This morning I came into work and went through my usual 100 or so emails. One of the emails was from MSSQLTips.com, it was on how to monitor SQL Server Database mirroring with email alerts. By Alan Cranfield. While agree with Alan that every DBA should monitor their database mirroring with email alerts I disagreed with his method. He had the DBA create a job that was scheduled to run at some interval throughout the day. His job would query the sys.database_mirroring view. As DBAs we need to know immediately when something fails or changes. 5 minutes could be the difference between a quick fix and restoring a 500 GB db mirror.

    So what would be a better way to monitor and alert a DBA when there is a change in the state of Database mirroring? I prefer to use Alerts for events. Event notifications can be created directly in the SQL Server Database Engine or by using the WMI Provider for Server Events. A DBA can specify which db mirroring event they wish to moitor. Here is a table of events to monitor for:

    StateNameDescription
    0Null NotificationThis state occurs briefly when a mirroring session is started.
    1Synchronized Principal with WitnessThis state occurs on the principal when the principal and mirror are connected and synchronized and the principal and witness are connected. For a mirroring configuration with a witness, this is the normal operating state.
    2Synchronized Principal without WitnessThis state occurs on the principal when the principal and mirror are connected and synchronized but the principal does not have a connection to the witness. For a mirroring configuration without a witness, this is the normal operating state.
    3Synchronized Mirror with WitnessThis state occurs on the mirror when the principal and mirror are connected and synchronized and the mirror and witness are connected. For a mirroring configuration with a witness, this is the normal operating state.
    4Synchronized Mirror without WitnessThis state occurs on the mirror when the principal and mirror are connected and synchronized but the mirror does not have a connection to the witness. For a mirroring configuration without a witness, this is the normal operating state.
    5Connection with Principal LostThis state occurs on the mirror server instance when it cannot connect to the principal.
    6Connection with Mirror LostThis state occurs on the principal server instance when it cannot connect to the mirror.
    7Manual FailoverThis state occurs on the principal server instance when the user fails over manually from the principal, or on the mirror server instance when a force service is executed at the mirror.
    8Automatic FailoverThis state occurs on the mirror server instance when the operating mode is high safety with automatic failover (synchronous) and the mirror and witness server instances cannot connect to the principal server instance.
    9Mirroring SuspendedThis state occurs on both partner instances when the user suspends (pauses) the mirroring session or when the mirror server instance encounters an error. It also occurs on the mirror server instance following a force service command. When the mirror comes online as the principal, mirroring is automatically suspended.
    10No QuorumIf a witness is configured, this state occurs on the principal or mirror server instance when it cannot connect to its partner or to the witness server instance.
    11Synchronizing MirrorThis state occurs on the mirror server instance when there is a backlog of unsent log. The status of the session is Synchronizing.
    12Principal Running ExposedThis state occurs on the principal server instance when the operating mode is high protection (synchronous) and the principal cannot connect to the mirror server instance.
    13Synchronizing PrincipalThis state occurs on the principal server instance when there is a backlog of unsent log. The status of the

    Now that we know the Event and the State here is how to add an Alert to notify you that the state of DB mirroring has changed.

    1. USE [msdb]
    2. GO
    3.  
    4. /****** Object:  Alert [DBM State Change]    Script Date: 10/15/2009 08:03:20 ******/
    5. EXEC msdb.dbo.sp_add_alert @name=N'DBM State Change',
    6.         @message_id=0,
    7.         @severity=0,
    8.         @enabled=1,
    9.         @delay_between_responses=0,
    10.         @include_event_description_in=1,
    11.         @category_name=N'[Uncategorized]',
    12.         @wmi_namespace=N'\\.\root\Microsoft\SqlServer\ServerEvents\MSSQLSERVER',
    13.         @wmi_query=N'SELECT * FROM DATABASE_MIRRORING_STATE_CHANGE WHERE State = 6 ',
    14.         @job_id=N'00000000-0000-0000-0000-000000000000'
    15. GO

    This is an alert that I created on the principal server. I also have created a similar alert on the mirror server where I look for state = 5. These two alerts will notify me if the connection between the Principal and Mirror is lost due to network or some other failure.
    To receive notification when this event happens it is simple to just create an operator and have the event email the operator if and when the event conditions are met.

    What other Mirror Events should every DBA monitor? I find the unsent and unrestored log to be two very import events to receive notifications for. For those events just simply create a new event for the event ID in the table below and set you monitor threshold.

    Database Mirroring Monitor warningEvent nameEvent ID
    Warn if the unsent log exceeds the thresholdUnsent log32042
    Warn if the unrestored log exceeds the thresholdUnrestored log32043
    Warn if the age of the oldest unsent transaction exceeds the thresholdOldest unsent transaction32044
    Warn if the mirror commit overhead exceeds the thresholdMirror commit overhead32045

    You can also script this by using sp_add_alert as follows:

    1. USE [msdb]
    2. GO
    3.  
    4. /****** Object:  Alert [DB Mirroring Unsent Log Warning]    Script Date: 10/15/2009 08:14:29 ******/
    5. EXEC msdb.dbo.sp_add_alert @name=N'DB Mirroring Unsent Log Warning',
    6.         @message_id=32042,
    7.         @severity=0,
    8.         @enabled=0,
    9.         @delay_between_responses=0,
    10.         @include_event_description_in=1,
    11.         @category_name=N'[Uncategorized]',
    12.         @job_id=N'00000000-0000-0000-0000-000000000000'
    13. GO

    Good Luck and Happy monitoring!

    About the Author

    Paul is a SR DBA and one of the Co-Founders of LessThanDot. Paul has over 12 years experience in RDBMS for Financial and Manufacturing companies and experienced with AS400, DB2, Oracle, Sybase and SQL Server. Currently living in the Newport, Rhode Island area with his wife and 5 children Paul dedicates his spare time to his family and coaching Little League and serving as the President of Middletown Little League Board of Directors.
    Social SitingsTwitterLinkedInLTD RSS Feed
    9671 views
    Instapaper

    14 comments

    Comment from: George Mastros (gmmastros) [Member]
    *****
    George Mastros (gmmastros) Excellent tip!
    10/15/09 @ 07:32
    Comment from: Ted Krueger (onpnt) [Member]
    *****
    Ted Krueger (onpnt) I know I'm doing things inline with the Sr DBAs now :P

    Great blog!
    10/15/09 @ 07:39
    Comment from: Christiaan Baes (chrissie1) [Member]
    Christiaan Baes (chrissie1) That's the first, they are always the most difficult one.
    10/15/09 @ 08:21
    Comment from: SQLDenis [Member] Email
    *****
    SQLDenis back in the day (on sql 2000) I also used net send because every now and then email would be delayed or could be stuck and then you would get it 3 hours later :-)

    but yes Alerts are the way to go...you can use them for hardware errors, percent log used, deadlocks notifications, full tempdb etc etc

    10/15/09 @ 08:46
    Comment from: John [Visitor] · http://www.johnsterrett.com
    John You did a great job on this post. I implemented your solution to configuring alerts and I have a question. How do you get useful data in your email?

    the following is a sample body of the email generated.

    DATE/TIME: 10/22/2009 1:56:25 PM

    DESCRIPTION:

    COMMENT: (None)

    JOB RUN: (None)
    10/22/09 @ 12:20
    Comment from: ptheriault [Member] Email
    *****
    ptheriault In order to get more useful data you would have to query the WMI Event data. You can also insert the data in to a history table in the msdb database if you want to keep a records of the events. If you check out this link from msdn it goes into great detail and even provides scripts to set it all up.
    http://msdn.microsoft.com/en-us/library/cc966392.aspx
    10/23/09 @ 06:40
    Comment from: Alan Cranfield [Visitor]
    Alan Cranfield I appreciate the mention of my mssqltip. Following on I have now decided to implement more granular alerting by setting performance thresholds. I have been hitting what appears to be bugs with SQL in that the warning threshold event will hit the error log but the alert will not fire OR the alert will fire (if you omit the database name - !bug!) but an email will not he sent to the operator.

    Have you hit any of these bugs in your testing:
    (http://social.msdn.microsoft.com/forums/en-US/sqldatabasemirroring/thread/e4899020-731c-4a9a-b2f2-172b78f607f7/)

    This is the reason why I implement simple mirroring status alert as its not dependant on multiple layers to get the alert through to the DBA.
    10/27/09 @ 09:57
    Comment from: Andrey Samykin [Visitor]
    Andrey Samykin pay attention: according to Microsoft docs third and forth Event IDs are incorrect!

    http://msdn.microsoft.com/en-us/library/ms408393(SQL.90).aspx

    Oldest unsent transaction 32040
    Mirror commit overhead 32044
    04/14/10 @ 08:21
    Comment from: Nik D [Visitor]
    Nik D What I did is used the alerts to fire up the job that is created by Alan Cranfield.. better that i do not have to run the job and can get appropriate emails as well
    01/18/11 @ 14:16
    Comment from: Dev [Visitor]
    Dev Nik D, Can you please share the job script?
    02/01/11 @ 10:05
    Comment from: jbineyti [Visitor] Email · http://www.bineyti.com
    jbineyti Hi,
    Thank you very much for hints and scripts. I'm not familiar with T-SQL and SQL DB, so I don't know how to execute the T-SQL scripts. I am working for 2 years with SCOM. I have to monitor mirroring status changes and to create an alert on the SCOM Console. Do anybody have an example monitor, how can I get the status changes of mirrored DB?
    Thanks in advance
    07/28/11 @ 03:14
    Comment from: Danp [Visitor] · http://madebysql.blogspot.com/
    Danp Great Info
    Pingback
    http://madebysql.blogspot.com/2011/08/4-ways-to-efficiently-monitor-db.html
    08/05/11 @ 06:20
    Comment from: rowlandg [Member] Email
    rowlandg Thanks for an insightful post!

    08/10/11 @ 08:58
    Comment from: Bedlam69 [Visitor]
    Bedlam69 The alerts are firing properly but I have not received any mails yet. Hope you've a solution for this.
    09/08/11 @ 04:57

    Leave a comment


    Your email address will not be revealed on this site.

    Your URL will be displayed.
    (Line breaks become <br />)
    (Name, email & website)
    (Allow users to contact you through a message form (your email will not be revealed.)