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);
}
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.