Skip to content

Commit

Permalink
Fixes wrong regex for threshold detection
Browse files Browse the repository at this point in the history
  • Loading branch information
LordHepipud committed Dec 19, 2022
1 parent a7dcbe8 commit 69ced2f
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 8 deletions.
1 change: 1 addition & 0 deletions doc/100-General/10-Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Released closed milestones can be found on [GitHub](https://github.com/Icinga/ic
### 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
* [#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)

Expand Down
6 changes: 3 additions & 3 deletions lib/core/tools/Convert-Bytes.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ function Convert-Bytes()
# Ensure we always use proper formatting of values
$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);
}

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];
[string]$CurrentUnit = $Matches[2];
[string]$CurrentUnit = $Matches[4];

switch ($CurrentUnit) {
{ 'KiB', 'MiB', 'GiB', 'TiB', 'PiB' -contains $_ } { $CurrentValue = ConvertTo-ByteIEC $CurrentValue $CurrentUnit; $boolOption = $true; }
Expand Down
9 changes: 6 additions & 3 deletions lib/core/tools/Convert-IcingaPluginThresholds.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,14 @@ function Convert-IcingaPluginThresholds()
$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';
if ([string]::IsNullOrEmpty($RetValue.Unit) -eq $FALSE -And $RetValue.Unit -ne $WorkUnit) {
Exit-IcingaThrowException -ExceptionType 'Input' -ExceptionThrown $IcingaExceptions.Inputs.MultipleUnitUsage -Force;
}
$Value = (Convert-Bytes -Value $ThresholdValue -Unit $WorkUnit).Value;
$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';
if ([string]::IsNullOrEmpty($RetValue.Unit) -eq $FALSE -And $RetValue.Unit -ne $WorkUnit) {
Exit-IcingaThrowException -ExceptionType 'Input' -ExceptionThrown $IcingaExceptions.Inputs.MultipleUnitUsage -Force;
Expand All @@ -152,6 +152,9 @@ function Convert-IcingaPluginThresholds()
$WorkUnit = '%';
$Value = ([string]$ThresholdValue).Replace(' ', '').Replace('%', '');
$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 {
# Load all other units/values generically
[string]$StrNumeric = '';
Expand Down Expand Up @@ -206,7 +209,7 @@ function Convert-IcingaPluginThresholds()
}

# Always ensure we are using correct digits
$Value = ([string]$Value).Replace(',', '.');
$Value = ([string]$Value).Replace(',', '.');
$RetValue.Value = $Value;

return $RetValue;
Expand Down
2 changes: 1 addition & 1 deletion lib/core/tools/Convert-IcingaPluginValueToString.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function Convert-IcingaPluginValueToString()

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") } {
$TransferSpeed = Get-IcingaNetworkInterfaceUnits -Value $Value;
$TransferSpeed = Get-IcingaNetworkInterfaceUnits -Value $Value -Unit $Unit;
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") } {
Expand Down
10 changes: 9 additions & 1 deletion lib/core/tools/Get-IcingaNetworkInterfaceUnits.psm1
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
function Get-IcingaNetworkInterfaceUnits()
{
param (
[long]$Value
[decimal]$Value = 0,
[string]$Unit = ''
);

[hashtable]$InterfaceData = @{
Expand All @@ -10,6 +11,13 @@ function Get-IcingaNetworkInterfaceUnits()
'Unit' = 'Mbit'
};

if ([string]::IsNullOrEmpty($Unit) -eq $FALSE) {
$InterfaceData.LinkSpeed = $Value;
$InterfaceData.Unit = $Unit;

return $InterfaceData;
}

[decimal]$result = ($Value / [Math]::Pow(10, 6));

if ($result -ge 1000) {
Expand Down

0 comments on commit 69ced2f

Please sign in to comment.