Skip to content

Commit a2bea2f

Browse files
committed
#3362 Rewrite of check 192 and 193
1 parent 4bb747a commit a2bea2f

File tree

1 file changed

+134
-115
lines changed

1 file changed

+134
-115
lines changed

sp_Blitz.sql

Lines changed: 134 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -8674,122 +8674,141 @@ IF @ProductVersionMajor >= 10 AND NOT EXISTS ( SELECT 1
86748674
EXECUTE(@StringToExecute);
86758675
END;
86768676

8677-
/*
8678-
Starting with SQL Server 2014 SP2, Instant File Initialization
8679-
is logged in the SQL Server Error Log.
8680-
*/
8681-
IF NOT EXISTS ( SELECT 1
8682-
FROM #SkipChecks
8683-
WHERE DatabaseName IS NULL AND CheckID = 193 )
8684-
AND ((@ProductVersionMajor >= 13) OR (@ProductVersionMajor = 12 AND @ProductVersionMinor >= 5000))
8685-
BEGIN
8686-
8687-
IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 193) WITH NOWAIT;
8688-
8689-
-- If this is Amazon RDS, use rdsadmin.dbo.rds_read_error_log
8690-
IF LEFT(CAST(SERVERPROPERTY('ComputerNamePhysicalNetBIOS') AS VARCHAR(8000)), 8) = 'EC2AMAZ-'
8691-
AND LEFT(CAST(SERVERPROPERTY('MachineName') AS VARCHAR(8000)), 8) = 'EC2AMAZ-'
8692-
AND db_id('rdsadmin') IS NOT NULL
8693-
AND EXISTS(SELECT * FROM master.sys.all_objects WHERE name IN ('rds_startup_tasks', 'rds_help_revlogin', 'rds_hexadecimal', 'rds_failover_tracking', 'rds_database_tracking', 'rds_track_change'))
8694-
BEGIN
8695-
INSERT INTO #ErrorLog
8696-
EXEC rdsadmin.dbo.rds_read_error_log 0, 1, N'Database Instant File Initialization: enabled';
8697-
END
8698-
ELSE
8699-
BEGIN
8700-
BEGIN TRY
8701-
INSERT INTO #ErrorLog
8702-
EXEC sys.xp_readerrorlog 0, 1, N'Database Instant File Initialization: enabled';
8703-
END TRY
8704-
BEGIN CATCH
8705-
IF @Debug IN (1, 2) RAISERROR('No permissions to execute xp_readerrorlog.', 0, 1) WITH NOWAIT;
8706-
END CATCH
8707-
END
8708-
8709-
IF EXISTS
8710-
(
8711-
SELECT 1/0
8712-
FROM #ErrorLog
8713-
WHERE LEFT([Text], 45) = N'Database Instant File Initialization: enabled'
8714-
)
8715-
BEGIN
8716-
INSERT INTO #BlitzResults
8717-
( CheckID ,
8718-
[Priority] ,
8719-
FindingsGroup ,
8720-
Finding ,
8721-
URL ,
8722-
Details
8723-
)
8724-
SELECT
8725-
193 AS [CheckID] ,
8726-
250 AS [Priority] ,
8727-
'Server Info' AS [FindingsGroup] ,
8728-
'Instant File Initialization Enabled' AS [Finding] ,
8729-
'https://www.brentozar.com/go/instant' AS [URL] ,
8730-
'The service account has the Perform Volume Maintenance Tasks permission.';
8731-
END;
8732-
else -- if version of sql server has instant_file_initialization_enabled column in dm_server_services, check that too
8733-
-- in the event the error log has been cycled and the startup messages are not in the current error log
8734-
begin
8735-
if EXISTS ( SELECT *
8736-
FROM sys.all_objects o
8737-
INNER JOIN sys.all_columns c ON o.object_id = c.object_id
8738-
WHERE o.name = 'dm_server_services'
8739-
AND c.name = 'instant_file_initialization_enabled' )
8740-
begin
8741-
SET @StringToExecute = N'
8742-
INSERT INTO #BlitzResults
8743-
( CheckID ,
8744-
[Priority] ,
8745-
FindingsGroup ,
8746-
Finding ,
8747-
URL ,
8748-
Details
8749-
)
8750-
SELECT
8751-
193 AS [CheckID] ,
8752-
250 AS [Priority] ,
8753-
''Server Info'' AS [FindingsGroup] ,
8754-
''Instant File Initialization Enabled'' AS [Finding] ,
8755-
''https://www.brentozar.com/go/instant'' AS [URL] ,
8756-
''The service account has the Perform Volume Maintenance Tasks permission.''
8757-
where exists (select 1 FROM sys.dm_server_services
8758-
WHERE instant_file_initialization_enabled = ''Y''
8759-
AND filename LIKE ''%sqlservr.exe%'')
8760-
OPTION (RECOMPILE);';
8761-
EXEC(@StringToExecute);
8762-
end;
8763-
end;
8764-
END;
8677+
/* Performance - Instant File Initialization Not Enabled - Check 192 */
8678+
/* Server Info - Instant File Initialization Enabled - Check 193 */
8679+
IF NOT EXISTS ( SELECT 1/0
8680+
FROM #SkipChecks
8681+
WHERE DatabaseName IS NULL AND CheckID = 192 /* IFI disabled check disabled */
8682+
) OR NOT EXISTS
8683+
( SELECT 1/0
8684+
FROM #SkipChecks
8685+
WHERE DatabaseName IS NULL AND CheckID = 193 /* IFI enabled check disabled */
8686+
)
8687+
BEGIN
8688+
IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d] and CheckId [%d].', 0, 1, 192, 193) WITH NOWAIT;
8689+
8690+
DECLARE @IFISetting varchar(1) = N'N'
8691+
,@IFIReadDMVFailed bit = 0
8692+
,@IFIAllFailed bit = 0;
8693+
8694+
BEGIN TRY
8695+
/* See if we can get the instant_file_initialization_enabled column from sys.dm_server_services */
8696+
SET @StringToExecute = N'
8697+
SELECT @IFISetting = instant_file_initialization_enabled
8698+
FROM sys.dm_server_services
8699+
WHERE filename LIKE ''%sqlservr.exe%''
8700+
OPTION (RECOMPILE);';
8701+
8702+
IF @Debug = 2 AND @StringToExecute IS NOT NULL PRINT @StringToExecute;
8703+
IF @Debug = 2 AND @StringToExecute IS NULL PRINT '@StringToExecute has gone NULL, for some reason.';
8704+
8705+
EXEC dbo.sp_executesql
8706+
@StringToExecute
8707+
,N'@IFISetting varchar(1) OUTPUT'
8708+
,@IFISetting = @IFISetting OUTPUT
8709+
END TRY
8710+
BEGIN CATCH
8711+
/* We couldn't get the instant_file_initialization_enabled column from sys.dm_server_services, fall back to read error log */
8712+
SET @IFIReadDMVFailed = 1;
8713+
END CATCH;
8714+
8715+
IF @IFIReadDMVFailed = 1
8716+
BEGIN
8717+
/* If this is Amazon RDS, we'll use the rdsadmin.dbo.rds_read_error_log */
8718+
IF LEFT(CAST(SERVERPROPERTY('ComputerNamePhysicalNetBIOS') AS VARCHAR(8000)), 8) = 'EC2AMAZ-'
8719+
AND LEFT(CAST(SERVERPROPERTY('MachineName') AS VARCHAR(8000)), 8) = 'EC2AMAZ-'
8720+
AND db_id('rdsadmin') IS NOT NULL
8721+
AND EXISTS ( SELECT 1/0
8722+
FROM master.sys.all_objects
8723+
WHERE name IN ('rds_startup_tasks', 'rds_help_revlogin', 'rds_hexadecimal', 'rds_failover_tracking', 'rds_database_tracking', 'rds_track_change')
8724+
)
8725+
BEGIN
8726+
/* Amazon RDS detected, read rdsadmin.dbo.rds_read_error_log */
8727+
INSERT INTO #ErrorLog
8728+
EXEC rdsadmin.dbo.rds_read_error_log 0, 1, N'Database Instant File Initialization: enabled';
8729+
END
8730+
ELSE
8731+
BEGIN
8732+
/* Try to read the error log, this might fail due to permissions */
8733+
BEGIN TRY
8734+
INSERT INTO #ErrorLog
8735+
EXEC sys.xp_readerrorlog 0, 1, N'Database Instant File Initialization: enabled';
8736+
END TRY
8737+
BEGIN CATCH
8738+
IF @Debug IN (1, 2) RAISERROR('No permissions to execute xp_readerrorlog.', 0, 1) WITH NOWAIT;
8739+
SET @IFIAllFailed = 1;
8740+
END CATCH
8741+
END;
8742+
END;
8743+
8744+
IF @IFIAllFailed = 0
8745+
BEGIN
8746+
IF @IFIReadDMVFailed = 1
8747+
/* We couldn't read the DMV so set the @IFISetting variable using the error log */
8748+
BEGIN
8749+
IF EXISTS ( SELECT 1/0
8750+
FROM #ErrorLog
8751+
WHERE LEFT([Text], 45) = N'Database Instant File Initialization: enabled'
8752+
)
8753+
BEGIN
8754+
SET @IFISetting = 'Y';
8755+
END
8756+
ELSE
8757+
BEGIN
8758+
SET @IFISetting = 'N';
8759+
END;
8760+
END;
8761+
8762+
IF NOT EXISTS ( SELECT 1/0
8763+
FROM #SkipChecks
8764+
WHERE DatabaseName IS NULL AND CheckID = 192 /* IFI disabled check disabled */
8765+
) AND @IFISetting = 'N'
8766+
BEGIN
8767+
INSERT INTO #BlitzResults
8768+
(
8769+
CheckID ,
8770+
[Priority] ,
8771+
FindingsGroup ,
8772+
Finding ,
8773+
URL ,
8774+
Details
8775+
)
8776+
SELECT
8777+
192 AS [CheckID] ,
8778+
50 AS [Priority] ,
8779+
'Performance' AS [FindingsGroup] ,
8780+
'Instant File Initialization Not Enabled' AS [Finding] ,
8781+
'https://www.brentozar.com/go/instant' AS [URL] ,
8782+
'Consider enabling IFI for faster restores and data file growths.' AS [Details]
8783+
END;
8784+
8785+
IF NOT EXISTS ( SELECT 1/0
8786+
FROM #SkipChecks
8787+
WHERE DatabaseName IS NULL AND CheckID = 193 /* IFI enabled check disabled */
8788+
) AND @IFISetting = 'Y'
8789+
BEGIN
8790+
INSERT INTO #BlitzResults
8791+
(
8792+
CheckID ,
8793+
[Priority] ,
8794+
FindingsGroup ,
8795+
Finding ,
8796+
URL ,
8797+
Details
8798+
)
8799+
SELECT
8800+
193 AS [CheckID] ,
8801+
250 AS [Priority] ,
8802+
'Server Info' AS [FindingsGroup] ,
8803+
'Instant File Initialization Enabled' AS [Finding] ,
8804+
'https://www.brentozar.com/go/instant' AS [URL] ,
8805+
'The service account has the Perform Volume Maintenance Tasks permission.' AS [Details]
8806+
END;
8807+
END;
8808+
END;
87658809

8766-
/* Server Info - Instant File Initialization Not Enabled - Check 192 - SQL Server 2016 SP1 and newer */
8767-
IF NOT EXISTS ( SELECT 1
8768-
FROM #SkipChecks
8769-
WHERE DatabaseName IS NULL AND CheckID = 192 )
8770-
AND EXISTS ( SELECT *
8771-
FROM sys.all_objects o
8772-
INNER JOIN sys.all_columns c ON o.object_id = c.object_id
8773-
WHERE o.name = 'dm_server_services'
8774-
AND c.name = 'instant_file_initialization_enabled' )
8775-
BEGIN
8776-
8777-
IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 192) WITH NOWAIT;
8778-
8779-
SET @StringToExecute = 'INSERT INTO #BlitzResults (CheckID, Priority, FindingsGroup, Finding, URL, Details)
8780-
SELECT 192 AS CheckID ,
8781-
50 AS Priority ,
8782-
''Server Info'' AS FindingsGroup ,
8783-
''Instant File Initialization Not Enabled'' AS Finding ,
8784-
''https://www.brentozar.com/go/instant'' AS URL ,
8785-
''Consider enabling IFI for faster restores and data file growths.''
8786-
FROM sys.dm_server_services WHERE instant_file_initialization_enabled <> ''Y'' AND filename LIKE ''%sqlservr.exe%'' OPTION (RECOMPILE);';
8787-
8788-
IF @Debug = 2 AND @StringToExecute IS NOT NULL PRINT @StringToExecute;
8789-
IF @Debug = 2 AND @StringToExecute IS NULL PRINT '@StringToExecute has gone NULL, for some reason.';
8790-
8791-
EXECUTE(@StringToExecute);
8792-
END;
8810+
/* End of checkId 192 */
8811+
/* End of checkId 193 */
87938812

87948813
IF NOT EXISTS ( SELECT 1
87958814
FROM #SkipChecks

0 commit comments

Comments
 (0)