Hi, I was looking for a simple program to display my bandwidth usage. I tried many c programs, nload, nethogs, etc, but to my dismay they often jumped to 100% cpu usage, and more often than not, they needed root priviledges to run. Ugh. So I found a bash shell script which is floating around on the search engines which did the trick. The problem with it, was that it was a scrolling display in an xterm, and I always had to set the Window Manager option on the xterm to "Stay on Top". It was a hassle plus it didn't look sweet. So I put the basic idea from the shell script into a Tk script, and I have something useful enough to post here. :-)

You set your interface to watch on the command line, as first argument, or it defaults to eth0. I placed it just above the lower right corner to stay out of most things way. Once the display is started, a left mouse button click on it kills it.

It also has a non-blocking sleep (thx to Slaven Reszic) that you might find useful in other Tk scripts.

#!/usr/bin/perl use warnings; use strict; use Tk; #specify interface on commandline or here my $iface = shift || 'eth0'; #correction my $mw = new MainWindow; # I have my toolbar at the top, so # I like my info boxes at the bottom $mw->geometry('-50-50'); $mw->overrideredirect(1); $mw->configure(-cursor => 'pirate'); #:-) $mw->fontCreate('big', -family=>'courier', -weight=>'bold', -size=> 18); my $bw = $mw->Label(-text=>' ', -font=>'big', -bg=>'black', -fg=>'yellow')->pack (-side=>'left',-fill =>'both'); # left click exits program $mw->bind('<1>' => sub{exit}); #refresh every 1.5 seconds my $id = Tk::After->new($mw,1500,'repeat',\&refresh); MainLoop; sub refresh{ my $r0 = `cat /sys/class/net/$iface/statistics/rx_bytes`; my $t0 = `cat /sys/class/net/$iface/statistics/tx_bytes`; tksleep($mw, 1000); my $r1 = `cat /sys/class/net/$iface/statistics/rx_bytes`; my $t1 = `cat /sys/class/net/$iface/statistics/tx_bytes`; my $rr = sprintf ("%03d",($r1 - $r0)/1024); my $tr = sprintf ("%03d",($t1 - $t0)/1024); $bw->configure(-text=>"Rx: $rr kBs || Tx: $tr kBs"); } sub tksleep { # Like sleep, but actually allows the display to be # updated, and takes milliseconds instead of seconds. # A non-blocking sleep for the eventloop my $mw = shift; my $ms = shift; my $flag = 0; $mw->after($ms, sub { $flag++ }); $mw->waitVariable(\$flag); }

I'm not really a human, but I play one on earth. ..... an animated JAPH

In reply to Tk Bandwidth use indicator by zentara

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.