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 [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]; [single]$CurrentValue = $Matches[1];
[string]$CurrentUnit = $Matches[2]; [string]$CurrentUnit = $Matches[2];
@ -16,7 +16,7 @@ function Convert-Bytes()
switch ($Unit) { 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;} { 'KB' -contains $_} { $FinalValue = ConvertTo-KiloByte $CurrentValue -Unit B; $boolOption = $true;}
{ 'MB' -contains $_} { $FinalValue = ConvertTo-MegaByte $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;} { '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 [WARNING]: % Memory Check 78.74 is greater than 60
1 1
.EXAMPLE .EXAMPLE
PS> Invoke-IcingaCheckMemory -Unit GB -Critical 1850000KB -Warning 2500000KB -CriticalPercent 80 -WarningPercent 30 -Verbosity 3 PS>
[Warning]: Check package "Memory Usage" is [Warning] (Match All) .PARAMETER WarningBytes
\_ [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 string 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" 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. Used to specify a Critical threshold. In this case an string value.
The string has to be like, "20B", "20KB", "20MB", "20GB", "20TB", "20TB" The string has to be like, "20B", "20KB", "20MB", "20GB", "20TB", "20TB"
@ -39,22 +34,19 @@ Import-IcingaLib core\tools;
.PARAMETER CriticalPercent .PARAMETER CriticalPercent
Used to specify a Critical threshold. In this case an integer value. 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 .PARAMETER CriticalPercent
Used to specify a Critical threshold. In this case an integer value. 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. Like 30 for 30%. If memory usage is below 30%, the check will return Warning.
.PARAMETER Unit
Determines output Unit. Allowed Units: 'B', 'KB', 'MB', 'GB', 'TB', 'PB'
.INPUTS .INPUTS
System.String System.String
.OUTPUTS .OUTPUTS
System.String System.String
.LINK .LINK
https://github.com/LordHepipud/icinga-module-windows https://github.com/LordHepipud/icinga-module-windows
.NOTES .NOTES
@ -63,34 +55,38 @@ Import-IcingaLib core\tools;
function Invoke-IcingaCheckMemory() function Invoke-IcingaCheckMemory()
{ {
param( param(
[string]$Critical = $null, [string]$CriticalBytes = $null,
[string]$Warning = $null, [string]$WarningBytes = $null,
[string]$Unit = 'B', $CriticalPercent = $null,
$CriticalPercent = $null, $WarningPercent = $null,
$WarningPercent = $null,
[switch]$PageFile, [switch]$PageFile,
[ValidateSet(0, 1, 2, 3)] [ValidateSet(0, 1, 2, 3)]
[int]$Verbosity = 0, [int]$Verbosity = 0,
[switch]$NoPerfData [switch]$NoPerfData
); );
$CrticalConverted = Convert-Bytes $Critical -Unit $Unit If ([string]::IsNullOrEmpty($CriticalBytes) -eq $FALSE) {
$WarningConverted = Convert-Bytes $Warning -Unit $Unit [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; $MemoryPackage = New-IcingaCheckPackage -Name 'Memory Usage' -OperatorAnd -Verbos $Verbosity;
$MemoryData = (Get-IcingaMemoryPerformanceCounter); $MemoryData = (Get-IcingaMemoryPerformanceCounter);
$MemoryPerc = New-IcingaCheck -Name 'Memory Percent Available' -Value $MemoryData['Memory Available %'] -Unit '%';
$MemoryPerc = New-IcingaCheck -Name 'Memory Percent' -Value $MemoryData['Memory Used %'] -Unit '%'; $MemoryByteUsed = New-IcingaCheck -Name "Used Bytes" -Value $MemoryData['Memory Used Bytes'] -Unit 'B';
$MemoryByte = New-IcingaCheck -Name "Used Bytes in $Unit" -Value (Convert-Bytes ([string]::Format('{0}B', $MemoryData['Memory used Bytes'])) -Unit $Unit); $MemoryByteAvailable = New-IcingaCheck -Name "Available Bytes" -Value $MemoryData['Memory Available Bytes'] -Unit 'B';
#$PageFileCheck = New-IcingaCheck -Name 'PageFile Percent' -Value $MemoryData['PageFile %'] -Unit '%'; #$PageFileCheck = New-IcingaCheck -Name 'PageFile Percent' -Value $MemoryData['PageFile %'] -Unit '%';
# PageFile To-Do # PageFile To-Do
$MemoryByte.WarnOutOfRange($WarningConverted).CritOutOfRange($CrticalConverted) | Out-Null; $MemoryByteAvailable.WarnIfLowerThan($WarningConverted).CritIfLowerThan($CrticalConverted) | Out-Null;
$MemoryPerc.WarnOutOfRange($WarningPercent).CritOutOfRange($CriticalPercent) | Out-Null; $MemoryPerc.WarnIfLowerThan($WarningPercent).CritIfLowerThan($CriticalPercent) | Out-Null;
$MemoryPackage.AddCheck($MemoryPerc); $MemoryPackage.AddCheck($MemoryPerc);
$MemoryPackage.AddCheck($MemoryByte); $MemoryPackage.AddCheck($MemoryByteAvailable);
$MemoryPackage.AddCheck($MemoryByteUsed);
#$MemoryPackage.AddCheck($PageFileCheck); #$MemoryPackage.AddCheck($PageFileCheck);
return (New-IcingaCheckResult -Check $MemoryPackage -NoPerfData $NoPerfData -Compile); 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 Available Bytes', [decimal]($Initial.'\Memory\Available Bytes'.value));
$MemoryData.Add('Memory Total Bytes', (Get-CimInstance Win32_ComputerSystem).TotalPhysicalMemory); $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 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); $MemoryData.Add('PageFile %', $Initial.'\Paging File(_Total)\% usage'.value);
return $MemoryData; return $MemoryData;