Skip to content

Commit

Permalink
ModelDbGrowth check added and tested #882
Browse files Browse the repository at this point in the history
  • Loading branch information
SQLDBAWithABeard committed Apr 27, 2022
1 parent ba748b4 commit c62535f
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 11 deletions.
16 changes: 10 additions & 6 deletions Perf Testing pesterv5.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ ipmo ./dbachecks.psd1

#

$Checks = 'DefaultBackupCompression','SaExist','SaDisabled','SaRenamed','DefaultFilePath','AdHocDistributedQueriesEnabled','AdHocWorkload', 'DefaultTrace', 'OleAutomationProceduresDisabled', 'CrossDBOwnershipChaining', 'ScanForStartupProceduresDisabled', 'RemoteAccessDisabled', 'SQLMailXPsDisabled', 'DAC', 'OLEAutomation'
$Checks = 'DefaultBackupCompression'
$Checks = 'ModelDbGrowth','DefaultBackupCompression','SaExist','SaDisabled','SaRenamed','DefaultFilePath','AdHocDistributedQueriesEnabled','AdHocWorkload', 'DefaultTrace', 'OleAutomationProceduresDisabled', 'CrossDBOwnershipChaining', 'ScanForStartupProceduresDisabled', 'RemoteAccessDisabled', 'SQLMailXPsDisabled', 'DAC', 'OLEAutomation'
$Checks = 'ModelDbGrowth'
Compare-CheckRuns -Checks $checks

<#
Expand Down Expand Up @@ -60,16 +60,20 @@ $originalCodeMessage = "With original Code it takes {0} MilliSeconds" -f $origin


$savingMessage = "
Running with {3} Checks against 3 SQL Containers
Running with
{3}
Checks against 3 SQL Containers
With original Code it takes {1} Seconds
With New Code it takes {4} Seconds
New Code for these
checks is saving {0} seconds
New Code for these {5} checks
is saving {0} seconds
from a run of {1} seconds
New Code runs in {2} % of the time
" -f ('{0:N2}' -f ($originalCodetrace.StopwatchDuration.TotalSeconds - $NewCodetrace.StopwatchDuration.TotalSeconds)),('{0:N2}' -f $originalCodetrace.StopwatchDuration.TotalSeconds),('{0:N2}' -f (($NewCodetrace.StopwatchDuration.TotalSeconds/$originalCodetrace.StopwatchDuration.TotalSeconds) * 100)),($Checks -split ',' -join ',') ,('{0:N2}' -f $NewCodetrace.StopwatchDuration.TotalSeconds)
" -f ('{0:N2}' -f ($originalCodetrace.StopwatchDuration.TotalSeconds - $NewCodetrace.StopwatchDuration.TotalSeconds)),('{0:N2}' -f $originalCodetrace.StopwatchDuration.TotalSeconds),('{0:N2}' -f (($NewCodetrace.StopwatchDuration.TotalSeconds/$originalCodetrace.StopwatchDuration.TotalSeconds) * 100)),($Checks -split ',' -join ',') ,('{0:N2}' -f $NewCodetrace.StopwatchDuration.TotalSeconds), $Checks.Count
cls

Write-PSFMessage -Message $savingMessage -Level Output
Expand Down
4 changes: 2 additions & 2 deletions Validate v4 adn v5.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ ipmo ./dbachecks.psd1

#

$Checks = 'DefaultBackupCompression','SaExist','SaDisabled','SaRenamed','DefaultFilePath','AdHocDistributedQueriesEnabled','AdHocWorkload', 'DefaultTrace', 'OleAutomationProceduresDisabled', 'CrossDBOwnershipChaining', 'ScanForStartupProceduresDisabled', 'RemoteAccessDisabled', 'SQLMailXPsDisabled', 'DAC', 'OLEAutomation'
$Checks = 'DefaultBackupCompression'
$Checks = 'ModelDbGrowth','DefaultBackupCompression','SaExist','SaDisabled','SaRenamed','DefaultFilePath','AdHocDistributedQueriesEnabled','AdHocWorkload', 'DefaultTrace', 'OleAutomationProceduresDisabled', 'CrossDBOwnershipChaining', 'ScanForStartupProceduresDisabled', 'RemoteAccessDisabled', 'SQLMailXPsDisabled', 'DAC', 'OLEAutomation'
$Checks = 'ModelDbGrowth'
Compare-v4andv5Results -Checks $Checks

<#
Expand Down
23 changes: 22 additions & 1 deletion checks/Instancev5.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ Describe "SA Login Disabled" -Tag SaDisabled, DISA, CIS, Medium, Instance -ForEa

Describe "Login SA cannot exist" -Tag SaExist, CIS, Medium, Instance -ForEach $InstancesToTest {
$skip = Get-DbcConfigValue skip.security.saexist
Context "Checking that a login named sa does not exist on <_.Name>" {
Context "Checking that a login named sa does not exist on <_.Name>" {
It "sa login does not exist on <_.Name>" -Skip:$Skip {
$PsItem.Logins['sa'].Count | Should -Be 0 -Because "We expected no login to exist with the name sa"
}
Expand All @@ -167,3 +167,24 @@ Describe "Default Backup Compression" -Tag DefaultBackupCompression, Low, Instan
}
}
}

Describe "Model Database Growth" -Tag ModelDbGrowth, Low, Instance -ForEach $InstancesToTest {
$skip = Get-DbcConfigValue skip.instance.modeldbgrowth
Context "Testing model database growth setting is not default on <_.Name>" {
It "Growth settings should not be percent for file <_.Name> on <_.Parent.Parent.Parent.Name>" -Skip:$skip -ForEach $PsItem.Databases['model'].FileGroups.Files {
$psitem.GrowthType | Should -Not -Be 'Percent' -Because 'New databases use the model database as a template and percent growth can cause performance problems'
}
It "Growth settings should not be 1Mb for file <_.Name> on <_.Parent.Parent.Parent.Name>" -Skip:$skip -ForEach $PsItem.Databases['model'].FileGroups.Files {
$psitem.Growth | Should -Not -Be 1024 -Because 'New databases use the model database as a template and growing for each Mb will have a performance impact'
}
It "Growth settings should not be percent for file <_.Name> on <_.Parent.Parent.Name>" -Skip:$skip -ForEach @($PsItem.Databases['model'].LogFiles) {
$psitem.GrowthType | Should -Not -Be 'Percent' -Because 'New databases use the model database as a template and percent growth can cause performance problems'
}
It "Growth settings should not be 1Mb for file <_.Name> on <_.Parent.Parent.Name>" -Skip:$skip -ForEach @($PsItem.Databases['model'].LogFiles) {
$psitem.Growth | Should -Not -Be 1024 -Because 'New databases use the model database as a template and growing for each Mb will have a performance impact'
}
}
}



26 changes: 24 additions & 2 deletions internal/functions/NewGet-AllInstanceInfo.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,20 @@ function NewGet-AllInstanceInfo {
$Instance.SetDefaultInitFields([Microsoft.SqlServer.Management.Smo.StoredProcedure], $false)
$Instance.SetDefaultInitFields([Microsoft.SqlServer.Management.Smo.Information], $false)
$Instance.SetDefaultInitFields([Microsoft.SqlServer.Management.Smo.Settings], $false)
$Instance.SetDefaultInitFields([Microsoft.SqlServer.Management.Smo.LogFile], $false)
$Instance.SetDefaultInitFields([Microsoft.SqlServer.Management.Smo.DataFile], $false)



# Server Initial fields
$ServerInitFields = $Instance.GetDefaultInitFields([Microsoft.SqlServer.Management.Smo.Server])
$ServerInitFields.Add("VersionMajor") | Out-Null # so we can check versions
$Instance.SetDefaultInitFields([Microsoft.SqlServer.Management.Smo.Server], $ServerInitFields)

# Database Initial Fields
$DatabaseInitFields = $Instance.GetDefaultInitFields([Microsoft.SqlServer.Management.Smo.Database])

# Stored Procedure Initial Fields
$StoredProcedureInitFields = $Instance.GetDefaultInitFields([Microsoft.SqlServer.Management.Smo.StoredProcedure])
$StoredProcedureInitFields.Add("Startup") | Out-Null # So we can check SPs start up for the CIS checks

# Information Initial Fields

Expand All @@ -29,6 +33,12 @@ function NewGet-AllInstanceInfo {
# Login Initial Fields
$LoginInitFields = $Instance.GetDefaultInitFields([Microsoft.SqlServer.Management.Smo.Login])

# Log File Initial Fields
$LogFileInitFields = $Instance.GetDefaultInitFields([Microsoft.SqlServer.Management.Smo.LogFile])

# Data File Initial Fields
$DataFileInitFields = $Instance.GetDefaultInitFields([Microsoft.SqlServer.Management.Smo.DataFile])

# Configuration cannot have default init fields :-)
$configurations = $false

Expand All @@ -51,6 +61,7 @@ function NewGet-AllInstanceInfo {
# we have to check the spconfigure and we have to check that any stored procedurees in master have startup set to true
$configurations = $true
$ScanForStartupProceduresDisabled = $true
$StoredProcedureInitFields.Add("Startup") | Out-Null # So we can check SPs start up for the CIS checks
$Instance.SetDefaultInitFields([Microsoft.SqlServer.Management.Smo.StoredProcedure], $StoredProcedureInitFields)
$ConfigValues | Add-Member -MemberType NoteProperty -Name 'scanforstartupproceduresdisabled' -Value (Get-DbcConfigValue policy.security.scanforstartupproceduresdisabled)
}
Expand Down Expand Up @@ -92,6 +103,16 @@ function NewGet-AllInstanceInfo {
$LoginInitFields.Add("ID") | Out-Null # so we can check if sa is disabled even if it has been renamed
$Instance.SetDefaultInitFields([Microsoft.SqlServer.Management.Smo.Settings], $LoginInitFields)
}
'ModelDbGrowth' {
$LogFileInitFields.Add("Growth") | Out-Null # So we can check the model file growth settings
$LogFileInitFields.Add("GrowthType") | Out-Null # So we can check the model file growth settings
$LogFileInitFields.Add("Name") | Out-Null # So we can check the model file growth settings
$DataFileInitFields.Add("Growth") | Out-Null # So we can check the model file growth settings
$DataFileInitFields.Add("GrowthType") | Out-Null # So we can check the model file growth settings
$DataFileInitFields.Add("Name") | Out-Null # So we can check the model file growth settings
$Instance.SetDefaultInitFields([Microsoft.SqlServer.Management.Smo.LogFile], $LogFileInitFields)
$Instance.SetDefaultInitFields([Microsoft.SqlServer.Management.Smo.DataFile], $DataFileInitFields)
}

Default { }
}
Expand All @@ -109,6 +130,7 @@ function NewGet-AllInstanceInfo {
Configuration = if ($configurations) { $Instance.Configuration } else { $null }
Settings = $Instance.Settings
Logins = $Instance.Logins
Databases = $Instance.Databases
}
if ($ScanForStartupProceduresDisabled) {
$StartUpSPs = $Instance.Databases['master'].StoredProcedures.Where{ $_. Name -ne 'sp_MSrepl_startup' -and $_.StartUp -eq $true }.count
Expand Down

0 comments on commit c62535f

Please sign in to comment.