security_error.sql 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /*
  2. T-SQL script to fetch security_error_ring_buffer_recorded information from the system health extended event session.
  3. Author: Amit Banerjee
  4. Contact details:
  5. Blog: www.troubleshootingsql.com
  6. Twitter: http://twitter.com/banerjeeamit
  7. DISCLAIMER:
  8. This Sample Code is provided for the purpose of illustration only and is not intended to be used in a production environment. THIS SAMPLE CODE AND ANY RELATED INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. We grant You a nonexclusive, royalty-free right to use and modify the Sample Code and to reproduce and distribute the object code form of the Sample Code, provided that You agree: (i) to not use Our name, logo, or trademarks to market Your software product in which the Sample Code is embedded; (ii) to include a valid copyright notice on Your software product in which the Sample Code is embedded; and (iii) to indemnify, hold harmless, and defend Us and Our suppliers from and against any claims or lawsuits, including attorneys’ fees, that arise or result from the use or distribution of the Sample Code.
  9. */
  10. SET NOCOUNT ON
  11. -- Fetch data for only SQL Server 2012 instances
  12. IF (SUBSTRING(CAST(SERVERPROPERTY ('ProductVersion') AS varchar(50)),1,CHARINDEX('.',CAST(SERVERPROPERTY ('ProductVersion') AS varchar(50)))-1) >= 11)
  13. BEGIN
  14. -- Get UTC time difference for reporting event times local to server time
  15. DECLARE @UTCDateDiff int = DATEDIFF(mi,GETUTCDATE(),GETDATE());
  16. -- Store XML data retrieved in temp table
  17. SELECT TOP 1 CAST(xet.target_data AS XML) AS XMLDATA
  18. INTO #SystemHealthSessionData
  19. FROM sys.dm_xe_session_targets xet
  20. JOIN sys.dm_xe_sessions xe
  21. ON (xe.address = xet.event_session_address)
  22. WHERE xe.name = 'system_health'
  23. AND xet.target_name = 'ring_buffer';
  24. -- Parse XML data and provide required values in the form of a table
  25. ;WITH CTE_HealthSession (EventXML) AS
  26. (
  27. SELECT C.query('.') EventXML
  28. FROM #SystemHealthSessionData a
  29. CROSS APPLY a.XMLDATA.nodes('/RingBufferTarget/event') as T(C)
  30. )
  31. SELECT DATEADD(mi,@UTCDateDiff,EventXML.value('(/event/@timestamp)[1]', 'datetime')) as EventTime,
  32. EventXML.value('(/event/data/value)[3]', 'int') as SessionID,
  33. EventXML.value('(/event/data/value)[4]', 'int') as ErrorCode,
  34. EventXML.value('(/event/data/value)[5]', 'varchar(100)') as APIName,
  35. EventXML.value('(/event/data/value)[6]', 'varchar(100)') as CallingAPIName,
  36. EventXML.value('(/event/data/value)[7]', 'nvarchar(max)') as CallStack
  37. FROM CTE_HealthSession
  38. WHERE EventXML.value('(/event/@name)[1]', 'varchar(255)') = 'security_error_ring_buffer_recorded'
  39. ORDER BY EventTime;
  40. -- Drop the temporary table
  41. DROP TABLE #SystemHealthSessionData;
  42. END
  43. SET NOCOUNT OFF