Monday, August 5, 2013

SQl Server 2008R2 mirroring monitoring

1) Go to Principle database and start GUI monitoring , stating the GUI will create the DBM_Monitor_data table in MSDB database
2) Register all the database for monitoring
3) View
select * from dbm_monitor_data

select @@VERSION

sp_dbmmonitorresults 'ControllerLive'

select * from dbo.dbm_monitor_alerts

exec sys.dbo.sysmail_help_principleprofile_sp

DECLARE @state VARCHAR(30)
DECLARE @DbMirrored INT
DECLARE @DbId INT
DECLARE @String VARCHAR(100)
DECLARE @databases TABLE (DBid INT, mirroring_state_desc VARCHAR(30))

-- get status for mirrored databases
INSERT @databases
SELECT database_id, mirroring_state_desc
FROM sys.database_mirroring
WHERE mirroring_role_desc IN ('PRINCIPAL','MIRROR')
AND mirroring_state_desc NOT IN ('SYNCHRONIZED','SYNCHRONIZING')

-- iterate through mirrored databases and send email alert
WHILE EXISTS (SELECT TOP 1 DBid FROM @databases WHERE mirroring_state_desc IS NOT NULL)
BEGIN
SELECT TOP 1 @DbId = DBid, @State = mirroring_state_desc
FROM @databases
SET @string = 'Host: '+@@servername+'.'+CAST(DB_NAME(@DbId) AS VARCHAR)+ ' - DB Mirroring is '+@state +' - notify DBA'
EXEC msdb.dbo.sp_send_dbmail 'DLVOTPRDSQL02_DBA', 'himanshu.sharma@dixonsretail.com', @body = @string, @subject = @string
DELETE FROM @databases WHERE DBid = @DbId
END

--also alert if there is no mirroring just in case there should be mirroring :)
SELECT @DbMirrored = COUNT(*)
FROM sys.database_mirroring
WHERE mirroring_state IS NOT NULL
IF @DbMirrored = 0
BEGIN
SET @string = 'Host: '+@@servername+' - No databases are mirrored on this server - notify DBA'
EXEC msdb.dbo.sp_send_dbmail 'DLVOTPRDSQL02_DBA', 'himanshu.sharma@dixonsretail.com', @body = @string, @subject = @string
END

No comments:

Post a Comment