Skip to content

Commit

Permalink
Merge pull request #2166 from microsoft/dpaul-PerfCounters
Browse files Browse the repository at this point in the history
Add Dynamic Memory Check and Current CPU Usage
  • Loading branch information
dpaulson45 authored Aug 8, 2024
2 parents 142acec + 00003a0 commit 05fc4c3
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,18 @@ function Invoke-AnalyzerHardwareInformation {
}
Add-AnalyzedResultInformation @params

if ($null -ne $osInformation.PerformanceCounters) {
$counter = $osInformation.PerformanceCounters | Where-Object { $_.OriginalCounterLookup -eq "\Processor(_Total)\% Processor Time" }

if ($null -ne $counter) {
$params = $baseParams + @{
Name = "Current Total Processor Usage"
Details = [System.Math]::Round($counter.CookedValue, 2)
}
Add-AnalyzedResultInformation @params
}
}

$numberOfProcessors = $hardwareInformation.Processor.NumberOfProcessors
$displayWriteType = "Green"
$displayValue = $numberOfProcessors
Expand Down Expand Up @@ -275,4 +287,35 @@ function Invoke-AnalyzerHardwareInformation {
AddHtmlOverviewValues = $true
}
Add-AnalyzedResultInformation @params

if ($hardwareInformation.ServerType -eq "HyperV" -or
$hardwareInformation.ServerType -eq "VMware") {
$params = $baseParams + @{
Name = "Dynamic Memory Detected"
Details = $false
DisplayWriteType = "Green"
}

if ($null -eq $osInformation.PerformanceCounters) {
$params.Details = "Unknown - No Performance Counters was able to be collected"
$params.DisplayWriteType = "Yellow"
} else {
if ($hardwareInformation.ServerType -eq "HyperV") {
$counterName = "\Hyper-V Dynamic Memory Integration Service\Maximum Memory, MBytes"
} else {
$counterName = "\VM Memory\Memory Reservation in MB"
}
$counter = $osInformation.PerformanceCounters | Where-Object { $_.OriginalCounterLookup -eq $counterName }

if ($null -eq $counter) {
$params.Details = "Unknown - Required Counter Not Loaded"
$params.DisplayWriteType = "Yellow"
} elseif (($counter.CookedValue / 1024) -ne $totalPhysicalMemory) {
$params.Details = "$true $($counter.CookedValue / 1024)GB is the allowed dynamic memory of the server. Not supported to have dynamic memory configured."
$params.DisplayWriteType = "Red"
}
}

Add-AnalyzedResultInformation @params
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,17 @@ function Get-OperatingSystemInformation {
Invoke-CatchActions
}

$params = @{
MachineName = $Server
Counter = @(
"\Hyper-V Dynamic Memory Integration Service\Maximum Memory, MBytes", # This is used to determine if dynamic memory is set on the Hyper-V machine.
"\Processor(_Total)\% Processor Time",
"\VM Memory\Memory Reservation in MB" # used to determine if dynamic memory is set on the VMware machine.
)
CustomErrorAction = "SilentlyContinue" # Required because not all counters would be there.
}

$counters = Get-LocalizedCounterSamples @params
$serverPendingReboot = (Get-ServerRebootPending -ServerName $Server -CatchActionFunction ${Function:Invoke-CatchActions})
$timeZoneInformation = Get-TimeZoneInformation -MachineName $Server -CatchActionFunction ${Function:Invoke-CatchActions}
$tlsSettings = Get-AllTlsSettings -MachineName $Server -CatchActionFunction ${Function:Invoke-CatchActions}
Expand Down Expand Up @@ -94,6 +105,7 @@ function Get-OperatingSystemInformation {
CredentialGuardCimInstance = $credentialGuardCimInstance
WindowsFeature = $windowsFeature
EventLogInformation = $eventLogInformation
PerformanceCounters = $counters
}
}
}
22 changes: 19 additions & 3 deletions Diagnostics/HealthChecker/Helpers/PerformanceCountersFunctions.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ function Get-CounterSamples {
try {
return (Get-Counter -ComputerName $MachineName -Counter $Counter -ErrorAction $CustomErrorAction).CounterSamples
} catch {
Write-Verbose "Failed ot get counter samples"
Invoke-CatchActions
Write-Verbose "Failed to get counter samples"
}
}

Expand All @@ -39,6 +38,7 @@ function Get-LocalizedCounterSamples {
)

Write-Verbose "Calling: $($MyInvocation.MyCommand)"
$localizedCounterLookup = @{}
$localizedCounters = @()

foreach ($computer in $MachineName) {
Expand All @@ -51,11 +51,27 @@ function Get-LocalizedCounterSamples {

if (-not ($localizedCounters.Contains($localizedFullCounterName))) {
$localizedCounters += $localizedFullCounterName
$localizedCounterLookup.Add($localizedCounterName, $counterObject.FullName)
}
}
}

return (Get-CounterSamples -MachineName $MachineName -Counter $localizedCounters -CustomErrorAction $CustomErrorAction)
$currentErrorIndex = $Error.Count
# Store the localized counter sample information so we can reverse engineer back to the English counter name so other code can handle it.
$localizedCounterSamples = (Get-CounterSamples -MachineName $MachineName -Counter $localizedCounters -CustomErrorAction $CustomErrorAction)

foreach ($localSample in $localizedCounterSamples) {
foreach ($key in $localizedCounterLookup.Keys) {
if ($localSample.Path -like "\\*$key") {
# Found the localized Counter lookup, now we want to add a property to be able to find the counters in other areas of code by the English name.
$localSample | Add-Member -MemberType NoteProperty -Name "OriginalCounterLookup" -Value $localizedCounterLookup[$key]
break
}
}
}
Invoke-ErrorCatchActionLoopFromIndex $currentErrorIndex

return $localizedCounterSamples
}

function Get-LocalizedPerformanceCounterName {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ Describe "Testing Health Checker by Mock Data Imports - Exchange 2013" {
TestObjectMatch "Max Processor Speed" 2200
TestObjectMatch "Physical Memory" 6

$Script:ActiveGrouping.Count | Should -Be 9
$Script:ActiveGrouping.Count | Should -Be 11
}

It "Display Results - NIC Settings" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ Describe "Testing Health Checker by Mock Data Imports - Exchange 2016" {
TestObjectMatch "Max Processor Speed" 2200
TestObjectMatch "Physical Memory" 6

$Script:ActiveGrouping.Count | Should -Be 10
$Script:ActiveGrouping.Count | Should -Be 12
}

It "Display Results - NIC Settings" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,9 @@ Describe "Testing Health Checker by Mock Data Imports" {
TestObjectMatch "All Processor Cores Visible" "Passed" -WriteType "Green"
TestObjectMatch "Max Processor Speed" 2200
TestObjectMatch "Physical Memory" 6 -WriteType "Yellow"
TestObjectMatch "Dynamic Memory Detected" $false -WriteType "Green"

$Script:ActiveGrouping.Count | Should -Be 9
$Script:ActiveGrouping.Count | Should -Be 11
}

It "Display Results - NIC Settings" {
Expand Down Expand Up @@ -218,7 +219,7 @@ Describe "Testing Health Checker by Mock Data Imports" {
TestObjectMatch "Manufacturer" "My Custom PC"
TestObjectMatch "Model" "CHG-GG"

$Script:ActiveGrouping.Count | Should -Be 12
$Script:ActiveGrouping.Count | Should -Be 13
}

It "Display Results - NIC Settings" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,18 @@ Describe "Testing Health Checker by Mock Data Imports" {
$obj.Add($suObject)
return $obj
}
Mock Get-LocalizedCounterSamples {
$objList = New-Object System.Collections.Generic.List[object]
$objList.Add(([PSCustomObject]@{
OriginalCounterLookup = "\Processor(_Total)\% Processor Time"
CookedValue = 55.55555
}))
$objList.Add(([PSCustomObject]@{
OriginalCounterLookup = "\Hyper-V Dynamic Memory Integration Service\Maximum Memory, MBytes"
CookedValue = 24576
}))
return $objList
}

SetDefaultRunOfHealthChecker "Debug_Scenario1_Results.xml"
}
Expand All @@ -120,6 +132,11 @@ Describe "Testing Health Checker by Mock Data Imports" {
TestObjectMatch "Common MSExchangeDagMgmt" ($displayFormat -f "MSExchangeDagMgmt", "Stopped", "Automatic") -WriteType "Yellow"
}

It "Dynamic Memory Set" {
SetActiveDisplayGrouping "Processor/Hardware Information"
TestObjectMatch "Dynamic Memory Detected" "True 24GB is the allowed dynamic memory of the server. Not supported to have dynamic memory configured." -WriteType "Red"
}

It "Http Proxy Settings" {
SetActiveDisplayGrouping "Operating System Information"
$httpProxy = GetObject "Http Proxy Setting"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ Describe "Testing Health Checker by Mock Data Imports" {
Assert-MockCalled Get-DnsClient -Exactly 1
Assert-MockCalled Get-NetAdapterRss -Exactly 1
Assert-MockCalled Get-HotFix -Exactly 1
Assert-MockCalled Get-LocalizedCounterSamples -Exactly 1
Assert-MockCalled Get-LocalizedCounterSamples -Exactly 2
Assert-MockCalled Get-ServerRebootPending -Exactly 1
Assert-MockCalled Get-AllTlsSettings -Exactly 1
Assert-MockCalled Get-SmbServerConfiguration -Exactly 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,6 @@ Mock Get-HotFix {
return Import-Clixml "$Script:MockDataCollectionRoot\OS\GetHotFix.xml"
}

Mock Get-LocalizedCounterSamples {
return Import-Clixml "$Script:MockDataCollectionRoot\OS\GetCounterSamples.xml"
}

Mock Get-ServerRebootPending {
return Import-Clixml "$Script:MockDataCollectionRoot\OS\GetServerRebootPending.xml"
}
Expand Down Expand Up @@ -265,6 +261,21 @@ Mock Get-ExchangeDiagnosticInfo -ParameterFilter { $Process -eq "Microsoft.Excha
Mock Get-ExchangeDiagnosticInfo -ParameterFilter { $Process -eq "EdgeTransport" -and $Component -eq "ResourceThrottling" } `
-MockWith { return Import-Clixml "$Script:MockDataCollectionRoot\Exchange\GetExchangeDiagnosticInfo_EdgeTransportResourceThrottling.xml" }

Mock Get-LocalizedCounterSamples -ParameterFilter { $Counter -eq "\Network Interface(*)\Packets Received Discarded" } `
-MockWith { return Import-Clixml "$Script:MockDataCollectionRoot\OS\GetCounterSamples.xml" }
Mock Get-LocalizedCounterSamples {
$objList = New-Object System.Collections.Generic.List[object]
$objList.Add(([PSCustomObject]@{
OriginalCounterLookup = "\Processor(_Total)\% Processor Time"
CookedValue = 55.55555
}))
$objList.Add(([PSCustomObject]@{
OriginalCounterLookup = "\Hyper-V Dynamic Memory Integration Service\Maximum Memory, MBytes"
CookedValue = 6144
}))
return $objList
}

# Need to use function instead of Mock for Exchange cmdlets
function Get-ExchangeServer {
return Import-Clixml "$Script:MockDataCollectionRoot\Exchange\GetExchangeServer.xml"
Expand Down

0 comments on commit 05fc4c3

Please sign in to comment.