Reverse thresholds for memory

This commit is contained in:
Alexander Stoll 2019-10-30 14:44:44 +01:00
parent 17d5269a7a
commit 2e7c91e6ca
3 changed files with 28 additions and 32 deletions

View file

@ -5,7 +5,7 @@ function Convert-Bytes()
[string]$Unit
);
If (($Value -Match "(^[0-9]*) ?(B|KB|MB|GB|TB|PT|Kibi|Mibi|Gibi|Tibi|Pibi)")) {
If (($Value -Match "(^[\d\.]*) ?(B|KB|MB|GB|TB|PT|Kibi|Mibi|Gibi|Tibi|Pibi)")) {
[single]$CurrentValue = $Matches[1];
[string]$CurrentUnit = $Matches[2];
@ -16,7 +16,7 @@ function Convert-Bytes()
switch ($Unit) {
#{ 'B' -contains $_} { $FinalValue = ConvertTo-ByteSI $CurrentValue -Unit B; $boolOption = $true;}
{ 'B' -contains $_} { $FinalValue = $CurrentValue; $boolOption = $true;}
{ 'KB' -contains $_} { $FinalValue = ConvertTo-KiloByte $CurrentValue -Unit B; $boolOption = $true;}
{ 'MB' -contains $_} { $FinalValue = ConvertTo-MegaByte $CurrentValue -Unit B; $boolOption = $true;}
{ 'GB' -contains $_} { $FinalValue = ConvertTo-GigaByte $CurrentValue -Unit B; $boolOption = $true;}

View file

@ -17,17 +17,12 @@ Import-IcingaLib core\tools;
[WARNING]: % Memory Check 78.74 is greater than 60
1
.EXAMPLE
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
PS>
.PARAMETER WarningBytes
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
.PARAMETER CriticalBytes
Used to specify a Critical threshold. In this case an string value.
The string has to be like, "20B", "20KB", "20MB", "20GB", "20TB", "20TB"
@ -39,15 +34,12 @@ Import-IcingaLib core\tools;
.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.
Like 30 for 30%. If memory usage is below 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'
Like 30 for 30%. If memory usage is below 30%, the check will return Warning.
.INPUTS
System.String
@ -63,9 +55,8 @@ Import-IcingaLib core\tools;
function Invoke-IcingaCheckMemory()
{
param(
[string]$Critical = $null,
[string]$Warning = $null,
[string]$Unit = 'B',
[string]$CriticalBytes = $null,
[string]$WarningBytes = $null,
$CriticalPercent = $null,
$WarningPercent = $null,
[switch]$PageFile,
@ -74,23 +65,28 @@ function Invoke-IcingaCheckMemory()
[switch]$NoPerfData
);
$CrticalConverted = Convert-Bytes $Critical -Unit $Unit
$WarningConverted = Convert-Bytes $Warning -Unit $Unit
If ([string]::IsNullOrEmpty($CriticalBytes) -eq $FALSE) {
[decimal]$CrticalConverted = Convert-Bytes $CriticalBytes -Unit B
}
If ([string]::IsNullOrEmpty($WarningBytes) -eq $FALSE) {
[decimal]$WarningConverted = Convert-Bytes $WarningBytes -Unit B
}
$MemoryPackage = New-IcingaCheckPackage -Name 'Memory Usage' -OperatorAnd -Verbos $Verbosity;
$MemoryData = (Get-IcingaMemoryPerformanceCounter);
$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);
$MemoryPerc = New-IcingaCheck -Name 'Memory Percent Available' -Value $MemoryData['Memory Available %'] -Unit '%';
$MemoryByteUsed = New-IcingaCheck -Name "Used Bytes" -Value $MemoryData['Memory Used Bytes'] -Unit 'B';
$MemoryByteAvailable = New-IcingaCheck -Name "Available Bytes" -Value $MemoryData['Memory Available Bytes'] -Unit 'B';
#$PageFileCheck = New-IcingaCheck -Name 'PageFile Percent' -Value $MemoryData['PageFile %'] -Unit '%';
# PageFile To-Do
$MemoryByte.WarnOutOfRange($WarningConverted).CritOutOfRange($CrticalConverted) | Out-Null;
$MemoryPerc.WarnOutOfRange($WarningPercent).CritOutOfRange($CriticalPercent) | Out-Null;
$MemoryByteAvailable.WarnIfLowerThan($WarningConverted).CritIfLowerThan($CrticalConverted) | Out-Null;
$MemoryPerc.WarnIfLowerThan($WarningPercent).CritIfLowerThan($CriticalPercent) | Out-Null;
$MemoryPackage.AddCheck($MemoryPerc);
$MemoryPackage.AddCheck($MemoryByte);
$MemoryPackage.AddCheck($MemoryByteAvailable);
$MemoryPackage.AddCheck($MemoryByteUsed);
#$MemoryPackage.AddCheck($PageFileCheck);
return (New-IcingaCheckResult -Check $MemoryPackage -NoPerfData $NoPerfData -Compile);

View file

@ -11,7 +11,7 @@ function Get-IcingaMemoryPerformanceCounter()
$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('Memory Available %', 100 - ($MemoryData.'Memory Available Bytes' / $MemoryData.'Memory Total Bytes' * 100));
$MemoryData.Add('PageFile %', $Initial.'\Paging File(_Total)\% usage'.value);
return $MemoryData;