Created
September 23, 2022 13:52
-
-
Save spaghettidba/f611d00fd8ec93096deee34ba98525df to your computer and use it in GitHub Desktop.
HA status 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SET DEADLOCK_PRIORITY -10; | |
IF SERVERPROPERTY('EngineEdition') NOT IN (2,3,4) BEGIN /*NOT IN Standard,Enterpris,Express*/ | |
DECLARE @ErrorMessage AS nvarchar(500) = 'Telegraf - Connection string Server:'+ @@ServerName + ',Database:' + DB_NAME() +' is not a SQL Server Standard,Enterprise or Express. Check the database_type parameter in the telegraf configuration.'; | |
RAISERROR (@ErrorMessage,11,1) | |
RETURN | |
END | |
DECLARE | |
@SqlStatement AS nvarchar(max) | |
,@MajorMinorVersion AS int = CAST(PARSENAME(CAST(SERVERPROPERTY('ProductVersion') AS nvarchar),4) AS int)*100 + CAST(PARSENAME(CAST(SERVERPROPERTY('ProductVersion') AS nvarchar),3) AS int) | |
,@Columns AS nvarchar(MAX) = '' | |
IF @MajorMinorVersion >= 1200 BEGIN | |
SET @Columns += N' | |
,is_primary_replica' | |
END | |
IF @MajorMinorVersion >= 1300 BEGIN | |
SET @Columns += N' | |
,secondary_lag_seconds' | |
END | |
SET @SqlStatement = N' | |
IF SERVERPROPERTY(''IsHadrEnabled'') = 1 BEGIN | |
SELECT | |
''sqlserver_hadr_dbreplica_states'' AS [measurement] | |
,REPLACE(@@SERVERNAME,''\'','':'') AS [sql_instance] | |
,database_id | |
,db_name(database_id) as database_name | |
,convert(nvarchar(36), drs.replica_id) as replica_id | |
,ar.replica_server_name | |
,convert(nvarchar(36), drs.group_database_id) as group_database_id | |
,synchronization_state | |
,synchronization_state_desc | |
,is_commit_participant | |
,synchronization_health | |
,synchronization_health_desc | |
,database_state | |
,database_state_desc | |
,is_suspended | |
,suspend_reason | |
,suspend_reason_desc | |
,last_sent_time | |
,last_received_time | |
,last_hardened_time | |
,last_redone_time | |
,log_send_queue_size | |
,log_send_rate | |
,redo_queue_size | |
,redo_rate | |
,filestream_send_rate | |
,last_commit_time' | |
+ @Columns + N' | |
FROM sys.dm_hadr_database_replica_states AS drs | |
INNER JOIN sys.availability_replicas AS ar on drs.replica_id = ar.replica_id | |
END' | |
EXEC sp_executesql @SqlStatement |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment