|
From: Buchan M. <bg...@st...> - 2011-06-20 12:24:57
|
On Tuesday, 14 June 2011 13:52:06 Olivier AUDRY wrote:
> hello
>
> someone in my compagnie need to get the volume of traffic on interfaces. As
> far as I know the only oid I have it the IfInOctets and IfOutOctets.
> That's good but reading a volume in octets is not really human readable.
> Some solution exist with devmon ? May I implement a need transform method
> like HUMAN for exemple to do this ? Following :
I don't know if the name of the transform is the best choice. I was also
wondering if it would be better to have this transform take an argument, such
as the precision.
>
> sub octets_to_human
> 2 {
> 4
> 5 my $size = shift;
> 6
> 7 if ($size > 1099511627776) # TiB: 1024 GiB
> 8 {
> 9 return sprintf("%.2f TiB", $size / 1099511627776);
> 10 }
> 11 elsif ($size > 1073741824) # GiB: 1024 MiB
> 12 {
> 13 return sprintf("%.2f GiB", $size / 1073741824);
> 14 }
> 15 elsif ($size > 1048576) # MiB: 1024 KiB
> 16 {
> 17 return sprintf("%.2f MiB", $size / 1048576);
> 18 }
> 19 elsif ($size > 1024) # KiB: 1024 B
> 20 {
> 21 return sprintf("%.2f KiB", $size / 1024);
> 22 }
> 23 else # bytes
> 24 {
> 25 return sprintf("%.2f bytes", $size);
> 26 }
> 27 }
>
> what do you think ?
How about:
sub trans_best_binary_prefix {
my $size = shift;
my $precision = shift;
my @units = qw/B KiB MiB GiB TiB/;
my $i;
for ( $i = 0; $i < @units -1; $i++) {
last if (2**(10*($i+1)) > $size)
}
return sprintf("%.${precision}f ${units[$i]}",$size/(2**(10*$i)) );
}
Regards,
Buchan
|