Output looks like this:
Code:
As of Wed Jan 24 04:45:59 2007 you are currently UNDER your set limit (80%).
Input Bytes Xfer: 0.84 GiBs Host: host29.linode.com
Output Bytes Xfer: 2.62 GiBs Load: IDLE
------------------------
Total Bytes Xfer: 3.46 GiBs of 200.00 available.
Recommended Installation Method:
Add this to your $HOME/.bash_profile
Code:
[ -f $HOME/bin/bw.pl ] && $HOME/bin/bw.pl
Code:
Code:
#!/usr/bin/perl
# $Id: bw.pl,v 1.11 2007/01/24 04:40:51 jason Exp $
########################################################################
# bw.pl - Linode Bandwidth Utilization^W^H^Winfo Monitor via shell
#
# Revision History:
# 1.7 - First Public Release
# 1.8 - some lame fixes, such as 0byte $myLastState
# 1.11 - updated URL to /info/, added host level info
#
# To setup, modify $username to match your LPM login (Web Login)
#
# NOTES: Please do not modify $userAgent or $maxTime as this is used
# on linode server to monitor usage.
#
# If you desire to graph your bandwidth usage, use your your
# local interface stats.
########################################################################
# Installation Tips:
# gentoo: emerge XML-LibXML ....
# debian: install gentoo OR apt-get install libxml-libxml-perl
# redhat: install windows
# slackware: rock on!
use XML::LibXML;
use LWP::UserAgent;
use IO::File;
use strict;
# Config
my $username = "someuser";
my $warning_pct = 80;
my $userAgent = "BandWidth Snarf v1.11/" . $username;
my $myLastState = $ENV{HOME} . "/.bw_state";
my $maxTime = 43200;
my $xmlState;
my $kilobyte = 1024;
my $megabyte = $kilobyte * 1024;
my $gigabyte = $megabyte * 1024;
if ((time()-(stat($myLastState))[9] > $maxTime) || ((our ($f)=(stat($myLastState))[6]) && $f==0) ) {
# Use LWP to GET the data without any error checking.
my $ua = LWP::UserAgent->new(keep_alive => 0, timeout => 10, agent => $userAgent);
my $req = "http://www.linode.com/members/info/?user=" . $username;
my $res = $ua->get($req) || die "uh oh: $!\n";
my $fh = new IO::File($myLastState, 'w') || die "Ack $!";
print $fh $res->content;
$fh->close;
$xmlState = $res->content;
} else {
my $fh = new IO::File($myLastState, 'r') || die "Ack $!";
local $/;
$xmlState = <$fh>;
$fh->close;
}
# Instantiate a new parser object then utilize the crap out of it
my $parser = new XML::LibXML;
my $doc = $parser->parse_string($xmlState);
# Leeto like a burrito parse da shanizzle out!
my $year = $doc->findvalue('/linData/bwdata/year/text()');
my $month = $doc->findvalue('/linData/bwdata/month/text()');
my $max_bytes = $doc->findvalue('/linData/bwdata/max_avail/text()');
my $rx_bytes = $doc->findvalue('/linData/bwdata/rx_bytes/text()');
my $tx_bytes = $doc->findvalue('/linData/bwdata/tx_bytes/text()');
my $total_bytes = $doc->findvalue('/linData/bwdata/total_bytes/text()');
# new, v1.11 +
my $parentHost = $doc->findvalue('/linData/host/host/text()');
my $hostLoad = $doc->findvalue('/linData/host/hostLoad/text()');
my $pendingJobs = $doc->findvalue('/linData/host/pendingJobs/text()');
# Create some potentially useful variables based on the datum above
my $timestamp = $month . "/" . $year;
if ( ( $rx_bytes + $tx_bytes ) != $total_bytes ) {
print "Hmmm. My tx+rx count != caker's total_bytes count!\n";
print "Additionally, you shouldn't ever see this message.";
}
printf("As of %s you are currently %s your set limit (%d%%).\n",
scalar(gmtime((stat($myLastState))[9])),
((($total_bytes / $max_bytes) * 100) > $warning_pct) ? "OVER" : "UNDER",
$warning_pct);
printf("\tInput Bytes Xfer: %02.2f GiBs\tHost: %s\n", $rx_bytes / $gigabyte, $parentHost);
printf("\tOutput Bytes Xfer: %02.2f GiBs\tLoad: %s\n", $tx_bytes / $gigabyte,uc($hostLoad));
printf("\t------------------------\n");
printf("\tTotal Bytes Xfer: %02.2f GiBs of %02.2f available.\n",
$total_bytes / $gigabyte, $max_bytes / $gigabyte);
You can download this from
Code:
http://forever.broked.net/~jason/bw.pl.txt
Please report any bugs to
bw@broked.net or find me on IRC.
You will need to edit a couple of things in the script as defined inside it for this to work. You will also need the perl modules used... i've listed installation instructions for these modules for each distribution as best I can inside the script.
LASTLY - Please *LEAVE* it at 7200 seconds and do not modify the $useragent variable.
Not modifying them makes you a very good community member and mikegrb will give you cookies.
-NOTE-
There are a couple of minor bugs in this that only show up with -w and strict on first run due to uninitialized variables. It is a one-time error.
ALSO - If you fail to properly set your $username, you will have to manually remove $HOME/.bw_state and try again.
Lastly - the time displayed is in UTC instead of localtime.
-EDIT-
Thu Feb 17 18:42:28 EST 2005 - jasonl
Found a bug in 'Output'. Really. I apparently typo'd 'Otput'.
Fri Jun 17 22:52:45 EDT 2005 -jasonl
Fixed long standing issue with a 0 byte state file.
Tue Jan 23 23:47:04 EST 2007 -jasonl
Updated for /info vs. /bw URL, fixed bug in useragent, added hostload information
-efudd