Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for CPULoad issue #154 #167

Open
wants to merge 2 commits into
base: Development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 33 additions & 4 deletions src/Functions/PoShMon.Monitoring.OS/Test-CPULoad.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,43 @@ Function Test-CPULoad
[hashtable]$PoShMonConfiguration
)

# Initialization Section
if ($PoShMonConfiguration.OperatingSystem -eq $null) { throw "'OperatingSystem' configuration not set properly on PoShMonConfiguration parameter." }

$mainOutput = Get-InitialOutputWithTimer -SectionHeader "Server CPU Load Review" -OutputHeaders ([ordered]@{ 'ServerName' = 'Server Name'; 'CPULoad' = 'CPU Load (%)' })
$serverNames = $PoShMonConfiguration.General.ServerNames
$results = @()

if ($PoShMonConfiguration.General.ServerNames -eq $env:COMPUTERNAME)
{ $results = Get-Counter "\processor(_total)\% processor time" }
else
{ $results = Get-Counter "\processor(_total)\% processor time" -Computername $PoShMonConfiguration.General.ServerNames }
# Retreive processor counters from computers
if ($PoShMonConfiguration.General.ServerNames -is [String]) {

$serverName = $ServerNames

if (($serverName -eq $env:COMPUTERNAME) -or ($serverName -eq "localhost")) {
$results += Get-Counter "\processor(_total)\% processor time"
}
else {
throw "''OperatingSystem' in PoShMonconfiguration is not set properly. It had a single name listed, but was not the local system name. It must either be this or an array of computer names."
}

}

elseif ($PoShMonConfiguration.General.ServerNames -is [Array]) {

foreach ($serverName in $serverNames)
{
if (($serverName -eq $env:COMPUTERNAME) -or ($serverName -eq "localhost")) {
$results += Get-Counter "\processor(_total)\% processor time"
}
else {
$results += Get-Counter "\processor(_total)\% processor time" -Computername $ServerName
}
}
}

else {
throw "'OperatingSystem' in PoShMonconfiguration is not set properly. It must be either a string or an array"
}

foreach ($counterResult in $results.CounterSamples)
{
Expand Down
57 changes: 57 additions & 0 deletions src/Tests/CI/Unit/PoShMon.Monitoring.OS/Test-CPULoad.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -179,5 +179,62 @@ Describe "Test-CPULoad" {
$actual.OutputValues.Highlight.Count | Should Be 1
$actual.OutputValues.Highlight | Should Be 'CPULoad'
}
It "Should allow an array as input without the current machine listed" {

Mock -CommandName Get-Counter -MockWith {
$sample1 = [CounterSampleMock]::new("\\Server1\\processor(_total)\% processor time", 12.345)
$sample2 = [CounterSampleMock]::new("\\Server1\\processor(_total)\% processor time", 57.789)
$samples = @($sample1, $sample2)
$timestamp = Get-Date
return [CounterResultsMock]::new($timestamp, $samples)
}

$poShMonConfiguration = New-PoShMonConfiguration {
General -ServerNames ("Server1","Server2")
OperatingSystem
}

$actual = Test-CPULoad $poShMonConfiguration

$actual.NoIssuesFound | Should Be $true
}
It "Should fail if string as input is not the local system" {

Mock -CommandName Get-Counter -MockWith {
$sample1 = [CounterSampleMock]::new("\\Server1\\processor(_total)\% processor time", 12.345)
$sample2 = [CounterSampleMock]::new("\\Server1\\processor(_total)\% processor time", 57.789)
$samples = @($sample1, $sample2)
$timestamp = Get-Date
return [CounterResultsMock]::new($timestamp, $samples)
}

$poShMonConfiguration = New-PoShMonConfiguration {
General -ServerNames 'NotLocalhost'
OperatingSystem
}

$actual = Test-CPULoad $poShMonConfiguration

$actual.NoIssuesFound | Should Be $true
}
It "Should succeed if a string as input is the current machine listed as localhost or the machine name" {

Mock -CommandName Get-Counter -MockWith {
$sample1 = [CounterSampleMock]::new("\\localhost\\processor(_total)\% processor time", 12.345)
$sample2 = [CounterSampleMock]::new("\\localhost\\processor(_total)\% processor time", 57.789)
$samples = @($sample1, $sample2)
$timestamp = Get-Date
return [CounterResultsMock]::new($timestamp, $samples)
}

$poShMonConfiguration = New-PoShMonConfiguration {
General -ServerNames 'localhost'
OperatingSystem
}

$actual = Test-CPULoad $poShMonConfiguration

$actual.NoIssuesFound | Should Be $true
}
}
}