-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiskQueueMonitor.ps1
More file actions
79 lines (68 loc) · 2.72 KB
/
Copy pathDiskQueueMonitor.ps1
File metadata and controls
79 lines (68 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
param(
[int]$DurationSeconds = 60,
[int]$IntervalSeconds = 5
)
function Get-DiskQueueAverages {
param(
[int]$DurationSeconds,
[int]$IntervalSeconds
)
$sampleCount = [math]::Max(1, [int]($DurationSeconds / $IntervalSeconds))
$counterCandidates = @('\PhysicalDisk(*)\Avg. Disk Queue Length', '\LogicalDisk(*)\Avg. Disk Queue Length')
$resultSets = $null
$usedCounter = $null
foreach ($path in $counterCandidates) {
try {
$resultSets = Get-Counter -Counter $path -SampleInterval $IntervalSeconds -MaxSamples $sampleCount -ErrorAction Stop
$usedCounter = $path
break
} catch {
$resultSets = $null
}
}
if (-not $resultSets) {
throw "Failed to read disk queue length counters. Tried: $($counterCandidates -join ', ')."
}
$valuesByInstance = @{}
foreach ($set in $resultSets) {
foreach ($cs in $set.CounterSamples) {
$inst = $cs.InstanceName
if (-not $inst -or $inst -eq '_Total') { continue }
if (-not $valuesByInstance.ContainsKey($inst)) { $valuesByInstance[$inst] = @() }
$valuesByInstance[$inst] += [double]$cs.CookedValue
}
}
$averages = foreach ($kv in $valuesByInstance.GetEnumerator()) {
$avg = if ($kv.Value.Count) { ($kv.Value | Measure-Object -Average).Average } else { 0 }
[PSCustomObject]@{ Instance = $kv.Key; Average = [Math]::Round([double]$avg, 4) }
}
[PSCustomObject]@{
CounterPath = $usedCounter
IntervalSeconds = $IntervalSeconds
DurationSeconds = $DurationSeconds
SampleCount = $sampleCount
Results = $averages | Sort-Object Instance
}
}
try {
$data = Get-DiskQueueAverages -DurationSeconds $DurationSeconds -IntervalSeconds $IntervalSeconds
$logPath = Join-Path -Path $PSScriptRoot -ChildPath 'disk_queue_log.txt'
$ts = Get-Date -Format o
$lines = @()
$lines += "==== Disk Queue Monitor Run ===="
$lines += "Timestamp: $ts"
$lines += "Counter: $($data.CounterPath)"
$lines += "Interval: $($data.IntervalSeconds)s Duration: $($data.DurationSeconds)s Samples: $($data.SampleCount)"
foreach ($row in $data.Results) {
$lines += ("{0,-20} avg={1}" -f $row.Instance, $row.Average)
}
$lines += ""
$lines | Out-File -FilePath $logPath -Encoding utf8 -Append
Write-Host "Average 'Avg. Disk Queue Length' over $($data.DurationSeconds)s (every $($data.IntervalSeconds)s):" -ForegroundColor Cyan
$data.Results | Sort-Object Average -Descending | Format-Table -AutoSize Instance, Average
Write-Host "Logged details to: $logPath" -ForegroundColor DarkGray
}
catch {
Write-Error $_
exit 1
}