Skip to content

Commit 351fa70

Browse files
Last Agent Job Run
1 parent 35c4152 commit 351fa70

File tree

2 files changed

+81
-77
lines changed

2 files changed

+81
-77
lines changed

source/checks/Agentv5.Tests.ps1

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

3535
#TODO : Clean this up
3636
Write-PSFMessage -Message "Instances = $($InstancesToTest.Name)" -Level Verbose
37-
38-
Write-PSFMessage -Message "JobOwner = $($InstancesToTest.JobOwner)" -Level Verbose
39-
Write-PSFMessage -Message "JobOwner = $($InstancesToTest.JobOwner.JobName)" -Level Verbose
40-
37+
4138
Set-PSFConfig -Module dbachecks -Name global.notcontactable -Value $NotContactable
4239

4340
# Get-DbcConfig is expensive so we call it once
@@ -134,14 +131,30 @@ Describe "Invalid Job Owner" -Tag InvalidJobOwner, Agent -ForEach $InstancesToTe
134131
$skipAgentJobTargetInvalidOwner = ($__dbcconfig | Where-Object { $_.Name -eq 'skip.agent.invalidjobowner.name' }).Value
135132

136133
Context "Testing Invalid SQL Agent Job Owner on <_.Name>" {
137-
It "The Job <_.JobName> has the Job Owner <_.ActualJobOwnerName> that shouldn't exist in this list ($([String]::Join(', ', "<_.InvalidJobOwnerName>"))) on <_.InstanceName>" -Skip:$skipAgentJobTargetOwner -ForEach ($PSItem.InvalidJobOwner) {
134+
It "The Job <_.JobName> has the Job Owner <_.ActualJobOwnerName> that shouldn't exist in this list ($([String]::Join(', ', "<_.InvalidJobOwnerName>"))) on <_.InstanceName>" -Skip:$skipAgentJobTargetInvalidOwner -ForEach ($PSItem.InvalidJobOwner) {
138135
$PSItem.ActualJobOwnerName | Should -Not -BeIn $PSItem.InvalidJobOwnerName -Because 'The account that is the job owner has been defined as not valid'
139136
}
140137
}
141138
}
142139

143140

144141

142+
Describe "Last Agent Job Run" -Tag LastJobRunTime, Agent -ForEach $InstancesToTest {
143+
$skipAgentJobLastRun = ($__dbcconfig | Where-Object { $_.Name -eq 'skip.agent.lastjobruntime' }).Value
144+
145+
Context "Testing last job run time on <_.Name>" {
146+
It "Job <_.JobName> last run duration (<_.Duration> seconds) should not be greater than <_.ExpectedRunningJobPercentage>% extra of the average run time (<_.Average> seconds) on <_.InstanceName>" -Skip:$skipAgentJobLastRun -ForEach ($PSItem.LastJobRuns) {
147+
$PSItem.ActualRunningJobPercentage | Should -BeLessThan $PSItem.ExpectedRunningJobPercentage -Because "The last run of job $($PSItem.JobName) was $($PSItem.Duration) seconds. This is more than the $($PSItem.ExpectedRunningJobPercentage)% specified as the maximum variance"
148+
}
149+
}
150+
}
151+
152+
153+
154+
155+
156+
157+
145158

146159

147160

@@ -337,77 +350,7 @@ Describe "Invalid Job Owner" -Tag InvalidJobOwner, Agent -ForEach $InstancesToTe
337350
# }
338351
# }
339352
# }
340-
# Describe "Last Agent Job Run" -Tags LastJobRunTime, $filename {
341-
# $skip = Get-DbcConfigValue skip.agent.lastjobruntime
342-
# $runningjobpercentage = Get-DbcConfigValue agent.lastjobruntime.percentage
343-
# $maxdays = Get-DbcConfigValue agent.failedjob.since
344-
# if (-not $skip) {
345-
# $query = "IF OBJECT_ID('tempdb..#dbachecksLastRunTime') IS NOT NULL DROP Table #dbachecksLastRunTime
346-
# SELECT * INTO #dbachecksLastRunTime
347-
# FROM
348-
# (
349-
# SELECT
350-
# j.job_id,
351-
# j.name AS JobName,
352-
# DATEDIFF(SECOND, 0, STUFF(STUFF(RIGHT('000000' + CONVERT(VARCHAR(6),jh.run_duration),6),5,0,':'),3,0,':')) AS Duration
353-
# FROM msdb.dbo.sysjobs j
354-
# INNER JOIN
355-
# (
356-
# SELECT job_id, instance_id = MAX(instance_id)
357-
# FROM msdb.dbo.sysjobhistory
358-
# GROUP BY job_id
359-
# ) AS h
360-
# ON j.job_id = h.job_id
361-
# INNER JOIN
362-
# msdb.dbo.sysjobhistory AS jh
363-
# ON jh.job_id = h.job_id
364-
# AND jh.instance_id = h.instance_id
365-
# WHERE msdb.dbo.agent_datetime(jh.run_date, jh.run_time) > DATEADD(DAY,- $maxdays,GETDATE())
366-
# AND jh.step_id = 0
367-
# ) AS lrt
368-
369-
# IF OBJECT_ID('tempdb..#dbachecksAverageRunTime') IS NOT NULL DROP Table #dbachecksAverageRunTime
370-
# SELECT * INTO #dbachecksAverageRunTime
371-
# FROM
372-
# (
373-
# SELECT
374-
# job_id,
375-
# AVG(DATEDIFF(SECOND, 0, STUFF(STUFF(RIGHT('000000' + CONVERT(VARCHAR(6),run_duration),6),5,0,':'),3,0,':'))) AS AvgSec
376-
# FROM msdb.dbo.sysjobhistory hist
377-
# WHERE msdb.dbo.agent_datetime(run_date, run_time) > DATEADD(DAY,- $maxdays,GETDATE())
378-
# AND Step_id = 0
379-
# AND run_duration >= 0
380-
# GROUP BY job_id
381-
# ) as art
382-
383-
# SELECT
384-
# JobName,
385-
# Duration,
386-
# AvgSec,
387-
# Duration - AvgSec AS Diff
388-
# FROM #dbachecksLastRunTime lastrun
389-
# JOIN #dbachecksAverageRunTime avgrun
390-
# ON lastrun.job_id = avgrun.job_id
391-
392-
# DROP Table #dbachecksLastRunTime
393-
# DROP Table #dbachecksAverageRunTime"
394-
# $lastagentjobruns = Invoke-DbaQuery -SqlInstance $PSItem -Database msdb -Query $query
395-
# Context "Testing last job run time on $psitem" {
396-
# foreach ($lastagentjobrun in $lastagentjobruns | Where-Object { $_.AvgSec -ne 0 }) {
397-
# It "Job $($lastagentjobrun.JobName) last run duration should be not be greater than $runningjobpercentage % extra of the average run time on $psitem" -Skip:$skip {
398-
# Assert-LastJobRun -lastagentjobrun $lastagentjobrun -runningjobpercentage $runningjobpercentage
399-
# }
400-
# }
401-
# }
402-
# }
403-
# else {
404-
# Context "Testing last job run time on $psitem" {
405-
# It "Job average run time on $psitem" -Skip {
406-
# Assert-LastJobRun -lastagentjobrun $lastagentjobrun -runningjobpercentage $runningjobpercentage
407-
# }
408-
# }
409-
# }
410-
# }
353+
411354

412355

413356

source/internal/functions/Get-AllAgentInfo.ps1

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,67 @@ function Get-AllAgentInfo {
236236

237237
}
238238
'LastJobRunTime' {
239-
239+
$maxdays = Get-DbcConfigValue agent.failedjob.since
240+
$query = "IF OBJECT_ID('tempdb..#dbachecksLastRunTime') IS NOT NULL DROP Table #dbachecksLastRunTime
241+
SELECT * INTO #dbachecksLastRunTime
242+
FROM
243+
(
244+
SELECT
245+
j.job_id,
246+
j.name AS JobName,
247+
DATEDIFF(SECOND, 0, STUFF(STUFF(RIGHT('000000' + CONVERT(VARCHAR(6),jh.run_duration),6),5,0,':'),3,0,':')) AS Duration
248+
FROM msdb.dbo.sysjobs j
249+
INNER JOIN
250+
(
251+
SELECT job_id, instance_id = MAX(instance_id)
252+
FROM msdb.dbo.sysjobhistory
253+
GROUP BY job_id
254+
) AS h
255+
ON j.job_id = h.job_id
256+
INNER JOIN
257+
msdb.dbo.sysjobhistory AS jh
258+
ON jh.job_id = h.job_id
259+
AND jh.instance_id = h.instance_id
260+
WHERE msdb.dbo.agent_datetime(jh.run_date, jh.run_time) > DATEADD(DAY,- $maxdays,GETDATE())
261+
AND jh.step_id = 0
262+
) AS lrt
263+
IF OBJECT_ID('tempdb..#dbachecksAverageRunTime') IS NOT NULL DROP Table #dbachecksAverageRunTime
264+
SELECT * INTO #dbachecksAverageRunTime
265+
FROM
266+
(
267+
SELECT
268+
job_id,
269+
AVG(DATEDIFF(SECOND, 0, STUFF(STUFF(RIGHT('000000' + CONVERT(VARCHAR(6),run_duration),6),5,0,':'),3,0,':'))) AS AvgSec
270+
FROM msdb.dbo.sysjobhistory hist
271+
WHERE msdb.dbo.agent_datetime(run_date, run_time) > DATEADD(DAY,- $maxdays,GETDATE())
272+
AND Step_id = 0
273+
AND run_duration >= 0
274+
GROUP BY job_id
275+
) as art
276+
SELECT
277+
JobName,
278+
Duration,
279+
AvgSec,
280+
Duration - AvgSec AS Diff
281+
FROM #dbachecksLastRunTime lastrun
282+
JOIN #dbachecksAverageRunTime avgrun
283+
ON lastrun.job_id = avgrun.job_id
284+
DROP Table #dbachecksLastRunTime
285+
DROP Table #dbachecksAverageRunTime"
286+
$lastagentjobruns = Invoke-DbaQuery -SqlInstance $Instance -Database msdb -Query $query
287+
288+
$ConfigValues | Add-Member -MemberType NoteProperty -Name 'LastJobRuns' -Value (Get-DbcConfigValue agent.lastjobruntime.percentage)
289+
290+
$LastJobRuns = $($lastagentjobruns | Where-Object { $_.AvgSec -ne 0 }).ForEach{
291+
[PSCustomObject]@{
292+
InstanceName = $Instance.Name
293+
JobName = $PSItem.JobName
294+
Duration = $PSItem.Duration
295+
Average = $PSItem.AvgSec
296+
ExpectedRunningJobPercentage = $ConfigValues.LastJobRuns
297+
ActualRunningJobPercentage = [math]::Round($PSItem.Diff / $PSItem.AvgSec * 100)
298+
}
299+
}
240300
}
241301
Default { }
242302
}
@@ -257,6 +317,7 @@ function Get-AllAgentInfo {
257317
AgentMailProfile = @($agentMailProfile)
258318
JobOwner = $JobOwner
259319
InvalidJobOwner = $InvalidJobOwner
320+
LastJobRuns = $LastJobRuns
260321
}
261322
return $testInstanceObject
262323
}

0 commit comments

Comments
 (0)