Thursday, May 17, 2018

uspEmailDatabaseLogStatus: Monitoring Log files

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'dbo.uspEmailDatabaseLogStatus') AND type in (N'P', N'PC'))
DROP PROCEDURE dbo.uspEmailDatabaseLogStatus
GO

--------------------------------------------------------------------------------
-- =============================================
-- Author: Simon You
-- Create date: May 2018
-- Description:
-- =============================================
create procedure dbo.uspEmailDatabaseLogStatus
as
begin
set nocount on;

declare
@body varchar(max),
@text varchar(max),
@SuccessCount int,
@FailedCount int,
@NotRunCount int,
@EmailProfile dbo.EmailProfile,
@EmailFrom dbo.EmailFrom,
@EmailTo dbo.EmailTo,
@EmailCc dbo.EmailCc,
@RedAlertPercent int = 5,
@YellowAlertPercent int = 10,
@EmailCode dbo.EmailCode = 'LogFile_Report',
@Date date = GETDATE(),
@Subject varchar(max)

if dbo.fnRequireEmail(@Date, @EmailCode) = 0
begin
return
end

select
@EmailProfile = EmailProfile,
@EmailFrom = EmailFrom,
@EmailTo = EmailTo,
@EmailCc = EmailCc
from dbo.SettingEmail
where EmailCode = @EmailCode

if @@ROWCOUNT = 0
begin
return
end;

set @Subject = 'Log file Report for ' + convert(varchar(10), @Date, 120)

  set @text =
CAST( (
select
td = left(dovs.volume_mount_point, 1), '',
td = mf.name, '',
td = mf.physical_name, '',
[td/@align] = 'right',
td = REPLACE(CONVERT(varchar, CAST(cast(mf.size * 8 / 1024.0 as numeric(36, 0)) AS MONEY), 1), '.00' ,''), ''
FROM sys.master_files mf
CROSS APPLY sys.dm_os_volume_stats(mf.database_id, mf.FILE_ID) dovs
where left(dovs.volume_mount_point, 1) in ('H', 'O')
and name like '%log'
order by mf.size desc, mf.name
for xml path( 'tr' ), type
) as varchar(max) )

set @body = 'The following summary displays Log file size on Phone Channel Data Warehouse Server (i.e.: CRSDPSSCA1IDCS\PC):

'
+ ''

+ '' +
+ ' DriveNamePhysical nameFile Size
(GB) '
+ replace( replace( @text, '<', '<' ), '>', '>' )
+ '
'
--print @body
exec uspSendEmail
@EmailCode = @EmailCode,
@EmailProfile = @EmailProfile,
@EmailFrom = @EmailFrom,
@EmailTo = @EmailTo,
@EmailCc = @EmailCc,
@Subject = @Subject,
@body = @body
end
--------------------------------------------------------------------------------
GO

--exec uspEmailDatabaseLogStatus
/*
declare @date date = '2018-02-05'
exec uspEmailStorageStatus @date
*/
--select * from dbo.CtsJob

No comments: