Checking
the databases availability is one of the major task of a DBA which has to
be performed at regular intervals to make sure that the
critical live databases are always online and fix them immediately if they are
not online. This can be done in "n" number of ways and here is
one such method.
The method that i am going to discuss below makes use of "Database Mail" option to send e-mails to the intended recipients when the process finds the status of the database which is not online.
After the "Database Mail" Option is configured all you have to do is to create a job with the below code and schedule it to run every five minutes or every one minute depending on the criticality of the databases that you are going to monitor.
The method that i am going to discuss below makes use of "Database Mail" option to send e-mails to the intended recipients when the process finds the status of the database which is not online.
After the "Database Mail" Option is configured all you have to do is to create a job with the below code and schedule it to run every five minutes or every one minute depending on the criticality of the databases that you are going to monitor.
Use this Code if you wish to receive the
e-mail in HTML format:
if(select count(*) from sys.databases where state_desc<>'Online')>0
Begin
DECLARE @table NVARCHAR(MAX) ;
SET @table =
N'<H1>Offline Databases Report</H1>' +
N'<table border="1">' +
N'<tr><th>Database Name</th><th>Database Status</th></tr>' +
CAST ( ( Select td=name, '',td=state_desc from sys.databases where state_desc<>'Online'
FOR XML PATH('tr'), TYPE
) AS NVARCHAR(MAX) ) +
N'</table>' ;
EXEC msdb.dbo.sp_send_dbmail @profile_name='ProfileName', --Change to your Profile Name
@recipients='email@domain.com;email1@domain.com', --Put the email address of those who want to receive the e-mail
@subject = 'Offline Databases Report',
@body = @table,
@body_format = 'HTML' ;
END
Else Print 'All Databases are Online'
Use this Code if you wish to receive the e-mail in Plain Text format:
if(select count(*) from sys.databases where state_desc<>'Online')>0
Begin
EXEC msdb.dbo.sp_send_dbmail @profile_name='ProfileName', --Change to your Profile Name
@recipients='email@domain.com;email1@domain.com', --Put the email address of those who want to receive the e-mail
@subject = 'Offline Databases Report',
@Query='Select
rtrim(ltrim(name)) as DatabaseName,rtrim(ltrim(state_desc)) as
CurrentDatabaseStatus from sys.databases where
state_desc<>''Online'''
END
Else Print 'All Databases are Online'
No comments:
Post a Comment