Общая схема работы:
Процедура генерации разметки:
CREATE OR ALTER PROCEDURE [dbo].[as_exception_diag]
@backendLimit int =20,
@frontendLimit int =20,
@s nvarchar(max) OUTPUT
AS
BEGIN
declare @t1 table (title nvarchar(max), db nvarchar(256), [desc] nvarchar(max),
digit nvarchar(512), important int)
IF OBJECT_ID('tempdb..#exceptions') IS NOT NULL DROP TABLE #exceptions
CREATE TABLE #exceptions ( created datetime, header nvarchar(512))
insert into #exceptions
select created, header from as_trace t3 where t3.code='exception' and cast(created as date) > cast(dateadd(day, -7, getdate()) as date)
declare @week int = isnull((select count(*) from #exceptions where cast(created as date) > cast(dateadd(day, -7, getdate()) as date) and CHARINDEX('jserror', header)<1 ), 0)
declare @yesterday int = isnull((select count(*) from #exceptions where cast(created as date) = cast(dateadd(day, -1, getdate()) as date) and CHARINDEX('jserror', header)<1 ), 0)
declare @today int = isnull((select count(*) from #exceptions where cast(created as date) = cast(getdate() as date) and CHARINDEX('jserror', header)<1 ), 0)
declare @lastHour int = isnull((select count(*) from #exceptions where created > dateadd(hour, -1, getdate()) and CHARINDEX('jserror', header)<1 ), 0)
declare @jsweek int = isnull((select count(*) from #exceptions where cast(created as date) > cast(dateadd(day, -7, getdate()) as date) and CHARINDEX('jserror', header)>0 ), 0)
declare @jsyesterday int = isnull((select count(*) from #exceptions where cast(created as date) = cast(dateadd(day, -1, getdate()) as date) and CHARINDEX('jserror', header)>0 ), 0)
declare @jstoday int = isnull((select count(*) from #exceptions where cast(created as date) = cast(getdate() as date) and CHARINDEX('jserror', header)>0), 0)
declare @jslastHour int = isnull((select count(*) from #exceptions where created > dateadd(hour, -1, getdate()) and CHARINDEX('jserror', header)>0), 0)
if(@yesterday>=@backendLimit) begin
declare @s1 nvarchar(max)=''
set @s1 = @s1 + '<h2>Backend errors</h2>'
set @s1 = @s1 + 'week - <b>' + cast(@week as nvarchar) + '</b><br>'
set @s1 = @s1 + 'yesterday - <b>' + cast(@yesterday as nvarchar) + '</b><br>'
set @s1 = @s1 + 'today - <b>' + cast(@today as nvarchar) + '</b><br>'
set @s1 = @s1 + 'lastHour - <b>' + cast(@lastHour as nvarchar) + '</b><br>'
set @s = @s+ @s1
END
if(@yesterday>=@frontendLimit) begin
declare @s2 nvarchar(max)=''
set @s2 = @s2 + '<h2>Frontend errors (JS)</h2>'
set @s2 = @s2 + 'week - <b>' + cast(@jsweek as nvarchar) + '</b><br>'
set @s2 = @s2 + 'yesterday - <b>' + cast(@jsyesterday as nvarchar) + '</b><br>'
set @s2 = @s2 + 'today - <b>' + cast(@jstoday as nvarchar) + '</b><br>'
set @s2 = @s2 + 'lastHour - <b>' + cast(@jslastHour as nvarchar) + '</b><br>'
set @s = @s+ @s2
END
DROP TABLE #exceptions
end
'
set @s1 = @s1 + 'week - ' + cast(@week as nvarchar) + '
'
set @s1 = @s1 + 'yesterday - ' + cast(@yesterday as nvarchar) + '
'
set @s1 = @s1 + 'today - ' + cast(@today as nvarchar) + '
'
set @s1 = @s1 + 'lastHour - ' + cast(@lastHour as nvarchar) + '
'
set @s = @s+ @s1
END
if(@yesterday>=@frontendLimit) begin
declare @s2 nvarchar(max)=''
set @s2 = @s2 + '
'
set @s2 = @s2 + 'week - ' + cast(@jsweek as nvarchar) + '
'
set @s2 = @s2 + 'yesterday - ' + cast(@jsyesterday as nvarchar) + '
'
set @s2 = @s2 + 'today - ' + cast(@jstoday as nvarchar) + '
'
set @s2 = @s2 + 'lastHour - ' + cast(@jslastHour as nvarchar) + '
'
set @s = @s+ @s2
END
end
@backendLimit задает минимальное количество backend исключений за вчера, при котором должно отправляться письмо на почту.
@frontendLimit задает минимальное количество backend исключений за вчера, при котором должно отправляться письмо на почту.
Процедура sync_day с отправкой сообщений об ошибках:
CREATE PROCEDURE [dbo].[falcon_nt_sync_day]
AS
BEGIN
declare @s nvarchar(max) = ''
exec [as_exception_diag] @backendLimit = 5, @frontendLimit = 10, @s=@s OUTPUT
if(len(@s)>0) begin
select 'email' type, 'mail@site.ru' [to], 'Ошибки на site.ru' subject, @s body
end
END