Skip to content

Commit 24401bb

Browse files
SQL Agent Failed Jobs
1 parent 4a0d92b commit 24401bb

File tree

3 files changed

+70
-83
lines changed

3 files changed

+70
-83
lines changed

source/checks/Agentv5.Tests.ps1

Lines changed: 25 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ BeforeDiscovery {
3434

3535
#TODO : Clean this up
3636
Write-PSFMessage -Message "Instances = $($InstancesToTest.Name)" -Level Verbose
37-
37+
3838
Set-PSFConfig -Module dbachecks -Name global.notcontactable -Value $NotContactable
3939

4040
# Get-DbcConfig is expensive so we call it once
@@ -159,76 +159,37 @@ Describe "Long Running Agent Jobs" -Tag LongRunningJob, Agent -ForEach $Instance
159159
}
160160
}
161161

162-
# Describe "Long Running Agent Jobs" -Tags LongRunningJob, $filename {
163-
164-
# }
165-
# if ($NotContactable -contains $psitem) {
166-
# Context "Testing long running jobs on $psitem" {
167-
# It "Can't Connect to $Psitem" {
168-
# $false | Should -BeTrue -Because "The instance should be available to be connected to!"
169-
# }
170-
# }
171-
# }
172-
# else {
173-
# Context "Testing long running jobs on $psitem" {
174-
# if ($runningjobs) {
175-
# foreach ($runningjob in $runningjobs | Where-Object { $_.AvgSec -ne 0 }) {
176-
# It "Running job $($runningjob.JobName) duration should not be more than $runningjobpercentage % extra of the average run time on $psitem" -Skip:$skip {
177-
# Assert-LongRunningJobs -runningjob $runningjob -runningjobpercentage $runningjobpercentage
178-
# }
179-
# }
180-
# }
181-
# else {
182-
# It "There are no running jobs currently on $psitem" -Skip:$skip {
183-
# $True | SHould -BeTrue
184-
# }
185-
# }
186-
# }
187-
# }
188-
# }
189-
190-
191-
192162

163+
Describe "SQL Agent Failed Jobs" -Tag FailedJob, Agent -ForEach $InstancesToTest {
164+
$skipAgentFailedJobs = ($__dbcconfig | Where-Object { $_.Name -eq 'skip.agent.failedjobs' }).Value
165+
$excludecancelled = ($__dbcconfig | Where-Object { $_.Name -eq 'agent.failedjob.excludecancelled' }).Value
193166

167+
Context "Checking for failed enabled jobs since $startdate on <_.Name>" {
168+
($PSItem.JobsFailed).ForEach{
169+
Write-PSFMessage -Message "LastRunOutcome = $($PSItem)" -Level Verbose
170+
if ($PSItem.LastRunOutcome -eq "Unknown") {
171+
It "We chose to skip this as $($PSItem.JobName)'s last run outcome is unknown on $($PSItem.InstanceName)" -Skip {
172+
$PSItem.LastRunOutcome | Should -Be $PSItem.ExpectedOutcome -Because 'All Agent Jobs should have succeed this one is unknown - you need to investigate the failed jobs'
173+
}
174+
}
175+
elseif (($PSItem.LastRunOutcome -eq "Cancelled") -and ($excludecancelled -eq $true)) {
176+
It "You chose to skip this as $($PSItem.JobName)'s last run outcome is cancelled on $($PSItem.InstanceName)" -Skip {
177+
$PSItem.LastRunOutcome | Should -Be $PSItem.ExpectedOutcome -Because 'All Agent Jobs should have succeed this one is Cancelled - you need to investigate the failed jobs'
178+
}
179+
}
180+
else {
181+
It "Job $($PSItem.JobName) last run outcome is $($PSItem.LastRunOutcome) on $($PSItem.InstanceName)" -Skip:$skipAgentFailedJobs {
182+
$PSItem.LastRunOutcome | Should -Be $PSItem.ExpectedOutcome -Because "All Agent Jobs should have succeed - you need to investigate the failed jobs"
183+
}
184+
}
185+
}
186+
}
187+
}
194188

195189

196190

197191

198-
# Describe "Failed Jobs" -Tags FailedJob, $filename {
199192

200-
# if ($NotContactable -contains $psitem) {
201-
# Context "Checking for failed enabled jobs on $psitem" {
202-
# It "Can't Connect to $Psitem" {
203-
# $false | Should -BeTrue -Because "The instance should be available to be connected to!"
204-
# }
205-
# }
206-
# }
207-
# else {
208-
# $maxdays = Get-DbcConfigValue agent.failedjob.since
209-
# $startdate = (Get-Date).AddDays( - $maxdays)
210-
# Context "Checking for failed enabled jobs since $startdate on $psitem" {
211-
# $excludecancelled = Get-DbcConfigValue agent.failedjob.excludecancelled
212-
# @(Get-DbaAgentJob -SqlInstance $psitem | Where-Object { $Psitem.IsEnabled -and ($psitem.LastRunDate -gt $startdate) }).ForEach{
213-
# if ($psitem.LastRunOutcome -eq "Unknown") {
214-
# It -Skip "We chose to skip this as $psitem's last run outcome is unknown on $($psitem.SqlInstance)" {
215-
# $psitem.LastRunOutcome | Should -Be "Succeeded" -Because 'All Agent Jobs should have succeed this one is unknown - you need to investigate the failed jobs'
216-
# }
217-
# }
218-
# elseif (($psitem.LastRunOutcome -eq "Cancelled") -and ($excludecancelled -eq $true)) {
219-
# It -Skip "We chose to skip this as $psitem's last run outcome is cancelled on $($psitem.SqlInstance)" {
220-
# $psitem.LastRunOutcome | Should -Be "Succeeded" -Because 'All Agent Jobs should have succeed this one is unknown - you need to investigate the failed jobs'
221-
# }
222-
# }
223-
# else {
224-
# It "$psitem's last run outcome is $($psitem.LastRunOutcome) on $($psitem.SqlInstance)" {
225-
# $psitem.LastRunOutcome | Should -Be "Succeeded" -Because 'All Agent Jobs should have succeed - you need to investigate the failed jobs'
226-
# }
227-
# }
228-
# }
229-
# }
230-
# }
231-
# }
232193

233194

234195
# Describe "Agent Alerts" -Tags AgentAlert, $filename {

source/internal/configurations/configuration.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,7 @@ Set-PSFConfig -Module dbachecks -Name skip.agent.longrunningjobs -Validation boo
347347
Set-PSFConfig -Module dbachecks -Name skip.agent.lastjobruntime -Validation bool -Value $false -Initialize -Description "Skip the last agent job time check"
348348
Set-PSFConfig -Module dbachecks -Name skip.agent.jobowner -Validation bool -Value $false -Initialize -Description "Skip the Agent Job Owner check"
349349
Set-PSFConfig -Module dbachecks -Name skip.agent.invalidjobowner.name -Validation bool -Value $false -Initialize -Description "Skip the Agent Job Invalid Owner check"
350+
Set-PSFConfig -Module dbachecks -Name skip.agent.failedjobs -Validation bool -Value $false -Initialize -Description "Skip the Agent Failed Jobs check"
350351

351352

352353

source/internal/functions/Get-AllAgentInfo.ps1

Lines changed: 44 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -41,28 +41,32 @@ function Get-AllAgentInfo {
4141
# Invalid JobOwner Initial fields
4242
$InvalidJobOwnerInitFields = $Instance.GetDefaultInitFields([Microsoft.SqlServer.Management.Smo.Agent.Job])
4343

44-
# Database Initial Fields
45-
$DatabaseInitFields = $Instance.GetDefaultInitFields([Microsoft.SqlServer.Management.Smo.Database])
44+
# Failed Job Initial fields
45+
$FailedJobInitFields = $Instance.GetDefaultInitFields([Microsoft.SqlServer.Management.Smo.Agent.Job])
4646

47-
# Stored Procedure Initial Fields
48-
$StoredProcedureInitFields = $Instance.GetDefaultInitFields([Microsoft.SqlServer.Management.Smo.StoredProcedure])
49-
50-
# Information Initial Fields
51-
52-
# Settings Initial Fields
53-
$SettingsInitFields = $Instance.GetDefaultInitFields([Microsoft.SqlServer.Management.Smo.Settings])
54-
55-
# Login Initial Fields
56-
$LoginInitFields = $Instance.GetDefaultInitFields([Microsoft.SqlServer.Management.Smo.Login])
57-
58-
# Log File Initial Fields
59-
$LogFileInitFields = $Instance.GetDefaultInitFields([Microsoft.SqlServer.Management.Smo.LogFile])
60-
61-
# Data File Initial Fields
62-
$DataFileInitFields = $Instance.GetDefaultInitFields([Microsoft.SqlServer.Management.Smo.DataFile])
47+
#TODO: Clean up?
48+
## Database Initial Fields
49+
#$DatabaseInitFields = $Instance.GetDefaultInitFields([Microsoft.SqlServer.Management.Smo.Database])
50+
#
51+
## Stored Procedure Initial Fields
52+
#$StoredProcedureInitFields = $Instance.GetDefaultInitFields([Microsoft.SqlServer.Management.Smo.StoredProcedure])
53+
#
54+
## Information Initial Fields
55+
#
56+
## Settings Initial Fields
57+
#$SettingsInitFields = $Instance.GetDefaultInitFields([Microsoft.SqlServer.Management.Smo.Settings])
58+
#
59+
## Login Initial Fields
60+
#$LoginInitFields = $Instance.GetDefaultInitFields([Microsoft.SqlServer.Management.Smo.Login])
61+
#
62+
## Log File Initial Fields
63+
#$LogFileInitFields = $Instance.GetDefaultInitFields([Microsoft.SqlServer.Management.Smo.LogFile])
64+
#
65+
## Data File Initial Fields
66+
#$DataFileInitFields = $Instance.GetDefaultInitFields([Microsoft.SqlServer.Management.Smo.DataFile])
6367

6468
# Configuration cannot have default init fields :-)
65-
$configurations = $false
69+
#$configurations = $false
6670

6771
# Set up blank ConfigValues object for any config we need to use in the checks
6872
$ConfigValues = [PSCustomObject]@{}
@@ -189,7 +193,27 @@ function Get-AllAgentInfo {
189193
#Write-PSFMessage -Message "ActualDatabaseMailProfile instance : $($Instance.JobServer.DatabaseMailProfile)" -Level Verbose
190194
}
191195
'FailedJob' {
196+
$FailedJobInitFields.Add("Name") | Out-Null # so we can check Job Name
197+
$FailedJobInitFields.Add("IsEnabled") | Out-Null # so we can check Job status
198+
$FailedJobInitFields.Add("LastRunDate") | Out-Null # so we can check Job LastRunDate
199+
$FailedJobInitFields.Add("LastRunOutcome") | Out-Null # so we can check Job LastRunOutcome
200+
201+
$Instance.SetDefaultInitFields([Microsoft.SqlServer.Management.Smo.Agent.Job], $FailedJobInitFields)
202+
$FailedJobInitFields = $Instance.GetDefaultInitFields([Microsoft.SqlServer.Management.Smo.Agent.Job])
203+
204+
$maxdays = Get-DbcConfigValue agent.failedjob.since
205+
$startdate = (Get-Date).AddDays( - $maxdays)
192206

207+
$ConfigValues | Add-Member -MemberType NoteProperty -Name 'FailedJob' -Value 'Succeeded'
208+
209+
$JobsFailed = ($Instance.JobServer.Jobs | Where-Object { $_.IsEnabled -and ($_.LastRunDate -gt $startdate) }).ForEach{
210+
[PSCustomObject]@{
211+
InstanceName = $Instance.Name
212+
JobName = $PSItem.Name
213+
ExpectedOutcome = $ConfigValues.FailedJob
214+
LastRunOutcome = $PSItem.LastRunOutcome
215+
}
216+
}
193217
}
194218
'ValidJobOwner' {
195219
$JobOwnerInitFields.Add("OwnerLoginName") | Out-Null # so we can check Job Owner
@@ -359,6 +383,7 @@ function Get-AllAgentInfo {
359383
AgentMailProfile = @($agentMailProfile)
360384
JobOwner = $JobOwner
361385
InvalidJobOwner = $InvalidJobOwner
386+
JobsFailed = $JobsFailed
362387
LastJobRuns = $LastJobRuns
363388
LongRunningJobs = $LongRunningJobs
364389
}

0 commit comments

Comments
 (0)