#!/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);