Fixes wrong regex for threshold detection

This commit is contained in:
Lord Hepipud 2022-10-18 10:01:22 +02:00
parent 9f91290e23
commit b1ee7bc30a
5 changed files with 20 additions and 8 deletions

View file

@ -14,6 +14,7 @@ Released closed milestones can be found on [GitHub](https://github.com/Icinga/ic
### Bugfixes ### Bugfixes
* [#582](https://github.com/Icinga/icinga-powershell-framework/issues/582) Fixes background service registration caused by migration task for v1.10.0 being executed even when no services were defined before * [#582](https://github.com/Icinga/icinga-powershell-framework/issues/582) Fixes background service registration caused by migration task for v1.10.0 being executed even when no services were defined before
* [#588](https://github.com/Icinga/icinga-powershell-framework/issues/588) Fixes threshold values causing an error because of too aggressive regex expression
## 1.10.0 (2022-08-30) ## 1.10.0 (2022-08-30)

View file

@ -8,13 +8,13 @@ function Convert-Bytes()
# Ensure we always use proper formatting of values # Ensure we always use proper formatting of values
$Value = $Value.Replace(',', '.'); $Value = $Value.Replace(',', '.');
If (($Value -Match "(^-?[0-9].*)+((\.|\,)+[0-9])?(B|KB|MB|GB|TB|PT|KiB|MiB|GiB|TiB|PiB)") -eq $FALSE) { If (($Value -Match "(^-?[0-9]+)((\.|\,)[0-9]+)?(B|KB|MB|GB|TB|PT|KiB|MiB|GiB|TiB|PiB)$") -eq $FALSE) {
$Value = [string]::Format('{0}B', $Value); $Value = [string]::Format('{0}B', $Value);
} }
If (($Value -Match "(^[\d\.]*) ?(B|KB|MB|GB|TB|PT|KiB|MiB|GiB|TiB|PiB)")) { If (($Value -Match "(^-?[0-9]+)((\.|\,)[0-9]+)?(B|KB|MB|GB|TB|PT|KiB|MiB|GiB|TiB|PiB)$")) {
[single]$CurrentValue = $Matches[1]; [single]$CurrentValue = $Matches[1];
[string]$CurrentUnit = $Matches[2]; [string]$CurrentUnit = $Matches[4];
switch ($CurrentUnit) { switch ($CurrentUnit) {
{ 'KiB', 'MiB', 'GiB', 'TiB', 'PiB' -contains $_ } { $CurrentValue = ConvertTo-ByteIEC $CurrentValue $CurrentUnit; $boolOption = $true; } { 'KiB', 'MiB', 'GiB', 'TiB', 'PiB' -contains $_ } { $CurrentValue = ConvertTo-ByteIEC $CurrentValue $CurrentUnit; $boolOption = $true; }

View file

@ -134,14 +134,14 @@ function Convert-IcingaPluginThresholds()
$ThresholdValue = $ThresholdValue.Substring(1, $ThresholdValue.Length - 1); $ThresholdValue = $ThresholdValue.Substring(1, $ThresholdValue.Length - 1);
} }
If (($ThresholdValue -Match "(^-?[0-9].*)+((\.|\,)+[0-9])?(B|KB|MB|GB|TB|PT|KiB|MiB|GiB|TiB|PiB)")) { if (($ThresholdValue -Match "(^-?[0-9]+)((\.|\,)[0-9]+)?(B|KB|MB|GB|TB|PT|KiB|MiB|GiB|TiB|PiB)$")) {
$WorkUnit = 'B'; $WorkUnit = 'B';
if ([string]::IsNullOrEmpty($RetValue.Unit) -eq $FALSE -And $RetValue.Unit -ne $WorkUnit) { if ([string]::IsNullOrEmpty($RetValue.Unit) -eq $FALSE -And $RetValue.Unit -ne $WorkUnit) {
Exit-IcingaThrowException -ExceptionType 'Input' -ExceptionThrown $IcingaExceptions.Inputs.MultipleUnitUsage -Force; Exit-IcingaThrowException -ExceptionType 'Input' -ExceptionThrown $IcingaExceptions.Inputs.MultipleUnitUsage -Force;
} }
$Value = (Convert-Bytes -Value $ThresholdValue -Unit $WorkUnit).Value; $Value = (Convert-Bytes -Value $ThresholdValue -Unit $WorkUnit).Value;
$RetValue.Unit = $WorkUnit; $RetValue.Unit = $WorkUnit;
} elseif (($ThresholdValue -Match "(^-?[0-9].*)+((\.|\,)+[0-9])?(ms|s|m|h|d|w|M|y)")) { } elseif (($ThresholdValue -Match "(^-?[0-9]+)((\.|\,)[0-9]+)?(ms|s|m|h|d|w|M|y)$")) {
$WorkUnit = 's'; $WorkUnit = 's';
if ([string]::IsNullOrEmpty($RetValue.Unit) -eq $FALSE -And $RetValue.Unit -ne $WorkUnit) { if ([string]::IsNullOrEmpty($RetValue.Unit) -eq $FALSE -And $RetValue.Unit -ne $WorkUnit) {
Exit-IcingaThrowException -ExceptionType 'Input' -ExceptionThrown $IcingaExceptions.Inputs.MultipleUnitUsage -Force; Exit-IcingaThrowException -ExceptionType 'Input' -ExceptionThrown $IcingaExceptions.Inputs.MultipleUnitUsage -Force;
@ -152,6 +152,9 @@ function Convert-IcingaPluginThresholds()
$WorkUnit = '%'; $WorkUnit = '%';
$Value = ([string]$ThresholdValue).Replace(' ', '').Replace('%', ''); $Value = ([string]$ThresholdValue).Replace(' ', '').Replace('%', '');
$RetValue.Unit = $WorkUnit; $RetValue.Unit = $WorkUnit;
} elseif (($ThresholdValue -Match "(^-?[0-9]+)((\.|\,)[0-9]+)?(Kbit|Mbit|Gbit|Tbit|Pbit|Ebit|Zbit|Ybit)$")) {
$Value = $Matches[1];
$RetValue.Unit = $Matches[4];
} else { } else {
# Load all other units/values generically # Load all other units/values generically
[string]$StrNumeric = ''; [string]$StrNumeric = '';
@ -206,7 +209,7 @@ function Convert-IcingaPluginThresholds()
} }
# Always ensure we are using correct digits # Always ensure we are using correct digits
$Value = ([string]$Value).Replace(',', '.'); $Value = ([string]$Value).Replace(',', '.');
$RetValue.Value = $Value; $RetValue.Value = $Value;
return $RetValue; return $RetValue;

View file

@ -24,7 +24,7 @@ function Convert-IcingaPluginValueToString()
switch ($OriginalUnit) { switch ($OriginalUnit) {
{ ($_ -eq "Kbit") -or ($_ -eq "Mbit") -or ($_ -eq "Gbit") -or ($_ -eq "Tbit") -or ($_ -eq "Pbit") -or ($_ -eq "Ebit") -or ($_ -eq "Zbit") -or ($_ -eq "Ybit") } { { ($_ -eq "Kbit") -or ($_ -eq "Mbit") -or ($_ -eq "Gbit") -or ($_ -eq "Tbit") -or ($_ -eq "Pbit") -or ($_ -eq "Ebit") -or ($_ -eq "Zbit") -or ($_ -eq "Ybit") } {
$TransferSpeed = Get-IcingaNetworkInterfaceUnits -Value $Value; $TransferSpeed = Get-IcingaNetworkInterfaceUnits -Value $Value -Unit $Unit;
return ([string]::Format('{0}{1}', $TransferSpeed.LinkSpeed, $TransferSpeed.Unit)).Replace(',', '.'); return ([string]::Format('{0}{1}', $TransferSpeed.LinkSpeed, $TransferSpeed.Unit)).Replace(',', '.');
}; };
{ ($_ -eq "B") -or ($_ -eq "KiB") -or ($_ -eq "MiB") -or ($_ -eq "GiB") -or ($_ -eq "TiB") -or ($_ -eq "PiB") -or ($_ -eq "EiB") -or ($_ -eq "ZiB") -or ($_ -eq "YiB") } { { ($_ -eq "B") -or ($_ -eq "KiB") -or ($_ -eq "MiB") -or ($_ -eq "GiB") -or ($_ -eq "TiB") -or ($_ -eq "PiB") -or ($_ -eq "EiB") -or ($_ -eq "ZiB") -or ($_ -eq "YiB") } {

View file

@ -1,7 +1,8 @@
function Get-IcingaNetworkInterfaceUnits() function Get-IcingaNetworkInterfaceUnits()
{ {
param ( param (
[long]$Value [decimal]$Value = 0,
[string]$Unit = ''
); );
[hashtable]$InterfaceData = @{ [hashtable]$InterfaceData = @{
@ -10,6 +11,13 @@ function Get-IcingaNetworkInterfaceUnits()
'Unit' = 'Mbit' 'Unit' = 'Mbit'
}; };
if ([string]::IsNullOrEmpty($Unit) -eq $FALSE) {
$InterfaceData.LinkSpeed = $Value;
$InterfaceData.Unit = $Unit;
return $InterfaceData;
}
[decimal]$result = ($Value / [Math]::Pow(10, 6)); [decimal]$result = ($Value / [Math]::Pow(10, 6));
if ($result -ge 1000) { if ($result -ge 1000) {