Fix for IcingaMemoryCheck; Improve speed, changed provider

This commit is contained in:
Crited 2019-10-30 10:44:44 +01:00
parent 34d33e9b88
commit d2ef01eae5
2 changed files with 53 additions and 27 deletions

View file

@ -1,4 +1,5 @@
Import-IcingaLib provider\memory;
Import-IcingaLib core\tools;
<#
.SYNOPSIS
@ -16,20 +17,44 @@ Import-IcingaLib provider\memory;
[WARNING]: % Memory Check 78.74 is greater than 60
1
.EXAMPLE
PS> Invoke-IcingaCheckMemory -Verbosity 3 -Warning 60 -Critical 80 -PageFile
[OK]: % Memory Check is 10.99
0
PS> Invoke-IcingaCheckMemory -Unit GB -Critical 1850000KB -Warning 2500000KB -CriticalPercent 80 -WarningPercent 30 -Verbosity 3
[Warning]: Check package "Memory Usage" is [Warning] (Match All)
\_ [WARNING]: Memory Percent 35.95% is greater than 30%
\_ [OK]: PageFile Percent is 16.42%
| 'Memory_Percent'=35.95%;30;80;0;100 'Used_Bytes_in_GB'=3.44;2.5;1.85 'PageFile_Percent'=16.42%;;;0;100
1
.PARAMETER Warning
Used to specify a Warning threshold. In this case an integer value.
Used to specify a Warning threshold. In this case an string value.
The string has to be like, "20B", "20KB", "20MB", "20GB", "20TB", "20TB"
.PARAMETER Critical
Used to specify a Critical threshold. In this case an integer value.
Used to specify a Critical threshold. In this case an string value.
The string has to be like, "20B", "20KB", "20MB", "20GB", "20TB", "20TB"
.PARAMETER Pagefile
Switch which determines whether the pagefile should be used instead.
If not set memory will be checked.
.PARAMETER CriticalPercent
Used to specify a Critical threshold. In this case an integer value.
Like 30 for 30%. If memory usage is above 30%, the check will return CRITICAL.
.PARAMETER CriticalPercent
Used to specify a Critical threshold. In this case an integer value.
Like 30 for 30%. If memory usage is above 30%, the check will return Warning.
.PARAMETER Unit
Determines output Unit. Allowed Units: 'B', 'KB', 'MB', 'GB', 'TB', 'PB'
.INPUTS
System.String
.OUTPUTS
System.String
.LINK
https://github.com/LordHepipud/icinga-module-windows
.NOTES
@ -38,8 +63,9 @@ Import-IcingaLib provider\memory;
function Invoke-IcingaCheckMemory()
{
param(
$Critical = $null,
$Warning = $null,
[string]$Critical = $null,
[string]$Warning = $null,
[string]$Unit = 'B',
$CriticalPercent = $null,
$WarningPercent = $null,
[switch]$PageFile,
@ -48,21 +74,24 @@ function Invoke-IcingaCheckMemory()
[switch]$NoPerfData
);
$CrticalConverted = Convert-Bytes $Critical -Unit $Unit
$WarningConverted = Convert-Bytes $Warning -Unit $Unit
$MemoryPackage = New-IcingaCheckPackage -Name 'Memory Usage' -OperatorAnd -Verbos $Verbosity;
$MemoryData = (Get-IcingaMemoryPerformanceCounterFormated);
$MemoryData = (Get-IcingaMemoryPerformanceCounter);
$MemoryPerc = New-IcingaCheck -Name 'Memory Percent' -Value $MemoryData['Memory %'] -Unit '%';
$MemoryByte = New-IcingaCheck -Name 'Memory GigaByte' -Value $MemoryData['Memory GigaByte'] -Unit 'GB';
$PageFileCheck = New-IcingaCheck -Name 'PageFile Percent' -Value $MemoryData['PageFile %'] -Unit '%';
$MemoryPerc = New-IcingaCheck -Name 'Memory Percent' -Value $MemoryData['Memory Used %'] -Unit '%';
$MemoryByte = New-IcingaCheck -Name "Used Bytes in $Unit" -Value (Convert-Bytes ([string]::Format('{0}B', $MemoryData['Memory used Bytes'])) -Unit $Unit);
#$PageFileCheck = New-IcingaCheck -Name 'PageFile Percent' -Value $MemoryData['PageFile %'] -Unit '%';
# PageFile To-Do
$MemoryPerc.WarnOutOfRange($Warning).CritOutOfRange($Critical) | Out-Null;
$MemoryByte.WarnOutOfRange($WarningPercent).CritOutOfRange($CriticalPercent) | Out-Null;
$MemoryByte.WarnOutOfRange($WarningConverted).CritOutOfRange($CrticalConverted) | Out-Null;
$MemoryPerc.WarnOutOfRange($WarningPercent).CritOutOfRange($CriticalPercent) | Out-Null;
$MemoryPackage.AddCheck($MemoryPerc);
$MemoryPackage.AddCheck($MemoryByte);
$MemoryPackage.AddCheck($PageFileCheck);
#$MemoryPackage.AddCheck($PageFileCheck);
return (New-IcingaCheckResult -Check $MemoryPackage -NoPerfData $NoPerfData -Compile);
}

View file

@ -1,21 +1,18 @@
function Get-IcingaMemoryPerformanceCounter()
{
$MemoryPercent = New-IcingaPerformanceCounterArray -Counter "\Memory\% committed bytes in use","\Memory\committed bytes","\Paging File(_Total)\% usage"
[hashtable]$Result = @{};
$MemoryPercent = New-IcingaPerformanceCounterArray -Counter "\Memory\% committed bytes in use","\Memory\Available Bytes","\Paging File(_Total)\% usage"
[hashtable]$Initial = @{};
[hashtable]$MemoryData = @{};
foreach ($item in $MemoryPercent.Keys) {
$Result.Add($item, $MemoryPercent[$item]);
$Initial.Add($item, $MemoryPercent[$item]);
}
return $Result;
}
$MemoryData.Add('Memory Available Bytes', [decimal]($Initial.'\Memory\Available Bytes'.value));
$MemoryData.Add('Memory Total Bytes', (Get-CimInstance Win32_ComputerSystem).TotalPhysicalMemory);
$MemoryData.Add('Memory Used Bytes', $MemoryData.'Memory Total Bytes' - $MemoryData.'Memory Available Bytes');
$MemoryData.Add('Memory Used %', $MemoryData.'Memory Available Bytes' / $MemoryData.'Memory Total Bytes' * 100);
$MemoryData.Add('PageFile %', $Initial.'\Paging File(_Total)\% usage'.value);
function Get-IcingaMemoryPerformanceCounterFormated()
{
[hashtable]$Result = @{};
$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;
return $MemoryData;
}