#!/usr/bin/perl use strict; my $filename = "/proc/net/dev"; sub file_to_string ($) { my $filename = shift; local $/; undef $/; open FILE, "< $filename" or return undef; my $string = ; $string = "" if ( not defined $string ); close FILE; return $string; } sub get_samp { my $stuff = file_to_string $filename; $stuff =~ /^\s*eth0:\s*(.*?)$/m or die "could not parse stuff ($stuff)"; my @info = split(/\s+/, $1); return ($info[0], $info[8]); } my $sec = 5; $sec = shift if ( $ARGV[0] =~ /^\d+$/ ); print "Sample period: $sec seconds\n"; my @this_samp; my @last_samp; @this_samp = get_samp(); while (1) { sleep $sec; @last_samp = @this_samp; @this_samp = get_samp(); my $recei_bytes = $this_samp[0] - $last_samp[0]; # unit: bytes my $trans_bytes = $this_samp[1] - $last_samp[1]; # unit: bytes my $recei_rate = $recei_bytes / $sec * (8/(1024)); # unit: k bits / sec my $trans_rate = $trans_bytes / $sec * (8/(1024)); # unit: k bits / sec print sprintf( "Sec: %2d Receive: %8.1f kbytes, %8.2f kbits/sec Trans: %8.1f kbytes, %8.2f kbits/sec\n", $sec, $recei_bytes/1024, $recei_rate, $trans_bytes/1024, $trans_rate, ); }