Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

measuring IN/OUT traffic on your computer

by spx2 (Deacon)
on Aug 29, 2007 at 13:04 UTC ( #635792=CUFP: print w/replies, xml ) Need Help??

sometimes you need to see how much data your computer gets through a certain device(at least I do). I've put together this script that does just that

use strict; use warnings; sub get_measures { my $data = `/sbin/ifconfig | grep “RX bytes” | head -1`; $data =~ /RX bytes\:(\d+) .*TX bytes\:(\d+) .*/; my $recv = $1/1024; my $sent=$2/1024; $recv =~ s/\..*//; $sent =~ s/\..*//; #printf “received:%s KB transmitted:%s KB”,$recv,$sent;$_=”"; return ($recv,$sent); } my @m1 = get_measures; sleep 1; my @m2 = get_measures; my @rates = ($m2[0] - $m1[0], $m2[1]-$m1[1]); foreach (’ received’ , ‘ transmit’) { printf “$_ rate:%sKB”,shift @rates; } no comments yet

a slightly modified version,for making a live display of the transfered data chart using as points the transfer rates,and on the other axis time.

Replies are listed 'Best First'.
Re: measuring IN/OUT traffic on your computer
by andreas1234567 (Vicar) on Aug 29, 2007 at 15:03 UTC
Re: measuring IN/OUT traffic on your computer
by jwkrahn (Monsignor) on Aug 30, 2007 at 00:37 UTC
    se strict;

    Produces the error message:

    Can't locate object method "se" via package "strict" (perhaps you forg +ot to load "strict"?)
    my $data = `/sbin/ifconfig | grep “RX bytes” | head -1`;

    I guess you are assuming that the "certain device" you want information on is the first device listed by ifconfig?

    $data =~ /RX bytes\:(\d+) .*TX bytes\:(\d+) .*/; my $recv = $1/1024; my $sent=$2/1024; $recv =~ s/\..*//; $sent =~ s/\..*//;

    You shouldn't use the variables $1 and $2 unless the regular expression matched correctly otherwise the values in $1 and $2 will be from the last successful match. Is there any reason that you didn't use the int function instead of the substitution operator?

    You probably want something like this:

    sub get_measures { my $device = qr/\A\Q$_[0]\E\s/; # pass device name as first argum +ent local $/ = ''; # paragraph mode my ( $recv, $sent ) = map { /$device/ && /RX bytes\:(\d+) .*?TX bytes\:(\d+)/ ? int( $1 / 1024 ), int( $2 / 1024 ) : () } qx[/sbin/ifconfig]; return $recv, $sent; }
Re: measuring IN/OUT traffic on your computer
by goibhniu (Hermit) on Aug 29, 2007 at 14:19 UTC

    I like it. Perhaps on Windows, ifconfig could be replaced with netstat -e, though I don't know what that would look like if there were multiple interfaces or how difficult it would be to parse. i'm sure with Perl it wouldn't be THAT difficult.

    I humbly seek wisdom.
Re: measuring IN/OUT traffic on your computer
by bruceb3 (Pilgrim) on Aug 29, 2007 at 21:00 UTC
    If you happen to be using Solaris, there is a Perl script that will display the inbound and outbound usage per network interface here.
Re: measuring IN/OUT traffic on your computer
by ambrus (Abbot) on Aug 30, 2007 at 21:18 UTC

    I wrote something similar to this once. Here's the quick one-liner from my bash_history. I'm not sure if it measures the incoming or outgoing traffic on the eth0 device, because it was just a quick one-liner which I don't usually document or even save. It's using the linux proc interface instead of executing ifconfig.

    ruby -we '@s ="/proc/net/dev"); @pq = (); while sleep 20; @ +s.rewind; cq = @s.readlines.find{|l|l=~/^\s*eth0/}.scan(/[^\W:]+/)[9] +.to_i; ct =; @pq and printf "%.2f byte/sec\n", (cq-@pq)/Floa +t(ct-@pt); @pq, @pt = cq, ct; end;'

    By the way, you could also get more detailed statistics by setting up iptables rules because there's a packet and byte counter for each rule.

      very nice and small

      but does it do graphs with the rates in them ? :P

        That's easy too. Firstly change the print statement so it would print the time (in epoch seconds) and the rate separated by spaces. Write that output to a file, and then graph that file with gnuplot.

        I actually have a perl program that draws a graph by calling gnuplot, here are a few parts of its code (I did it as a work so I don't want to publish all of it). The part not shown here opens a tempfile and writes into it the numbers in the simple format mentioned above. I also print some overall statistics to stdout. The following sub graphs the data then (it might need some adaptation for your purposes).

        use File::Temp (); sub showgraph { my($cmdfile, $cmdfilename); my $cmds = qq[set key off\nplot "] . quote($outname) . qq[" wi +th lines\n! echo "Press return to continue" ; read\n]; ($cmdfile, $cmdfilename) = File::Temp::tempfile undef, "UNLINK +", 1; $out or die "error creating temporary commands file"; print "Writing gnuplot command to temporary file $cmdfilename\ +n"; print $cmdfile $cmds or die "error writing command file: $!"; flush $cmdfile or die "error flushing comand file"; print "Launching gnuplot\n"; system "gnuplot", $cmdfilename; close $cmdfile; }

        Update 2008-01-29: see Plot a spiral with gnuplot.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: CUFP [id://635792]
Approved by ikegami
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (8)
As of 2021-06-24 15:44 GMT
Find Nodes?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)

    Results (129 votes). Check out past polls.