From 7aafed9d44fbf6acbf4a41aec17b6dcf72c627de Mon Sep 17 00:00:00 2001 From: Crited Date: Wed, 30 Oct 2019 08:37:24 +0100 Subject: [PATCH] Changes to Memory Check to support GB and Percentage --- lib/plugins/Invoke-IcingaCheckMemory.psm1 | 29 ++++++++++--------- .../Get-IcingaMemoryPerformanceCounter.psm1 | 22 +++++--------- 2 files changed, 24 insertions(+), 27 deletions(-) diff --git a/lib/plugins/Invoke-IcingaCheckMemory.psm1 b/lib/plugins/Invoke-IcingaCheckMemory.psm1 index a28f03f..b714d59 100644 --- a/lib/plugins/Invoke-IcingaCheckMemory.psm1 +++ b/lib/plugins/Invoke-IcingaCheckMemory.psm1 @@ -40,24 +40,27 @@ function Invoke-IcingaCheckMemory() param( $Critical = $null, $Warning = $null, + $CriticalPercent = $null, + $WarningPercent = $null, [switch]$PageFile, [ValidateSet(0, 1, 2, 3)] [int]$Verbosity = 0 ); - if ($PageFile -eq $TRUE) { - $MemoryData = (Get-IcingaPageFilePerformanceCounter).'(*)\% usage'.'\Paging File(_Total)\% usage'.value - } else { - $MemoryData = (Get-IcingaMemoryPerformanceCounter).'% committed bytes in use'.value; - } + $MemoryPackage = New-IcingaCheckPackage -Name 'Memory Usage' -OperatorAnd -Verbos $Verbosity; + $MemoryData = (Get-IcingaMemoryPerformanceCounterFormated); + - $MemoryCheck = New-IcingaCheck -Name '% Memory Check' -Value $MemoryData -NoPerfData; + $MemoryPerc = New-IcingaCheck -Name 'Memory Percent' -Value $MemoryData.'Memory %' -NoPerfData; + $MemoryByte = New-IcingaCheck -Name 'Memory GigaByte' -Value $MemoryData.'Memory GigaByte' -NoPerfData; + $PageFile = New-IcingaCheck -Name 'PageFile Percent' -Value $MemoryData.'PageFile %' -NoPerfData; - $MemoryCheck.WarnOutOfRange( - ($Warning) - ).CritOutOfRange( - ($Critical) - ) | Out-Null; - - return (New-IcingaCheckresult -Check $MemoryCheck -NoPerfData $TRUE -Compile); + # PageFile To-Do + $MemoryPerc.WarnOutOfRange($Warning).CritOutOfRange($Critical) | Out-Null; + $MemoryByte.WarnOutOfRange($WarningPercent).CritOutOfRange($CriticalPercent) | Out-Null; + + $MemoryPackage.AddCheck($MemoryPerc); + $MemoryPackage.AddCheck($MemoryByte); + + return (New-IcingaCheckResult -Check $MemoryPackage -NoPerfData $TRUE -Compile); } \ No newline at end of file diff --git a/lib/provider/memory/Get-IcingaMemoryPerformanceCounter.psm1 b/lib/provider/memory/Get-IcingaMemoryPerformanceCounter.psm1 index 28ed1ae..9fcf04b 100644 --- a/lib/provider/memory/Get-IcingaMemoryPerformanceCounter.psm1 +++ b/lib/provider/memory/Get-IcingaMemoryPerformanceCounter.psm1 @@ -1,27 +1,21 @@ function Get-IcingaMemoryPerformanceCounter() { - $MemoryStart = (Show-IcingaPerformanceCounters -CounterCategory 'Memory'); - $MemoryCounter = New-IcingaPerformanceCounterArray -Counter $MemoryStart; + $MemoryPercent = New-IcingaPerformanceCounterArray -Counter "\Memory\% committed bytes in use","\Memory\committed bytes","\Paging File(_Total)\% usage" [hashtable]$Result = @{}; - foreach ($item in $MemoryCounter.Keys) { - $counter = $item.trimstart('\Memory\'); - $Result.Add($counter, $MemoryCounter[$item]); + foreach ($item in $MemoryPercent.Keys) { + $Result.Add($item, $MemoryPercent[$item]); } return $Result; } -function Get-IcingaPageFilePerformanceCounter() +function Get-IcingaMemoryPerformanceCounterFormated() { - $PageFileStart = (Show-IcingaPerformanceCounters -CounterCategory 'Paging File'); - $PageFileCounter = New-IcingaPerformanceCounterArray -Counter $PageFileStart; [hashtable]$Result = @{}; - - foreach ($item in $PageFileCounter.Keys) { - $counter = $item.trimstart('\Paging File\'); - $Result.Add($counter, $PageFileCounter[$item]); - } - + $Result.Add('Memory %', (Get-IcingaMemoryPerformanceCounter).'\Memory\% committed bytes in use'.value); + $Result.Add('Memory GigaByte', (ConvertTo-GigaByte ([decimal](Get-IcingaMemoryPerformanceCounter).'\Memory\committed bytes'.value) -Unit Byte)); + $Result.Add('PageFile %', (Get-IcingaMemoryPerformanceCounter).'\Paging File(_Total)\% usage'.value); + return $Result; } \ No newline at end of file