[pve-devel] applied: [PATCH manager v3] fix #2030: use looks_like_number for number check

Wolfgang Bumiller w.bumiller at proxmox.com
Mon Jan 7 15:02:16 CET 2019


applied

On Mon, Jan 07, 2019 at 01:59:47PM +0100, Dominik Csapak wrote:
> since numbers can also be in '1.e-10' format, we have to change
> how we check for a number
> 
> Scalar::Util is already core and we use it in PVE::Tools, so
> no new dependecy.
> 
> in case of "NaN" or "Infinity" we omit the key/value pair
> 
> else we quote like before
> 
> Signed-off-by: Dominik Csapak <d.csapak at proxmox.com>
> ---
> changes from v2:
> * use POSIX::isnan/isinf instead of using string comparison/regex
> 
>  PVE/Status/InfluxDB.pm | 28 ++++++++++++++++++++--------
>  1 file changed, 20 insertions(+), 8 deletions(-)
> 
> diff --git a/PVE/Status/InfluxDB.pm b/PVE/Status/InfluxDB.pm
> index 7364e572..d119becb 100644
> --- a/PVE/Status/InfluxDB.pm
> +++ b/PVE/Status/InfluxDB.pm
> @@ -5,6 +5,8 @@ use warnings;
>  use PVE::Status::Plugin;
>  use Data::Dumper;
>  use PVE::SafeSyslog;
> +use POSIX qw(isnan isinf);
> +use Scalar::Util 'looks_like_number';
>  
>  # example config (/etc/pve/status.cfg)
>  #influxdb:
> @@ -111,8 +113,9 @@ sub build_influxdb_payload {
>  	if (!ref($value) && $value ne '') {
>  	    # value is scalar
>  
> -	    $value = prepare_value($value);
> -	    push @values, "$key=$value";
> +	    if (defined(my $v = prepare_value($value))) {
> +		push @values, "$key=$v";
> +	    }
>  	} elsif (ref($value) eq 'HASH') {
>  	    # value is a hash
>  
> @@ -145,8 +148,9 @@ sub get_recursive_values {
>  	if(ref($value) eq 'HASH') {
>  	    push(@values, get_recursive_values($value));
>  	} elsif (!ref($value) && $value ne '') {
> -	    $value = prepare_value($value);
> -	    push @values, "$key=$value";
> +	    if (defined(my $v = prepare_value($value))) {
> +		push @values, "$key=$v";
> +	    }
>  	}
>      }
>  
> @@ -156,13 +160,21 @@ sub get_recursive_values {
>  sub prepare_value {
>      my ($value) = @_;
>  
> +    if (looks_like_number($value)) {
> +	if (isnan($value) || isinf($value)) {
> +	    # we cannot send influxdb NaN or Inf
> +	    return undef;
> +	}
> +
> +	# influxdb also accepts 1.0e+10, etc.
> +	return $value;
> +    }
> +
>      # if value is not just a number we
>      # have to replace " with \"
>      # and surround it with "
> -    if ($value =~ m/[^\d\.]/) {
> -	$value =~ s/\"/\\\"/g;
> -	$value = "\"$value\"";
> -    }
> +    $value =~ s/\"/\\\"/g;
> +    $value = "\"$value\"";
>  
>      return $value;
>  }
> -- 
> 2.11.0




More information about the pve-devel mailing list