Linode Forum
Linode Community Forums
 FAQFAQ    SearchSearch    MembersMembers      Register Register 
 LoginLogin [ Anonymous ] 
Post new topic  Reply to topic
Author Message
PostPosted: Thu Nov 05, 2009 10:32 am 
Offline
Senior Newbie

Joined: Sun Oct 28, 2007 7:50 pm
Posts: 13
It would be nice to have another bar under the bandwidth usage bar that shows the projected total usage vs total allowed usage, based on the Month-to-Date usage so far.

For example, if it's the 3rd of July and you've used 3GB transfer, then the bar would project out:
(3GB/3days * 31 days) = 31GB total transfer

So this way, you can tell right away if you need to cut back on transfers...


Top
   
 Post subject:
PostPosted: Thu Nov 05, 2009 2:18 pm 
Offline
Senior Newbie

Joined: Mon Nov 19, 2007 10:43 am
Posts: 6
I made this a while ago. It's crude but returns a number representing % of bandwidth used prorata, which you can use from a script to throttle accordingly.

Code:
#!/bin/bash

USERNAME="whateveryourlinodeiscalled"

wget -q -O - http://www.linode.com/members/bw/?user=${USERNAME} |
while read -s line ; do
if `echo $line | grep -q max_avail` ; then
  maxavail="`echo $line | grep -o \>.*\< | sed -e 's:^.::' -e 's:.$::'`"
fi
if `echo $line | grep -q rx_bytes` ; then
  rxbytes="`echo $line | grep -o \>.*\< | sed -e 's:^.::' -e 's:.$::'`"
fi
if `echo $line | grep -q tx_bytes` ; then
  txbytes="`echo $line | grep -o \>.*\< | sed -e 's:^.::' -e 's:.$::'`"
fi
if `echo $line | grep -q total_bytes ` ; then
 totalbytes="`echo $line | grep -o \>.*\< | sed -e 's:^.::' -e 's:.$::'`"
fi
if [ -n "$maxavail" -a -n "$totalbytes" ] ; then
  days_per_month="$((`cal | xargs -n1 | tail -n 1`))"
  hpm=`echo $(($days_per_month*24))`
  day_of_month=`date +"%d"`
  hour_of_day=`date +"%H"`
  hour_of_month="$(((`date +%e`-1)*24+`date +%k`))"
  bwperhour=`echo $((($maxavail+($maxavail%$hpm))/$hpm))`
  consumedperhour=`echo $((($totalbytes+($totalbytes%$hour_of_month))/$hour_of_month))`
  ratio=`echo $((100*$consumedperhour/$bwperhour))`
echo "$ratio"
  break
fi
done


Top
   
 Post subject:
PostPosted: Thu Nov 05, 2009 7:50 pm 
Offline
Senior Member
User avatar

Joined: Tue Apr 13, 2004 6:54 pm
Posts: 833
My version of the script, done without dozens of greps so it's more efficient and quicker :-)
Code:
#!/bin/bash

# Your linode user name
USER=XXX

# Where to report mail
EMAIL=YYY

URL="http://www.linode.com/members/bw/?user=$USER"

data=$(lynx --dump $URL)


year=${data#*\<year\>} ; year=${year%%\<*}
month=${data#*\<month\>} ; month=${month%%\<*}
max=${data#*\<max_avail\>} ; max=${max%%\<*}
tx=${data#*\<tx_bytes\>}; tx=${tx%%\<*}
rx=${data#*\<rx_bytes\>}; rx=${rx%%\<*}
total=${data#*\<total_bytes\>}; total=${total%%\<*}

let pct=100*total/max

let tx=tx/1048576
let rx=rx/1048576
let total=total/1048576
let max=max/1048576

{
  echo Report for $year/$month
  echo Transmitted: $tx Mbytes
  echo Received: $rx Mbytes
  echo Total: $total Mbytes
  echo
  echo Allowance: $max Mbytes
  echo Used: ${pct}%
} | /bin/mail -s "Linode bandwidth report" $EMAIL

_________________
Rgds
Stephen
(Linux user since kernel version 0.11)


Top
   
 Post subject:
PostPosted: Fri Nov 06, 2009 7:39 am 
Offline
Senior Newbie

Joined: Mon Nov 19, 2007 10:43 am
Posts: 6
Cool stuff, thanks. Always helps getting a simpleton like me to understand how it should really be done.

Just help me understand what's the 1048576 about that you divide through?


Top
   
 Post subject:
PostPosted: Fri Nov 06, 2009 8:23 am 
Offline
Senior Member
User avatar

Joined: Fri Oct 24, 2003 3:51 pm
Posts: 965
Location: Netherlands
marc_in_lux wrote:
Just help me understand what's the 1048576 about that you divide through?

2^10 - turns bytes into Megabytes

_________________
/ Peter


Top
   
 Post subject:
PostPosted: Fri Nov 06, 2009 9:26 am 
Offline
Senior Member

Joined: Fri Dec 07, 2007 1:37 am
Posts: 385
Location: NC, USA
pclissold wrote:
2^10 - turns bytes into Megabytes

You meant 2^20 of course, 2^10 is Kilobytes.


Top
   
 Post subject:
PostPosted: Fri Nov 06, 2009 11:53 am 
Offline
Senior Member

Joined: Sat Mar 28, 2009 4:23 pm
Posts: 415
Website: http://jedsmith.org/
Location: Out of his depth and job-hopping without a clue about network security fundamentals
Stever wrote:
pclissold wrote:
2^10 - turns bytes into Megabytes

You meant 2^20 of course, 2^10 is Kilobytes.

He meant 2**20, of course, because 2^20 is 22!

(Waiting patiently for the other programmers to get it)

_________________
Disclaimer: I am no longer employed by Linode; opinions are my own alone.


Top
   
 Post subject:
PostPosted: Fri Nov 06, 2009 7:25 pm 
Offline
Senior Member
User avatar

Joined: Tue Apr 13, 2004 6:54 pm
Posts: 833
jed wrote:
Stever wrote:
pclissold wrote:
2^10 - turns bytes into Megabytes

You meant 2^20 of course, 2^10 is Kilobytes.

He meant 2**20, of course, because 2^20 is 22!

(Waiting patiently for the other programmers to get it)

Not in "bc" it isn't :-)

Although since I scripted the answer in bash, I suppose we should use bash semantics where we would use $((2**20)). Whoever decided ^ should be XOR... *sigh* Even after writing C code for 20+ years, that still sometimes catches me out!

_________________
Rgds

Stephen

(Linux user since kernel version 0.11)


Top
   
 Post subject:
PostPosted: Fri Nov 06, 2009 7:57 pm 
Offline
Senior Member

Joined: Sun Aug 02, 2009 1:32 pm
Posts: 222
Website: https://www.barkerjr.net
Location: Connecticut, USA
So, maybe we need two bars on there. One for actual, and one for projected.


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
RSS

Powered by phpBB® Forum Software © phpBB Group