Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Tk Bandwidth use indicator

by zentara (Archbishop)
on Jun 29, 2018 at 17:45 UTC ( [id://1217622]=CUFP: print w/replies, xml ) Need Help??

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

Replies are listed 'Best First'.
Re: Tk Bandwidth use indicator
by jwkrahn (Abbot) on Jun 29, 2018 at 18:05 UTC
    #specify interface on commandline or here my $iface = $1 || 'eth0';

    $1 is only valid after a successful regular expression match with capturing parentheses.

    That should be:

    #specify interface on commandline or here my $iface = shift || 'eth0';

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://1217622]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2024-04-16 17:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found