Created
July 29, 2013 21:20
-
-
Save mbourgon/6107964 to your computer and use it in GitHub Desktop.
Use Event Notifications to send a 3-stage email based off of collected ERRORLOG messages. Default is 1 day, configure the date as you want by modifying around POST_TIME
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SET NOCOUNT ON | |
SET ANSI_NULLS ON | |
SET QUOTED_IDENTIFIER ON | |
IF OBJECT_ID('tempdb.dbo.#errors_mail') IS NOT NULL | |
DROP TABLE #errors_mail; | |
WITH cte | |
AS (SELECT servername | |
,loginname | |
,PostTime | |
,C.value('ApplicationName[1]', 'varchar(200)') AS ApplicationName | |
,DatabaseName | |
,C.value('TextData[1]', 'varchar(max)') AS TextData | |
,C.value('HostName[1]', 'varchar(200)') AS HostName | |
FROM EventNotificationRec..ENAudit_Events | |
OUTER APPLY message_body_xml.nodes('EVENT_INSTANCE') AS T (C) | |
WHERE EventType = 'ERRORLOG' | |
) | |
SELECT cte.* | |
INTO #errors_mail | |
FROM cte | |
LEFT JOIN EventNotificationRec.dbo.EN_sql_errors_exclusion_email e WITH (NOLOCK) | |
ON cte.textdata LIKE '%' + e.exclusions + '%' | |
WHERE --textdata LIKE '%severity%' --AND textdata NOT LIKE '%Login failed for user%' | |
[PostTime] > getdate()-1 --DATEADD(dd, DATEDIFF(dd, 1, GETDATE()), 0) | |
AND e.exclusions IS NULL | |
ORDER BY posttime DESC | |
DECLARE @tableHTML NVARCHAR(MAX) | |
,@MailSubject VARCHAR(100) | |
SELECT @MailSubject = 'Event Notifications - ' + CONVERT(VARCHAR,COUNT(*)) + ' SQL Errors since ' + CONVERT(VARCHAR(20),GETDATE()-1) | |
FROM #errors_mail | |
SET @tableHTML = N'<H3>Severity Errors since ' + CONVERT(VARCHAR(20), GETDATE()-1) + '</H3>' | |
+ N'<table border="1">' + N'<tr><th> ServerName </th>' + N'<th> LoginName </th>' | |
+ N'<th> PostTime </th>' + N'<th>ApplicationName</th>' + N'<th>DatabaseName</th>' + N'<th>TextData</th>' | |
+ N'<th> HostName </th></tr>' + ISNULL(CAST((SELECT td = RTRIM(LTRIM(ISNULL(T.ServerName,''))) | |
,'' | |
,td = RTRIM(LTRIM(ISNULL(T.LoginName,''))) | |
,'' | |
,td = ISNULL(CONVERT(VARCHAR(16), T.PostTime, 120),'') | |
,'' | |
,td = ISNULL(RTRIM(LTRIM(T.ApplicationName)),'') | |
,'' | |
,td = ISNULL(RTRIM(LTRIM(T.DatabaseName)),'') | |
,'' | |
,td = ISNULL(RTRIM(LTRIM(T.TextData)),'') | |
,'' | |
,td = ISNULL(RTRIM(LTRIM(T.HostName)),'') | |
FROM #errors_mail T | |
WHERE textdata NOT LIKE '%Login failed for%' | |
AND textdata LIKE '%Severity%' | |
ORDER BY T.ServerName, T.PostTime DESC | |
FOR | |
XML PATH('tr') | |
,TYPE | |
) AS NVARCHAR(MAX)),'') + N'</table>'; | |
--now add nonseverity errors | |
SET @tableHTML = ISNULL(@tableHTML,'') + '<br><br>' + N'<H3>non-Severity Errors since ' + CONVERT(VARCHAR(20), GETDATE()-1) + '</H3>' | |
+ N'<table border="1">' + N'<tr><th> ServerName </th>' + N'<th> LoginName </th>' | |
+ N'<th> PostTime </th>' + N'<th>ApplicationName</th>' + N'<th>DatabaseName</th>' + N'<th>TextData</th>' | |
+ N'<th> HostName </th></tr>' + ISNULL(CAST((SELECT td = RTRIM(LTRIM(ISNULL(T.ServerName,''))) | |
,'' | |
,td = RTRIM(LTRIM(ISNULL(T.LoginName,''))) | |
,'' | |
,td = ISNULL(CONVERT(VARCHAR(16), T.PostTime, 120),'') | |
,'' | |
,td = ISNULL(RTRIM(LTRIM(T.ApplicationName)),'') | |
,'' | |
,td = ISNULL(RTRIM(LTRIM(T.DatabaseName)),'') | |
,'' | |
,td = ISNULL(RTRIM(LTRIM(T.TextData)),'') | |
,'' | |
,td = ISNULL(RTRIM(LTRIM(T.HostName)),'') | |
FROM #errors_mail T | |
WHERE textdata NOT LIKE '%Severity%' | |
ORDER BY T.ServerName, T.PostTime DESC | |
FOR | |
XML PATH('tr') | |
,TYPE | |
) AS NVARCHAR(MAX)),'') + N'</table>'; | |
SET @tableHTML = ISNULL(@tableHTML,'') + '<br><br>' + N'<H3>Login Errors since ' + CONVERT(VARCHAR(20), GETDATE()-1) + '</H3>' | |
+ N'<table border="1">' + N'<tr><th> ServerName </th>' + N'<th> TextData </th>' | |
+ N'<th> Count(*) </th>' + N'<th>Started</th>' | |
+ N'<th> Ended </th></tr>' + ISNULL(CAST((SELECT td = RTRIM(LTRIM(ISNULL(T.ServerName,''))) | |
,'' | |
,td = ISNULL(RTRIM(LTRIM(SUBSTRING(textdata,CHARINDEX('Login failed for', textdata), 200))),'') | |
,'' | |
,td = isnull(CONVERT(VARCHAR(25), NULLIF(COUNT(*),0)),'') | |
,'' | |
,td = isnull(CONVERT(VARCHAR(16), MIN(posttime), 120),'') | |
,'' | |
,td = isnull(CONVERT(VARCHAR(16), Max(posttime), 120),'') | |
FROM #errors_mail T | |
WHERE textdata LIKE '%Login failed for%' | |
GROUP BY t.servername, SUBSTRING(textdata,CHARINDEX('Login failed for', textdata), 200) | |
ORDER BY T.ServerName, SUBSTRING(textdata,CHARINDEX('Login failed for', textdata), 200) DESC | |
FOR | |
XML PATH('tr') | |
,TYPE | |
) AS NVARCHAR(MAX)),'') + N'</table>'; | |
EXEC msdb.dbo.sp_send_dbmail @profile_name = 'yournamegoeshere', @recipients = '[email protected]', | |
@subject = @MailSubject, @body = @tableHTML, @body_format = 'HTML'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment