-
Notifications
You must be signed in to change notification settings - Fork 17
/
perfmon-console-graph.ps1
297 lines (247 loc) · 11.6 KB
/
perfmon-console-graph.ps1
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
<#
.SYNOPSIS
script to write output of one or more perfmon counters to console.
.DESCRIPTION
script to write output of one or more perfmon counters to console.
objects are stored in $global:counterObjs. counters are stored in $global:allCounters.
version 1.0
.PARAMETER counters
[string[]]one or more perfmon counters to write to the console
.PARAMETER sleepSeconds
[double]the number of seconds to sleep between samples. default is 1
.PARAMETER maxSamples
[double]the maximum number of samples to take. default is 1
.PARAMETER noChart
[switch]don't show chart output
.PARAMETER noColor
[switch]don't use color in the output
.PARAMETER newLine
[bool]write each sample on a new line. default is to write each sample on the same line
.PARAMETER useScaleAsSymbol
[bool]use the scale as the symbol
.PARAMETER computername
[string]the computer to query. default is the local computer
.PARAMETER matchCounters
[switch]match counters
.PARAMETER listCounters
[string]list counters that match the specified string
.EXAMPLE
perfmon-console-graph.ps1 -counters "\\$env:computername\memory\Available MBytes", "\\$env:computername\memory\% committed bytes in use", "\\$env:computername\Process(fabric*)\% Processor Time", "\\$env:computername\Processor(_Total)\% Processor Time", "\\$env:computername\PhysicalDisk(*:)\Avg. Disk Queue Length", "\\$env:computername\Paging File(*\pagefile.sys)\*", "\\$env:computername\Tcpv4\Segments Received/sec", "\\$env:computername\Tcpv4\Segments Sent/sec", "\\$env:computername\Tcpv4\Segments Retransmitted/sec"
.EXAMPLE
perfmon-console-graph.ps1 -counters "\\$env:computername\memory\Available MBytes" -sleepSeconds 1 -maxSamples 1
.EXAMPLE
perfmon-console-graph.ps1 -counters "current disk queue" -matchCounters
.EXAMPLE
perfmon-console-graph.ps1 -list "pagefile"
.LINK
invoke-webRequest "https://raw.githubusercontent.com/jagilber/powershellScripts/master/perfmon-console-graph.ps1" -outfile $pwd/perfmon-console-graph.ps1
#>
[cmdletbinding()]
param(
[string]$computername = $env:computername,
[string[]]$counters = @(
"\\$computername\memory\Available MBytes",
"\\$computername\memory\% committed bytes in use",
"\\$computername\Process(fabric*)\% Processor Time",
"\\$computername\Processor(_Total)\% Processor Time",
"\\$computername\PhysicalDisk(*:)\Avg. Disk Queue Length",
"\\$computername\Paging File(*\pagefile.sys)\*",
"\\$computername\Tcpv4\Segments Received/sec",
"\\$computername\Tcpv4\Segments Sent/sec",
"\\$computername\Tcpv4\Segments Retransmitted/sec"
),
[double]$sleepSeconds = 1,
[double]$maxSamples = 1,
[switch]$noChart,
[switch]$noColor,
[bool]$newLine = $true,
[bool]$useScaleAsSymbol = $false,
[switch]$matchCounters,
[string]$listCounters,
[switch]$force
)
$ErrorActionPreference = 'continue'
$global:counterObjs = @{}
$script:colorCount = 0
$script:bgColorCount = 0
$consoleColors = [enum]::GetNames([consolecolor])
$currentBackgroundColor = [console]::BackgroundColor
$currentForegroundColor = [console]::ForegroundColor
[double]$scale = 100
function main() {
try {
add-counters $force
if ($listCounters) {
$global:allCounters.Counter -imatch $listCounters
return
}
#remove-counters
monitor-counters
}
finally {
[console]::BackgroundColor = $currentBackgroundColor
[console]::ForegroundColor = $currentForegroundColor
write-host "values stored in `$global:counterObjs" -ForegroundColor Green
write-host "counters stored in `$global:allCounters" -ForegroundColor Green
}
}
function add-counterObj($counter, $match = $false) {
$backgroundColor = $currentBackgroundColor
$foregroundColor = $currentForegroundColor
write-host "adding counter $counter" -ForegroundColor Cyan
$noRootCounter = [regex]::replace($counter, '^\\\\.*?\\','\')
if (!$noRootCounter) {
$noRootCounter = $counter
}
$withRootCounter = "\\$computername\$($noRootCounter.TrimStart('\'))"
if ($match) {
$counterItems = @($global:allCounters.paths -imatch [regex]::Escape($noRootCounter))
$counterItems += @($global:allCounters.pathsWithInstances -imatch [regex]::Escape($noRootCounter))
if ($counterItems.Count -gt 0) {
write-host "multiple counters found for $counter" -ForegroundColor Cyan
foreach ($counterItem in $counterItems) {
#write-host $counter
add-counterObj $counterItem $false
}
return
}
else {
$counter = $counterItems[0]
}
}
elseif (!($global:allCounters.paths -contains $noRootCounter) -and !($global:allCounters.paths -contains $withRootCounter) `
-and !($global:allCounters.pathsWithInstances -contains $noRootCounter) -and !($global:allCounters.pathsWithInstances -contains $withRootCounter)) {
write-host "counter $counter not found" -ForegroundColor Yellow
}
do {
$backgroundColor = $consoleColors[[math]::Min($consoleColors.Count - 1, $script:bgColorCount++)]
}while ($backgroundColor -ieq $currentForegroundColor)
$script:colorCount = 0
$backgroundColor = $consoleColors[[math]::Min($consoleColors.Count - 1, $script:bgColorCount)]
do {
$foregroundColor = $consoleColors[[math]::Min($consoleColors.Count - 1, $script:colorCount++)]
}while ($foregroundColor -ieq $currentBackgroundColor)
$counterInfo = @{
backgroundColor = $backgroundColor
foregroundColor = $foregroundColor
lastScale = $scale
lastCounter = 0
counterSamples = 0
averageCounter = 0
maxCounter = 0
minCounter = [double]::MaxValue
}
if (!$global:counterObjs.Contains($withRootCounter)) {
$global:counterObjs.Add($withRootCounter, $counterInfo)
}
}
function add-counters($force = $false) {
if (!$global:AllCounters -or $force) {
Write-Host "loading counter list..."
$global:allCounters = Get-Counter -ListSet * -ComputerName $computername
Write-Host "done loading counter list" -ForegroundColor Green
}
foreach ($counter in $counters) {
add-counterObj $counter $matchCounters
}
}
function monitor-counters() {
write-host "monitoring counters..." -ForegroundColor Green
while ($global:counterObjs.Count -gt 0) {
write-verbose "get-counter -counter @($($global:counterObjs.Keys | out-string)) -SampleInterval ([math]::max(1, $sleepSeconds)) -MaxSamples $maxSamples"
$error.clear()
$counterSamples = get-counter -counter @($global:counterObjs.Keys) -SampleInterval ([math]::max(1, $sleepSeconds)) -MaxSamples $maxSamples -ComputerName $computername
if ($error) {
# todo - remove counters that don't exist
#add-counters $true
remove-counters
$error.clear()
}
foreach ($sample in $counterSamples.CounterSamples) {
$data = $sample.CookedValue
$sampleName = $sample.Path # $sample.Readings.split(' :')[0]
$sizedScale = $scale
$percentSize = ($data * 100) / $sizedScale
if (!$global:counterObjs[$sampleName]) {
add-counterObj $sampleName $matchCounters
}
while ($percentSize -gt 100) {
$percentSize = ($data * 100) / $sizedScale
if ($percentSize -gt 100) {
$sizedScale *= 10
}
}
$hostForegroundColor = $currentForegroundColor
$hostBackgroundColor = $currentBackgroundColor
$lastScale = $minCounter = $global:counterObjs[$sampleName].lastScale
$lastCounter = $minCounter = $global:counterObjs[$sampleName].lastCounter
$hostForegroundColor = $global:counterObjs[$sampleName].foregroundColor
$hostBackgroundColor = $global:counterObjs[$sampleName].backgroundColor
$global:counterObjs[$sampleName].counterSamples++
$avgCounter = $global:counterObjs[$sampleName].averageCounter = (($global:counterObjs[$sampleName].averageCounter * ($global:counterObjs[$sampleName].counterSamples - 1)) + $data) / $global:counterObjs[$sampleName].counterSamples
$maxCounter = $global:counterObjs[$sampleName].maxCounter = [math]::Max([double]$global:counterObjs[$sampleName].maxCounter, [double]$data)
$minCounter = $global:counterObjs[$sampleName].minCounter = [math]::Min([double]$global:counterObjs[$sampleName].minCounter, [double]$data)
$global:counterObjs[$sampleName].lastScale = $sizedScale
$global:counterObjs[$sampleName].lastCounter = $data
$counterDetails = "scale:$sizedScale avg:$($avgCounter.ToString("0.0")) min:$minCounter max:$maxCounter counter:$($sampleName.replace("\\$env:computername".tolower(),[string]::Empty))"
$trendSize = [math]::Abs($sizedScale.tostring().length - $lastScale.tostring().length) + 1
$trend = ">"
$graphSymbol = "X"
$diffSymbol = ""
$diffSize = (([double]$data - [double]$lastCounter) * 100) / $sizedScale
$graphSymbolMultiplier = $percentSize
$diffSymbolMultiplier = 0
if ($diffSize -gt 0) {
$trend = "^" * $trendSize
$graphSymbol = ">"
$diffSymbol = "+"
$graphSymbolMultiplier = [math]::min($percentSize, [math]::abs($percentSize - $diffSize))
$diffSymbolMultiplier = [math]::min($percentSize, [math]::abs($diffSize))
}
elseif ($diffSize -lt 0) {
$trend = "v" * $trendSize
$graphSymbol = "<"
$diffSymbol = "-"
$graphSymbolMultiplier = [math]::min($percentSize, [math]::abs($percentSize - $diffSize))
$diffSymbolMultiplier = [math]::min($sizedScale - $percentSize, [math]::abs($diffSize))
}
if ($useScaleAsSymbol) {
$graphSymbol = $(($sizedScale.tostring().length - $sizedScale.tostring().length).tostring())
}
if ($noChart) {
$output = "$trend $($data.ToString("0.0")) $counterDetails"
}
else {
if ($newLine) {
$graphPoint = ($graphSymbol * $graphSymbolMultiplier) + ($diffSymbol * $diffSymbolMultiplier)
$graph = "[$(($graphPoint).tostring().padright(100))]"
$output = "$trend $($data.ToString("0.0")) $counterDetails`r`n`t$graph"
}
else {
$output = "$graph $trend $($data.ToString("0.0")) $counterDetails"
}
}
if ($noColor) {
write-host $output
}
else {
write-host $output -ForegroundColor $hostForegroundColor -BackgroundColor $hostBackgroundColor
}
}
start-sleep -Seconds $sleepSeconds
}
}
function remove-counters() {
# remove counters that don't exist
foreach ($key in $global:counterObjs.clone().Keys) {
$error.clear()
Write-Verbose "get-counter -counter @($key) -SampleInterval ([math]::max(1, $sleepSeconds)) -MaxSamples $maxSamples"
get-counter -counter @($key) -SampleInterval ([math]::max(1, $sleepSeconds)) -MaxSamples $maxSamples -ComputerName $computername -ErrorAction SilentlyContinue
if ($error) {
write-host "error getting counter '$key'. $error removing from list..." -ForegroundColor Yellow
$error.clear()
$global:counterObjs.Remove($key)
}
}
}
main