From 86cc37353146790862cdf271dbc7d2b43fb76a94 Mon Sep 17 00:00:00 2001 From: garnier-quentin Date: Mon, 28 Apr 2014 20:40:19 +0200 Subject: [PATCH] + Optimize change_bytes method --- centreon/plugins/perfdata.pm | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/centreon/plugins/perfdata.pm b/centreon/plugins/perfdata.pm index 3b832d358..8d3026a1d 100644 --- a/centreon/plugins/perfdata.pm +++ b/centreon/plugins/perfdata.pm @@ -215,23 +215,17 @@ sub parse_threshold { sub change_bytes { my ($self, %options) = @_; - - my $unit = defined($options{network}) ? 'b' : 'B'; my $divide = defined($options{network}) ? 1000 : 1024; + my @units = ('K', 'M', 'G', 'T'); + my $unit = ''; - if (($options{value} / $divide) >= 1) { + for (my $i = 0; $i < scalar(@units); $i++) { + last if (($options{value} / $divide) < 1); + $unit = $units[$i]; $options{value} = $options{value} / $divide; - $unit = defined($options{network}) ? 'Kb' : 'KB'; } - if (($options{value} / $divide) >= 1) { - $options{value} = $options{value} / $divide; - $unit = defined($options{network}) ? 'Mb' : 'MB'; - } - if (($options{value} / $divide) >= 1) { - $options{value} = $options{value} / $divide; - $unit = defined($options{network}) ? 'Gb' : 'GB'; - } - return (sprintf("%.2f", $options{value}), $unit); + + return (sprintf("%.2f", $options{value}), $unit . (defined($options{network}) ? 'b' : 'B')); } 1;