DECLARE @FileName NVARCHAR(4000)
SELECT @FileName = target_data.value('(EventFileTarget/File/@name)[1]', 'nvarchar(4000)')
FROM (
SELECT CAST(target_data AS XML) target_data
FROM sys.dm_xe_sessions s
JOIN sys.dm_xe_session_targets t
ON s.address = t.event_session_address
WHERE s.name = N'AlwaysOn_health'
) ft;
WITH base
AS (
SELECT XEData.value('(event/@timestamp)[1]', 'datetime2(3)') AS event_timestamp
,XEData.value('(event/data/text)[1]', 'VARCHAR(255)') AS previous_state
,XEData.value('(event/data/text)[2]', 'VARCHAR(255)') AS current_state
,ar.replica_server_name
FROM (
SELECT CAST(event_data AS XML) XEData
,*
FROM sys.fn_xe_file_target_read_file(@FileName, NULL, NULL, NULL)
WHERE object_name = 'availability_replica_state_change'
) event_data
JOIN sys.availability_replicas ar
ON ar.replica_id = XEData.value('(event/data/value)[5]', 'VARCHAR(255)')
)
SELECT DATEADD(HOUR, DATEDIFF(HOUR, GETUTCDATE(), GETDATE()), event_timestamp) AS event_timestamp
,previous_state
,current_state
,replica_server_name
FROM base
ORDER BY event_timestamp DESC;
这是我提取 Aaron 提到的事件 (availability_replica_state_change) 的首选查询。此脚本还可以将 UTC 时间转换为您的本地时间:
availability_replica_state_change
在AlwaysOn_health
扩展事件会话中查找事件。如果此会话未运行:更多信息:
SQL Server 2012 RC0 中的新 AlwaysOn_health 扩展事件会话
使用 AlwaysOn 仪表板回答问题
监控可用性组第 3 部分 - 扩展事件
AlwaysOn 扩展事件
我使用了从EXCHANGE SPILL获得的以下查询,但稍作修改以将时间从 UTC 转换为本地时间。