traffic.phps
#!/usr/bin/php -q
<?
// traffic.php
// Displays the total amount of transferred data the current month, taken from a rrdtool-database
// hellfairy - http://packy.se/
require("/home/erik/public_html/roundbytes.php");
$intFromDate = mktime(0,0,0,date("m"),"1",date("Y"));
#$intFromDate = 0;
$intToDate = mktime(0,0,0,(date("m")+1),"1",(date("Y")));
$intTotalSeconds = $intToDate - $intFromDate;
$arrValues = array();
$intOkOver = 0;
$intCounter = 0;
$intTotalIn = 0;
$intTotalOut = 0;
$fd = fopen("/home/erik/www/rrdtool/cbl0.rrd", "r") or die("Can't open log file");
while (!feof($fd)) {
$strLina = fgets($fd, 4096);
$arrLina = split(" ", $strLina);
if (($arrLina[0] > $intFromDate) & ($arrLina[0] < $intToDate)){
$arrValues[$intCounter] = array($arrLina[0],$arrLina[1],$arrLina[2]);
$intCounter ++;
$intOkOver = 1;
}
else if ($intOkOver){
$arrValues[$intCounter] = array($arrLina[0],$arrLina[1],$arrLina[2]);
$intCounter ++;
$intOkOver = 0;
}
}
fclose ($fd);
for ($i = sizeof($arrValues)-1; $i >= 0; $i--){
list($intTime, $intIn, $intOut) = $arrValues[$i];
if ($i == sizeof($arrValues)-1){
$intLastTime = $intTime;
}
else{
$intRange = $intTime - $intLastTime;
$intTotalIn += $intRange * $intIn;
$intTotalOut += $intRange * $intOut;
$intLastTime = $intTime;
}
}
#print "Total in: ".roundbytes($intTotalIn)."\n";
#print "Total out: ".roundbytes($intTotalOut)."\n";
#print "Total: ".roundbytes($intTotalIn+$intTotalOut)."\n";
print "Traffic stats for kakburk.ath.cx this month: ".roundbytes($intTotalIn+$intTotalOut)." (".roundbytes($intTotalIn)." downloaded / ".roundbytes($intTotalOut)." uploaded / ratio: ".number_format(roundbytes($intTotalOut)/roundbytes($intTotalIn),3).")\n";
?>