2024-08-19 10:13:24 -04:00
<#
. SYNOPSIS
Converts an Icinga plugin value to a human-readable string .
. DESCRIPTION
The Convert-IcingaPluginValueToString function is used to convert an Icinga plugin value to a human-readable string . It supports various units and can handle percentage values .
. PARAMETER Value
The value to be converted .
. PARAMETER BaseValue
The base value used for percentage calculations .
. PARAMETER Unit
The unit of the value .
. PARAMETER OriginalUnit
The original unit of the value .
. PARAMETER UsePercent
Specifies whether to treat the value as a percentage .
. PARAMETER IsThreshold
Specifies whether the value is a threshold .
. OUTPUTS
System . String
Returns the converted value as a human-readable string .
. EXAMPLE
Convert-IcingaPluginValueToString -Value 1024 -Unit 'KiB'
Converts the value 1024 with the unit 'KiB' to a human-readable string .
. EXAMPLE
Convert-IcingaPluginValueToString -Value 50 -BaseValue 100 -UsePercent
Converts the value 50 as a percentage of the base value 100 to a human-readable string .
. NOTES
This function is part of the Icinga PowerShell Framework module .
#>
2021-05-07 08:38:10 -04:00
function Convert-IcingaPluginValueToString ( )
{
param (
2024-08-19 10:13:24 -04:00
$Value = $null ,
$BaseValue = $null ,
2021-05-07 08:38:10 -04:00
[ string ] $Unit = '' ,
2024-08-19 10:13:24 -04:00
[ string ] $OriginalUnit = '' ,
[ switch ] $UsePercent = $FALSE ,
[ switch ] $IsThreshold = $FALSE
2021-05-07 08:38:10 -04:00
) ;
2024-08-19 10:13:24 -04:00
$AdjustedValue = $Value ;
$PercentValue = $null ;
$HumanReadableValue = $null ;
2021-05-07 08:38:10 -04:00
if ( [ string ] :: IsNullOrEmpty ( $OriginalUnit ) ) {
$OriginalUnit = $Unit ;
}
try {
$AdjustedValue = ( [ math ] :: Round ( [ decimal ] $Value , 6 ) )
} catch {
$AdjustedValue = $Value ;
}
2024-08-19 10:13:24 -04:00
if ( $UsePercent -And ( $null -eq $BaseValue -Or $BaseValue -eq 0 ) ) {
2021-05-31 09:58:42 -04:00
return ( [ string ] :: Format ( '{0}{1}' , ( [ string ] $AdjustedValue ) . Replace ( ',' , '.' ) , $Unit ) ) ;
2024-08-19 10:13:24 -04:00
} elseif ( $UsePercent ) {
$Unit = $OriginalUnit ;
if ( $IsThreshold ) {
$PercentValue = [ math ] :: Round ( $Value , 2 ) ;
$AdjustedValue = [ math ] :: Round ( ( $BaseValue / 100 ) * $Value , 2 ) ;
} else {
$PercentValue = [ math ] :: Round ( ( $Value / $BaseValue ) * 100 , 2 ) ;
}
2021-05-07 08:38:10 -04:00
}
switch ( $OriginalUnit ) {
2021-05-29 08:25:06 -04:00
{ ( $_ -eq " Kbit " ) -or ( $_ -eq " Mbit " ) -or ( $_ -eq " Gbit " ) -or ( $_ -eq " Tbit " ) -or ( $_ -eq " Pbit " ) -or ( $_ -eq " Ebit " ) -or ( $_ -eq " Zbit " ) -or ( $_ -eq " Ybit " ) } {
2024-08-19 10:13:24 -04:00
$TransferSpeed = Get-IcingaNetworkInterfaceUnits -Value $AdjustedValue -Unit $Unit ;
$HumanReadableValue = ( [ string ] :: Format ( '{0}{1}' , $TransferSpeed . LinkSpeed , $TransferSpeed . Unit ) ) . Replace ( ',' , '.' ) ;
break ;
2021-05-29 08:25:06 -04:00
} ;
2021-05-07 08:38:10 -04:00
{ ( $_ -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 " ) } {
2024-08-19 10:13:24 -04:00
$HumanReadableValue = ( ConvertTo-BytesNextUnit -Value $AdjustedValue -Unit $Unit -Units @ ( 'B' , 'KiB' , 'MiB' , 'GiB' , 'TiB' , 'PiB' , 'EiB' , 'ZiB' , 'YiB' ) ) . Replace ( ',' , '.' ) ;
break ;
2021-05-07 08:38:10 -04:00
} ;
{ ( $_ -eq " KB " ) -or ( $_ -eq " MB " ) -or ( $_ -eq " GB " ) -or ( $_ -eq " TB " ) -or ( $_ -eq " PB " ) -or ( $_ -eq " EB " ) -or ( $_ -eq " ZB " ) -or ( $_ -eq " YB " ) } {
2024-08-19 10:13:24 -04:00
$HumanReadableValue = ( ConvertTo-BytesNextUnit -Value $AdjustedValue -Unit $Unit -Units @ ( 'B' , 'KB' , 'MB' , 'GB' , 'TB' , 'PB' , 'EB' , 'ZB' , 'YB' ) ) . Replace ( ',' , '.' ) ;
break ;
2021-05-07 08:38:10 -04:00
} ;
's' {
2024-08-19 10:13:24 -04:00
$HumanReadableValue = ( ConvertFrom-TimeSpan -Seconds $AdjustedValue ) . Replace ( ',' , '.' ) ;
break ;
2021-05-07 08:38:10 -04:00
} ;
}
2024-08-19 10:13:24 -04:00
if ( $null -eq $HumanReadableValue ) {
$HumanReadableValue = ( [ string ] :: Format ( '{0}{1}' , ( [ string ] $AdjustedValue ) . Replace ( ',' , '.' ) , $Unit ) ) ;
}
# In case the value provided is a percentage value, we need to adjust the output so it doesn't make sense to add the percentage value again for this case
if ( $UsePercent -And $Unit -ne '%' ) {
$HumanReadableValue = [ string ] :: Format ( '{0} ({1}%)' , $HumanReadableValue , $PercentValue ) ;
}
return $HumanReadableValue ;
2021-05-07 08:38:10 -04:00
}