Skip to content

Commit 69ced2f

Browse files
committed
Fixes wrong regex for threshold detection
1 parent a7dcbe8 commit 69ced2f

File tree

5 files changed

+20
-8
lines changed

5 files changed

+20
-8
lines changed

doc/100-General/10-Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Released closed milestones can be found on [GitHub](https://github.com/Icinga/ic
1414
### Bugfixes
1515

1616
* [#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
17+
* [#588](https://github.com/Icinga/icinga-powershell-framework/issues/588) Fixes threshold values causing an error because of too aggressive regex expression
1718

1819
## 1.10.0 (2022-08-30)
1920

lib/core/tools/Convert-Bytes.psm1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ function Convert-Bytes()
88
# Ensure we always use proper formatting of values
99
$Value = $Value.Replace(',', '.');
1010

11-
If (($Value -Match "(^-?[0-9].*)+((\.|\,)+[0-9])?(B|KB|MB|GB|TB|PT|KiB|MiB|GiB|TiB|PiB)") -eq $FALSE) {
11+
If (($Value -Match "(^-?[0-9]+)((\.|\,)[0-9]+)?(B|KB|MB|GB|TB|PT|KiB|MiB|GiB|TiB|PiB)$") -eq $FALSE) {
1212
$Value = [string]::Format('{0}B', $Value);
1313
}
1414

15-
If (($Value -Match "(^[\d\.]*) ?(B|KB|MB|GB|TB|PT|KiB|MiB|GiB|TiB|PiB)")) {
15+
If (($Value -Match "(^-?[0-9]+)((\.|\,)[0-9]+)?(B|KB|MB|GB|TB|PT|KiB|MiB|GiB|TiB|PiB)$")) {
1616
[single]$CurrentValue = $Matches[1];
17-
[string]$CurrentUnit = $Matches[2];
17+
[string]$CurrentUnit = $Matches[4];
1818

1919
switch ($CurrentUnit) {
2020
{ 'KiB', 'MiB', 'GiB', 'TiB', 'PiB' -contains $_ } { $CurrentValue = ConvertTo-ByteIEC $CurrentValue $CurrentUnit; $boolOption = $true; }

lib/core/tools/Convert-IcingaPluginThresholds.psm1

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,14 +134,14 @@ function Convert-IcingaPluginThresholds()
134134
$ThresholdValue = $ThresholdValue.Substring(1, $ThresholdValue.Length - 1);
135135
}
136136

137-
If (($ThresholdValue -Match "(^-?[0-9].*)+((\.|\,)+[0-9])?(B|KB|MB|GB|TB|PT|KiB|MiB|GiB|TiB|PiB)")) {
137+
if (($ThresholdValue -Match "(^-?[0-9]+)((\.|\,)[0-9]+)?(B|KB|MB|GB|TB|PT|KiB|MiB|GiB|TiB|PiB)$")) {
138138
$WorkUnit = 'B';
139139
if ([string]::IsNullOrEmpty($RetValue.Unit) -eq $FALSE -And $RetValue.Unit -ne $WorkUnit) {
140140
Exit-IcingaThrowException -ExceptionType 'Input' -ExceptionThrown $IcingaExceptions.Inputs.MultipleUnitUsage -Force;
141141
}
142142
$Value = (Convert-Bytes -Value $ThresholdValue -Unit $WorkUnit).Value;
143143
$RetValue.Unit = $WorkUnit;
144-
} elseif (($ThresholdValue -Match "(^-?[0-9].*)+((\.|\,)+[0-9])?(ms|s|m|h|d|w|M|y)")) {
144+
} elseif (($ThresholdValue -Match "(^-?[0-9]+)((\.|\,)[0-9]+)?(ms|s|m|h|d|w|M|y)$")) {
145145
$WorkUnit = 's';
146146
if ([string]::IsNullOrEmpty($RetValue.Unit) -eq $FALSE -And $RetValue.Unit -ne $WorkUnit) {
147147
Exit-IcingaThrowException -ExceptionType 'Input' -ExceptionThrown $IcingaExceptions.Inputs.MultipleUnitUsage -Force;
@@ -152,6 +152,9 @@ function Convert-IcingaPluginThresholds()
152152
$WorkUnit = '%';
153153
$Value = ([string]$ThresholdValue).Replace(' ', '').Replace('%', '');
154154
$RetValue.Unit = $WorkUnit;
155+
} elseif (($ThresholdValue -Match "(^-?[0-9]+)((\.|\,)[0-9]+)?(Kbit|Mbit|Gbit|Tbit|Pbit|Ebit|Zbit|Ybit)$")) {
156+
$Value = $Matches[1];
157+
$RetValue.Unit = $Matches[4];
155158
} else {
156159
# Load all other units/values generically
157160
[string]$StrNumeric = '';
@@ -206,7 +209,7 @@ function Convert-IcingaPluginThresholds()
206209
}
207210

208211
# Always ensure we are using correct digits
209-
$Value = ([string]$Value).Replace(',', '.');
212+
$Value = ([string]$Value).Replace(',', '.');
210213
$RetValue.Value = $Value;
211214

212215
return $RetValue;

lib/core/tools/Convert-IcingaPluginValueToString.psm1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function Convert-IcingaPluginValueToString()
2424

2525
switch ($OriginalUnit) {
2626
{ ($_ -eq "Kbit") -or ($_ -eq "Mbit") -or ($_ -eq "Gbit") -or ($_ -eq "Tbit") -or ($_ -eq "Pbit") -or ($_ -eq "Ebit") -or ($_ -eq "Zbit") -or ($_ -eq "Ybit") } {
27-
$TransferSpeed = Get-IcingaNetworkInterfaceUnits -Value $Value;
27+
$TransferSpeed = Get-IcingaNetworkInterfaceUnits -Value $Value -Unit $Unit;
2828
return ([string]::Format('{0}{1}', $TransferSpeed.LinkSpeed, $TransferSpeed.Unit)).Replace(',', '.');
2929
};
3030
{ ($_ -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") } {

lib/core/tools/Get-IcingaNetworkInterfaceUnits.psm1

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
function Get-IcingaNetworkInterfaceUnits()
22
{
33
param (
4-
[long]$Value
4+
[decimal]$Value = 0,
5+
[string]$Unit = ''
56
);
67

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

14+
if ([string]::IsNullOrEmpty($Unit) -eq $FALSE) {
15+
$InterfaceData.LinkSpeed = $Value;
16+
$InterfaceData.Unit = $Unit;
17+
18+
return $InterfaceData;
19+
}
20+
1321
[decimal]$result = ($Value / [Math]::Pow(10, 6));
1422

1523
if ($result -ge 1000) {

0 commit comments

Comments
 (0)