Skip to content

Commit dddd085

Browse files
committed
add CPU metrics
1 parent a7c8ef7 commit dddd085

File tree

1 file changed

+69
-2
lines changed

1 file changed

+69
-2
lines changed

src/SdnDiagnostics.psm1

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1248,7 +1248,12 @@ function Show-SdnGatewayUtilization {
12481248
$NcRestCredential = [System.Management.Automation.PSCredential]::Empty,
12491249

12501250
[Parameter (Mandatory = $false, ParameterSetName = 'RestCertificate')]
1251-
[X509Certificate]$NcRestCertificate
1251+
[X509Certificate]$NcRestCertificate,
1252+
1253+
[Parameter(Mandatory = $false)]
1254+
[System.Management.Automation.PSCredential]
1255+
[System.Management.Automation.Credential()]
1256+
$Credential
12521257
)
12531258

12541259
$ncRestParams = @{
@@ -1261,6 +1266,34 @@ function Show-SdnGatewayUtilization {
12611266
$ncRestParams.Add('NcRestCredential', $NcRestCredential)
12621267
}
12631268

1269+
1270+
$cpuCounterScriptBlock = {
1271+
try {
1272+
$counters = Get-Counter -Counter "\Processor(*)\% Processor Time" -SampleInterval 2 -MaxSamples 5 -ErrorAction Stop
1273+
1274+
# Group samples by CPU and calculate average
1275+
$cpuData = $counters.CounterSamples |
1276+
Where-Object { $_.InstanceName -ne '_total' } |
1277+
Group-Object -Property InstanceName |
1278+
ForEach-Object {
1279+
[PSCustomObject]@{
1280+
ProcessorId = $_.Name
1281+
Utilization = [math]::Round(($_.Group | Measure-Object -Property CookedValue -Average).Average, 2)
1282+
}
1283+
}
1284+
1285+
return [PSCustomObject]@{
1286+
Timestamp = Get-Date
1287+
Processors = $cpuData
1288+
AverageUtilization = [math]::Round(($cpuData | Measure-Object -Property Utilization -Average).Average, 2)
1289+
}
1290+
}
1291+
catch {
1292+
$_ | Write-Error
1293+
return $null
1294+
}
1295+
}
1296+
12641297
$gatewayPoolArray = @()
12651298

12661299
try {
@@ -1298,6 +1331,13 @@ function Show-SdnGatewayUtilization {
12981331
VirtualGatewayCount = $gatewayResource.properties.virtualGateways.virtualGateway.Count
12991332
NetworkConnectionCount = $gatewayResource.properties.virtualGateways.networkConnections.Count
13001333
VirtualGateways = @()
1334+
CpuMetrics = @()
1335+
}
1336+
1337+
$gatewayFqdn = Get-SdnGateway @ncRestParams -ResourceId $gatewayResource.resourceId -ManagementAddressOnly
1338+
$cpuMetrics = Invoke-PSRemoteCommand -ComputerName $gatewayFqdn -Credential $Credential -ScriptBlock $cpuCounterScriptBlock -ErrorAction Continue
1339+
if ($cpuMetrics) {
1340+
$gatewayObject.CpuMetrics = $cpuMetrics
13011341
}
13021342

13031343
if ($gatewayresource.properties.virtualGateways.Count -ge 1) {
@@ -1432,6 +1472,33 @@ function Show-SdnGatewayUtilization {
14321472
"`n`n" | Write-Host
14331473
}
14341474

1475+
# Display CPU metrics if available
1476+
if ($gateway.CpuMetrics -and $gateway.CpuMetrics.Processors) {
1477+
"`t`tCPU Utilization (sampled at {0}):" -f $gateway.CpuMetrics.Timestamp.ToString('yyyy-MM-dd HH:mm:ss') | Write-Host -ForegroundColor Cyan
1478+
1479+
foreach ($cpu in $gateway.CpuMetrics.Processors | Sort-Object -Property ProcessorId) {
1480+
$cpuBar = '' * [int]($cpu.Utilization / 2.5) # Scale to ~40 chars max
1481+
$cpuColor = if ($cpu.Utilization -ge 90) { 'Red' }
1482+
elseif ($cpu.Utilization -ge 75) { 'Yellow' }
1483+
else { 'Green' }
1484+
1485+
Write-Host "`t`t CPU $($cpu.ProcessorId): " -NoNewline
1486+
Write-Host ("{0,6:N2}% " -f $cpu.Utilization) -NoNewline
1487+
Write-Host "[$cpuBar]" -ForegroundColor $cpuColor
1488+
}
1489+
1490+
# Display average
1491+
$avgBar = '' * [int]($gateway.CpuMetrics.AverageUtilization / 2.5)
1492+
$avgColor = if ($gateway.CpuMetrics.AverageUtilization -ge 90) { 'Red' }
1493+
elseif ($gateway.CpuMetrics.AverageUtilization -ge 75) { 'Yellow' }
1494+
else { 'Green' }
1495+
1496+
Write-Host "`t`t Average: " -NoNewline
1497+
Write-Host ("{0,6:N2}% " -f $gateway.CpuMetrics.AverageUtilization) -NoNewline
1498+
Write-Host "[$avgBar]" -ForegroundColor $avgColor
1499+
"`n" | Write-Host
1500+
}
1501+
14351502
if ($gateway.VirtualGateways.Count -gt 0) {
14361503
"`t`tNetwork Connections:" | Write-Host -ForegroundColor Cyan
14371504
$tableOutput = $gateway.VirtualGateways | Sort-Object -Property VirtualGateway | Format-Table -AutoSize -Property @(
@@ -1457,7 +1524,7 @@ function Show-SdnGatewayUtilization {
14571524
}
14581525
}
14591526
catch {
1460-
#$_ | Trace-Exception
1527+
$_ | Trace-Exception
14611528
throw $_.Exception.Message
14621529
}
14631530

0 commit comments

Comments
 (0)