Bandwidth

From LinodeWiki

Jump to: navigation, search

http://www.linode.com/forums/viewtopic.php?t=1558


Example Output:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<linData>
       <host>
               <host>host41.linode.com</host>
               <hostLoad>low</hostLoad>
               <pendingJobs>0</pendingJobs>
       </host>
       <upSince>2005-03-16 11:49:34</upSince>
       <cpuConsumption>0.01</cpuConsumption>
       <bwdata>
               <year>2005</year>
               <month>3</month>
               <max_avail>39728447488</max_avail>
               <rx_bytes>4692233</rx_bytes>
               <tx_bytes>2963967</tx_bytes>
               <total_bytes>7656200</total_bytes>
       </bwdata>
</linData>

You can download a perlscript that stops webpages when their transfer limit(s) have been reached. Look here to see what I mean:

http://64.22.109.195/1/bw.html

It starts/stops my webpages depending on how much transfer I have left for the month. You're welcome to copy it.

P.S. Run the script as a cronjob. Instructions are inside the script. Running the cronjob less often than once an hour or so, makes you a considerate user.

--Harmone 19:07, 24 Jun 2007 (EDT)

[edit] PHP script to retrieve the bandwidth in a nicer way

Here's a little script I've wrote, to use from command line: (you'll need to have php client for that... install php-cli)

#!/usr/bin/php
<?php

function human ( $num ) {
  if ($num<1024) {
    $str = strval($num)." K";
  } elseif ($num<(1024*1024)) {
    $str = strval(intval(floor($num/1024)))." M";
  } else {
    $str = strval(intval(floor($num/(1024*1024))))." G";
  }
  return $str;
} 

function kb ( $bytes ) {
  $bytes /=1024;
  return $bytes;
}

exec("wget -q --output-document=/tmp/mybw.xml http://www.linode.com/members/info/?user=yourusername",$output);
$xml = simplexml_load_file('/tmp/mybw.xml');
$bwdata = $xml->bwdata;
$year = $bwdata->year;
$month = $bwdata->month;

// $bw_max = round ( GB( floatval( $xml->bwdata->max_avail ) ), 2 ) ;
$dl = round(kb(floatval($bwdata->rx_bytes)),2);
$ul = round(kb(floatval($bwdata->tx_bytes)),2);
$total = round(kb(floatval($bwdata->total_bytes)),2);
$limit = round(kb(floatval($bwdata->max_avail)),2);

$precentage = round($total/$limit*100,0)."%";

echo("MyBandWidth, version 0.1\n");
echo("\n");
echo("Month: $month/$year\n");
echo("Uploaded: ".human($ul)."\n");
echo("Downloaded: ".human($dl)."\n");
echo("Total use: ".human($total)." from ".human($limit)." allowed ($precentage)\n");
echo("\n");

?>

In my system it's in /usr/bin/mybw, but it doesn't have to be, as long as it is executable.

Now - just type wherever you are mybw (if it's in /usr/bin or so) or run it with php command, and evuala.

Hope it helps... post questions in my talk page. Peleg 19:51, 30 May 2008 (EDT)

[edit] Another PHP Script for webservers

This is a PHP script I did. It is ideal for webservers, to allow the webmaster to check the bandwidth usage wherever he is. Note that the script offers no protection, you should add some password mechanism or protect it with server authentication or something.

<?php
/******************************
 * Linode Bandwith Information
 * By Turl
 ******************************/

// Config
define('LINODE','your linode id (linodeXXXXX)');

//Get data
$data = file_get_contents('http://www.linode.com/members/info/?user='.LINODE);
if($data === false)
	die('oops... linode didn\'t give me the HTML...');


$dom = DOMDocument::loadXML($data);
$xpath = new DOMXPath($dom);

?>
<!DOCTYPE html>
<html>
<head>
	<title>Linode Stats for <?php echo LINODE; ?></title>
</head>
<body>
<?php
	
echo '<h1>Linode stats for '.LINODE.' </h1>';
echo '<h2>Host Stats</h2>';
echo '<p><ul><li><strong>Host:</strong> '.getFirstContent($xpath,'/linData/host/host').'</li>';
echo '<li><strong>Load:</strong> '.getFirstContent($xpath,'/linData/host/hostLoad').'</li>';
echo '<li><strong>Pending Jobs:</strong> '.getFirstContent($xpath,'/linData/host/pendingJobs').'</li></ul></p>';

echo '<h2>Linode Stats</h2>';
echo '<p><ul><li><strong>Running Since:</strong> '.getFirstContent($xpath,'/linData/upSince').'</li>';
echo '<li><strong>CPU Consumption:</strong> '.getFirstContent($xpath,'/linData/cpuConsumption').'%</li></ul></p>';

echo '<h3>Bandwidth Usage</h3>';
echo '<p><ul><li><strong>Period:</strong> '.getFirstContent($xpath,'/linData/bwdata/month').'/';
echo getFirstContent($xpath,'/linData/bwdata/year').'</li>';
echo '<li><strong>Received:</strong> '.formatBytes(getFirstContent($xpath,'/linData/bwdata/rx_bytes')).'</li>';
echo '<li><strong>Sent:</strong> '.formatBytes(getFirstContent($xpath,'/linData/bwdata/tx_bytes')).'</li>';
echo '<li><strong>Total:</strong> '.formatBytes(getFirstContent($xpath,'/linData/bwdata/total_bytes')).'</li>';
echo '<li><strong>Max. Avalable:</strong> '.formatBytes(getFirstContent($xpath,'/linData/bwdata/max_avail')).'</li></ul></p>';


?>
</body>
</html>
<?php

/*****FUNCTIONS*****/
function getFirstContent($xp,$exp)
{
	$nodes = $xp->query($exp);
	if($nodes->length > 0)
		return $nodes->item(0)->textContent;
	
	return false;
}

function formatBytes($bytes, $precision = 2)
{
	$units = array('B', 'KB', 'MB', 'GB', 'TB');
	
	$bytes = max($bytes, 0);
	$pow = floor(($bytes ? log($bytes) : 0) / log(1024));
	$pow = min($pow, count($units) - 1);
	
	$bytes /= pow(1024, $pow);
	
	return round($bytes, $precision) . ' ' . $units[$pow];
} 

--Turl 20:46, 5 Jul 2009 (ART)


Personal tools