最近大致本身寫了點alwayson相關的監控,是經過存儲過程的方法,作個筆記以下:node
--alwayson啓用狀態 declare @isenabled sql_variant SELECT @isenabled=SERVERPROPERTY ( 'IsHadrEnabled' ) if @isenabled=1 begin print 'is enabled(alwayson啓用狀態)' end ---------監控集羣的狀態 if exists(SELECT * FROM sys.dm_hadr_cluster_members where member_state <>1) begin print 'alwayson集羣有拖機機器,請查看' end ------------監控副本同步狀態 if exists(select * from sys.dm_hadr_availability_replica_states where synchronization_health<>2) begin print 'alwayson集羣同步異常,請查看' end ---------------監控數據庫同步狀態 if exists(select * from sys.dm_hadr_database_replica_states where synchronization_state_desc NOT IN('SYNCHRONIZED','SYNCHRONIZED')) begin print 'alwayson有數據庫同步異常,請查看' end if exists( select * from sys.dm_tcp_listener_states where state_desc not in('ONLINE')) begin print 'alwayson偵聽器異常,請查看' end -----------------主從延遲的監控 IF EXISTS( SELECT ag.name AS ag_name, ar.replica_server_name AS ag_replica_server, dr_state.database_id as database_id, dr_state.log_send_queue_size, is_ag_replica_local = CASE WHEN ar_state.is_local = 1 THEN N'LOCAL' ELSE 'REMOTE' END , ag_replica_role = CASE WHEN ar_state.role_desc IS NULL THEN N'DISCONNECTED' ELSE ar_state.role_desc END FROM (( sys.availability_groups AS ag JOIN sys.availability_replicas AS ar ON ag.group_id = ar.group_id ) JOIN sys.dm_hadr_availability_replica_states AS ar_state ON ar.replica_id = ar_state.replica_id) JOIN sys.dm_hadr_database_replica_states dr_state on ag.group_id = dr_state.group_id and dr_state.replica_id = ar_state.replica_id where dr_state.log_send_queue_size>300 ) begin print 'alwayson主體有超過300M的日誌沒有同步,請儘快查看' end if exists( SELECT ag.name AS ag_name, ar.replica_server_name AS ag_replica_server, dr_state.database_id as database_id, dr_state.redo_queue_size, is_ag_replica_local = CASE WHEN ar_state.is_local = 1 THEN N'LOCAL' ELSE 'REMOTE' END , ag_replica_role = CASE WHEN ar_state.role_desc IS NULL THEN N'DISCONNECTED' ELSE ar_state.role_desc END FROM (( sys.availability_groups AS ag JOIN sys.availability_replicas AS ar ON ag.group_id = ar.group_id ) JOIN sys.dm_hadr_availability_replica_states AS ar_state ON ar.replica_id = ar_state.replica_id) JOIN sys.dm_hadr_database_replica_states dr_state on ag.group_id = dr_state.group_id and dr_state.replica_id = ar_state.replica_id where dr_state.redo_queue_size>300 ) begin print 'alwayson副本有較多日誌未重作,請登陸查看' end ---------------主從狀態切換等監控 DECLARE @MASTER VARCHAR(20) --select @MASTER=a.replica_server_name,a.join_state,a.join_state_desc,b.is_local ,b.role,b.role_desc from sys.dm_hadr_availability_replica_cluster_states a join sys.dm_hadr_availability_replica_states b --on a.replica_id=b.replica_id where b.role=1 --and a.replica_server_name='ALWAYSON241' select @MASTER=a.replica_server_name from sys.dm_hadr_availability_replica_cluster_states a join sys.dm_hadr_availability_replica_states b on a.replica_id=b.replica_id where b.role=1 --and a.replica_server_name='ALWAYSON241' print @MASTER IF @MASTER IS NULL BEGIN PRINT '主從同步異常' END ELSE IF @MASTER<>'ALWAYSON241' BEGIN PRINT 'alwayson進行了切換,如今主庫是'+@master END ------------------------監控alwayson數據庫狀態 if exists(select * from sys.dm_hadr_database_replica_states a where a.database_state<>0 or a.synchronization_state not in(1,2)) begin print 'alwayson有數據庫異常' end -----------發郵件預警 ----declare @name nvarchar(222) ----declare @mailbody nvarchar(4000); ----declare @maillabel nvarchar(100); ----set @maillabel='做業失敗' --EXEC msdb.dbo.sp_send_dbmail @profile_name = 'sendmail', -- @recipients = '729975475@qq.com', -- @subject = 'alwayson預警', -- @body = 'alwayson故障
於alwayson相關的系統視圖主要涉及如下一些(這些系統的存儲過程能讓咱們很直觀的瞭解到咱們可用性組的狀態):sql
--監控alwayson可用性管理器是否啓動 SELECT SERVERPROPERTY ( 'HadrManagerStatus' ) --監控alwayson可用性組是否啓動 SELECT SERVERPROPERTY ( 'IsHadrEnabled' ) --查看服務器的集羣信息 SELECT * FROM sys.dm_hadr_cluster SELECT * FROM sys.dm_hadr_cluster_members select * from sys.dm_hadr_availability_replica_cluster_nodes SELECT * FROM sys.dm_hadr_name_id_map ----------------------------------------------- SELECT * FROM sys.availability_groups SELECT * FROM sys.availability_groups_cluster SELECT * FROM sys.dm_hadr_availability_group_states --監控可用性副本 select * from sys.availability_replicas --查看只讀路由 select * from sys.availability_read_only_routing_lists ---監控可用性副本狀態 select * from sys.dm_hadr_availability_replica_cluster_states select * from sys.dm_hadr_availability_replica_states --synchronization_health的狀態爲2纔算正常。須要監控 --監視可用性數據庫 select * from sys.availability_databases_cluster select * from sys.databases where name in(select database_name from sys.availability_databases_cluster) --該視圖包含對應於給定主/輔助數據庫上最新自動頁修復嘗試的行,每一個數據庫最多可對應 100 行,若是存儲數據就要手動查看 select * from sys.dm_hadr_auto_page_repair --除了已同步和爲同步的alwayson的狀態 select * from sys.dm_hadr_database_replica_states where synchronization_state_desc NOT IN('SYNCHRONIZED','SYNCHRONIZED') -------------------------監視可用性組偵聽器的監控 select * from sys.availability_group_listener_ip_addresses where state_desc='ONLINE' select * from sys.availability_group_listeners -----咱們能夠用 NOT EXISTS來判斷 select dns_name,port,ip_address from sys.availability_group_listener_ip_addresses A JOIN sys.availability_group_listeners B ON a.listener_id=b.listener_id where a.state_desc='ONLINE' --------------------------------------- select * from sys.dm_tcp_listener_states where state_desc not in('ONLINE')