From 990253f8ad0dbf757f4151151070e5bebb80bf52 Mon Sep 17 00:00:00 2001 From: Colin Gagnaire Date: Tue, 19 Mar 2019 15:51:43 +0100 Subject: [PATCH] handle negative values in change_bytes method --- centreon-plugins/centreon/plugins/perfdata.pm | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/centreon-plugins/centreon/plugins/perfdata.pm b/centreon-plugins/centreon/plugins/perfdata.pm index 7368b4844..fae3b9721 100644 --- a/centreon-plugins/centreon/plugins/perfdata.pm +++ b/centreon-plugins/centreon/plugins/perfdata.pm @@ -134,17 +134,23 @@ sub trim { sub change_bytes { my ($self, %options) = @_; + + my $value = $options{value}; my $divide = defined($options{network}) ? 1000 : 1024; my @units = ('K', 'M', 'G', 'T'); my $unit = ''; + my $sign = ''; + + $sign = '-' if ($value != abs($value)); + $value = abs($value); for (my $i = 0; $i < scalar(@units); $i++) { - last if (($options{value} / $divide) < 1); + last if (($value / $divide) < 1); $unit = $units[$i]; - $options{value} = $options{value} / $divide; + $value = $value / $divide; } - return (sprintf("%.2f", $options{value}), $unit . (defined($options{network}) ? 'b' : 'B')); + return (sprintf("%.2f", $sign . $value), $unit . (defined($options{network}) ? 'b' : 'B')); } 1;