Database Mirror Witness Status

Microsoft.SQLServer.2008.Mirroring.WitnessStateMonitor (UnitMonitor)

This monitor checks if database mirror witness is accessible.

Knowledge Base article:

Summary

This monitor checks the status of the database mirroring witness as reported by Microsoft® SQL Server™. This is performed by running a query against the master database of the SQL Server instance and returning the state of the database mirroring witness. If you receive an alert from this monitor, an action is required in order to bring the database mirroring witness back to an operational state.

Causes

Unhealthy state indicates that the SQL Server™ database mirroring witness is currently unavailable. All states except CONNECTED and NO-WITNESS will cause Unhealthy state. The possible states are detailed below:

Mirroring Witness State

Description

MOM Health State

CONNECTED

This is normal operational state. Mirroring Partner can access the Witness server.

HEALTHY

NO-WITNESS

This state indicates that there is no Witness specified for the mirroring session. It is normal operational state for High-Performance Mode and High-Safety Mode Without Automatic Failover.

HEALTHY

UNKNOWN

There are some issues with connection between Mirroring Partner and Witness server.

WARNING

DISCONNECTED

There is no connection between Mirroring Partner and Witness server.

CRITICAL

Resolutions

To resolve the issue try the following:

Overridable Parameters

Name

Description

Default Value

Alert Priority

Defines Alert Priority.

Normal

Alert Severity

Defines Alert Severity.

MatchMonitorHealth

Enabled

Enables or disables the workflow.

Yes

Generates Alerts

Defines whether the workflow generates an Alert.

Yes

Interval (seconds)

The recurring interval of time in seconds in which to run the workflow.

900

Synchronization Time

The synchronization time specified by using a 24-hour format. May be omitted.

 

Timeout (seconds)

Specifies the time the workflow is allowed to run before being closed and marked as failed.

300

Element properties:

TargetMicrosoft.SQLServer.2008.Mirroring.Database
Parent MonitorSystem.Health.AvailabilityState
CategoryAvailabilityHealth
EnabledTrue
Alert GenerateTrue
Alert SeverityMatchMonitorHealth
Alert PriorityNormal
Alert Auto ResolveTrue
Monitor TypeMicrosoft.SQLServer.2008.Mirroring.ScriptedThreeStateType
RemotableTrue
AccessibilityPublic
Alert Message
Database Mirror Witness is not accessible
Database mirror '{0}' in SQL Server instance '{1}' on computer '{2}' has lost connection to its witness '{3}'.
RunAsMicrosoft.SQLServer.SQLProbeAccount

Source Code:

<UnitMonitor ID="Microsoft.SQLServer.2008.Mirroring.WitnessStateMonitor" Accessibility="Public" Enabled="true" Target="SQL2008Mirroring!Microsoft.SQLServer.2008.Mirroring.Database" ParentMonitorID="Health!System.Health.AvailabilityState" TypeID="Microsoft.SQLServer.2008.Mirroring.ScriptedThreeStateType" Remotable="true" Priority="Normal" ConfirmDelivery="false" RunAs="SQL!Microsoft.SQLServer.SQLProbeAccount">
<Category>AvailabilityHealth</Category>
<AlertSettings AlertMessage="Microsoft.SQLServer.2008.Mirroring.WitnessStateMonitor.AlertMessage">
<AlertOnState>Warning</AlertOnState>
<AutoResolve>true</AutoResolve>
<AlertPriority>Normal</AlertPriority>
<AlertSeverity>MatchMonitorHealth</AlertSeverity>
<AlertParameters>
<AlertParameter1>$Target/Property[Type="SQL!Microsoft.SQLServer.Database"]/DatabaseName$</AlertParameter1>
<AlertParameter2>$Target/Host/Property[Type="SQL!Microsoft.SQLServer.ServerRole"]/InstanceName$</AlertParameter2>
<AlertParameter3>$Target/Host/Host/Property[Type="Windows!Microsoft.Windows.Computer"]/PrincipalName$</AlertParameter3>
<AlertParameter4>$Target/Property[Type="SQL2008Mirroring!Microsoft.SQLServer.2008.Mirroring.Database"]/Witness$</AlertParameter4>
</AlertParameters>
</AlertSettings>
<OperationalStates>
<OperationalState ID="Good" MonitorTypeStateID="Good" HealthState="Success"/>
<OperationalState ID="NeitherGoodNorBad" MonitorTypeStateID="NeitherGoodNorBad" HealthState="Warning"/>
<OperationalState ID="Bad" MonitorTypeStateID="Bad" HealthState="Error"/>
</OperationalStates>
<Configuration>
<ScriptName>GetSQL2008MirroringWitnessState.vbs</ScriptName>
<ScriptBody><Script>'#Include File:GetSQL2008MirroringWitnessState.vbs
'Copyright (c) Microsoft Corporation. All rights reserved.
' This script takes a single parameter
' Param 0: The SQL connection string to connect to

Const SQL_MONITORING_CONNECT_FAILURE = -1
Const SQL_MONITORING_QUERY_FAILURE = -2
Const SQL_MONITORING_SUCCESS = 0

Const SCRIPT_EVENT_ID = 4001

Dim GetWitnessStatusQuery
GetWitnessStatusQuery = "SELECT " &amp;_
" d.name, " &amp;_
" CASE WHEN dm.mirroring_witness_name = '' THEN 'NO-WITNESS' ELSE ISNULL(dm.mirroring_witness_state_desc, 'UNKNOWN') END " &amp;_
" FROM sys.databases d " &amp;_
" INNER JOIN sys.database_mirroring dm ON dm.database_id = d.database_id " &amp;_
" WHERE dm.mirroring_guid IS NOT NULL"

Call GetWitnessAccessibility()

Sub GetWitnessAccessibility()
If WScript.Arguments.Count = 4 Then
Dim oBag
Set oBag = oAPI.CreatePropertyBag()

Dim state, serviceName

serviceName = GetSQLServiceName(WScript.Arguments(2))
state = GetServiceState(WScript.Arguments(1), serviceName)

If (state &lt;&gt; "Running") And (state &lt;&gt; "Unknown") Then
Call oAPI.Return(oBag)
WScript.Quit()
End If

Dim nResult
nResult = WitnessAccessibility(WScript.Arguments(0), oBag, WScript.Arguments(1), WScript.Arguments(2), WScript.Arguments(3))
On Error Resume Next
Call GlobalErrorListToEventLog()
Call oAPI.Return(oBag)
Else
On Error Resume Next
Call GlobalErrorListToEventLog()
Wscript.Quit()
End If
End Sub

Function WitnessAccessibility(ByVal sSQLConnectionString, ByRef oBag, ByVal sComputerName, ByVal sInstanceName, ByVal sTcpPort)
Dim e
Set e = New Error

Dim cnADOConnection
Set cnADOConnection = SmartConnectWithoutSQLADODB(sSQLConnectionString, sTcpPort, sComputerName, sInstanceName, "master")
if cnADOConnection Is Nothing Then
WitnessAccessibility = SQL_MONITORING_CONNECT_FAILURE
Exit Function
End If

Dim oResults
e.Clear
On Error Resume Next
Set oResults = cnADOConnection.Execute(GetWitnessStatusQuery)
e.Save
On Error Goto 0
If e.Number &lt;&gt; 0 Then
WitnessAccessibility = SQL_MONITORING_QUERY_FAILURE
ThrowScriptErrorNoAbort "Query execution failed for '" &amp; sSQLConnectionString &amp; "' SQL Server", e
If (oResults &lt;&gt; null) Then oResults.Close
cnADOConnection.Close
Exit Function
End If

Do While Not oResults.EOF
Call oBag.AddValue(oResults(0) &amp; "-State", CStr(oResults(1)))
oResults.MoveNext
Loop

cnADOConnection.Close
WitnessAccessibility = SQL_MONITORING_SUCCESS
End Function
</Script></ScriptBody>
<IntervalSeconds>900</IntervalSeconds>
<SyncTime/>
<TimeoutSeconds>300</TimeoutSeconds>
<ConnectionString>$Target/Host/Property[Type="SQL!Microsoft.SQLServer.DBEngine"]/ConnectionString$</ConnectionString>
<GoodExpression>
<Or>
<Expression>
<SimpleExpression>
<ValueExpression>
<XPathQuery Type="String">Property[@Name='$Target/Property[Type="SQL!Microsoft.SQLServer.Database"]/DatabaseName$-State']</XPathQuery>
</ValueExpression>
<Operator>Equal</Operator>
<ValueExpression>
<Value Type="String">CONNECTED</Value>
</ValueExpression>
</SimpleExpression>
</Expression>
<Expression>
<SimpleExpression>
<ValueExpression>
<XPathQuery Type="String">Property[@Name='$Target/Property[Type="SQL!Microsoft.SQLServer.Database"]/DatabaseName$-State']</XPathQuery>
</ValueExpression>
<Operator>Equal</Operator>
<ValueExpression>
<Value Type="String">NO-WITNESS</Value>
</ValueExpression>
</SimpleExpression>
</Expression>
</Or>
</GoodExpression>
<NeitherGoodNorBadExpression>
<SimpleExpression>
<ValueExpression>
<XPathQuery Type="String">Property[@Name='$Target/Property[Type="SQL!Microsoft.SQLServer.Database"]/DatabaseName$-State']</XPathQuery>
</ValueExpression>
<Operator>Equal</Operator>
<ValueExpression>
<Value Type="String">UNKNOWN</Value>
</ValueExpression>
</SimpleExpression>
</NeitherGoodNorBadExpression>
<BadExpression>
<SimpleExpression>
<ValueExpression>
<XPathQuery Type="String">Property[@Name='$Target/Property[Type="SQL!Microsoft.SQLServer.Database"]/DatabaseName$-State']</XPathQuery>
</ValueExpression>
<Operator>Equal</Operator>
<ValueExpression>
<Value Type="String">DISCONNECTED</Value>
</ValueExpression>
</SimpleExpression>
</BadExpression>
</Configuration>
</UnitMonitor>