Location: PHPKode > projects > WaveWatcher > wavewatcher3/html/graph/olddatatotal.php
<?
// *** *** *** INPUT VARIABLES *** *** *** 
$interface = empty($interface) ? "3" : $interface;	
$node_id = empty($node_id) ? "10" : $node_id;
$nSizeX = empty($xsize) ? 450 : $xsize < 1 ? 450 : $xsize;						// default width = 450px
$nSizeY = empty($ysize) ? 225 : $ysize < 1 ? 225 : $ysize;						// default height = 225px
$font = empty($font) ? 2 : $font;														// default font = 2
$nHorPos = empty($xpos) ? 24 : $xpos < 1 ? 24 : $xpos;							// default nHorPos = 24 (1 day)
$nVerPos = empty($ypos) ? 5 : $ypos < 1 ? 5 : $ypos;								// default nVerPos = 5
$nDrawMode = empty($drawmode) ? 1 : $drawmode;										// default nDrawMode = 1 (small line)
$nHorGridMode = empty($xgrid) ? 3 : $xgrid;											// default $nHorGridMode = 3 (show all horizontal gridlines)
$nHorGridMode = ($nHorGridMode < 1 || $nHorGridMode > 3) ? 3 : $nHorGridMode;	// make sure it is a correct value
$nVerGridMode = empty($ygrid) ? 3 : $ygrid;											// default $nVerGridMode = 3 (show all vertical gridlines)
$nVerGridMode = ($nVerGridMode < 1 || $nVerGridMode > 3) ? 3 : $nVerGridMode;	// make sure it is a correct value
$dateStopTime = empty($stoptime) ? null : $stoptime;								// default "time now"
$dateStartTime = empty($starttime) ? null : $starttime;							// default "time now - 24h"
$nPresentation = empty($mode) ? 1 : $mode;											// default hours
$nMaxKiloBit = empty($maxkbit) ? 0 : $maxkbit < 1 || $maxkbit > 9999 ? 0 : $maxkbit;		// default: automatic calculation
$nBitsPerDel = empty($kbitperdel) ? 0 : $kbitperdel < 10 || $kbitperdel > 1000 ? 100 : $kbitperdel;	// default 100 kbit

// Sets default value to $nPresentation if its present value is not valid
$nPresentation = $nPresentation != 1 &&  $nPresentation != 2 && $nPresentation != 3 ? 1 : $nPresentation;

// *** *** *** INTERNAL VARIABLES *** *** *** 
$nStopTime = time();
//$nStopTime = 1015580663;

// Calculates $nStartTime from $nStopTime (days if $nPresentation == 2 || 3 otherwise hours)
$nStartTime = $nStopTime - $nHorPos * ($nPresentation == 2 || $nPresentation == 3  ? 3600 * 24 : 3600);

// If $dateStartTime && $dateStopTime have been sent as parameters to the program
// we calculate a valid $dateStartTime && $dateStopTime and finally we find a suitable 
// $nHorPos also!
if(!empty($dateStopTime) && !empty($dateStartTime))
{
	$nStopTime = mktime(substr($dateStopTime,6,2), 0, 0, substr($dateStopTime,2,2), substr($dateStopTime,4,2), substr($dateStopTime,0,2));
	$nStartTime = mktime(substr($dateStartTime,6,2), 0, 0, substr($dateStartTime,2,2), substr($dateStartTime,4,2), substr($dateStartTime,0,2));
	$nHorPos = ($nStopTime - $nStartTime) / 3600 / ($nPresentation == 2 || $nPresentation == 3  ? 24 : 1);	
}


// *** *** *** DATABASE ACCESS *** *** *** 
require_once("../../classes/dbconnection.php");
$dbcn = new DBConnection();
$dbcn->Connect();

$query = "SELECT utime, rxbytes, txbytes FROM node_interface_log WHERE node_id = $node_id and interface = $interface and utime % (1*$nPresentation) = 0 and utime > $nStartTime and utime < $nStopTime ORDER BY utime";
$result = $dbcn->Query($query);
$dbcn->Disconnect();
$nCounter = 0;
$nMaxBitInDB = 0;


List($time, $inbytes, $outbytes) = $dbcn->FetchRow($result);
$nInBytesOld	= $inbytes;
$nOutBytesOld	= $outbytes;
$nTimeOld		= $time;
$arrTransfer[$nCounter]["time"]		= $time;
$arrTransfer[$nCounter]["inbytes"]	= $inbytes;
$arrTransfer[$nCounter]["outbytes"] = $outbytes;
$nCounter++;

while( List($time, $inbytes, $outbytes) = $dbcn->FetchRow($result) )
{
	$inbits = (($inbytes-$nInBytesOld) / ($time-$nTimeOld)) * 8;
	$outbits = (($outbytes-$nOutBytesOld) / ($time-$nTimeOld)) * 8;
	$nMaxBitInDB = $nMaxBitInDB < $outbits || $nMaxBitInDB < $inbits ? ($outbits >= $inbits ? $outbits : $inbits) : $nMaxBitInDB;		

	$nInBytesOld = $inbytes;
	$nOutBytesOld = $outbytes;
	$nTimeOld = $time;

	$arrTransfer[$nCounter]["time"] = $time;
	$arrTransfer[$nCounter]["inbytes"] = $inbytes;
	$arrTransfer[$nCounter]["outbytes"] = $outbytes;
	$nCounter++;
}

$dbcn->Connect();
$query = "SELECT node_ip, node_name FROM nodes WHERE node_id = $node_id";
$result = $dbcn->Query($query);
$dbcn->Disconnect();
List($node_ip, $node_name) = $dbcn->FetchRow($result);										
// nodes ip-address
// *** *** *** !END! DATABASE ACCESS !END! *** *** ***

// If $nMaxKiloBit have not been sent as a parameter to the program
// the program shall by itself find a suitable $nMaxKiloBit
$nMaxKiloBit = $nMaxKiloBit != 0 ? $nMaxKiloBit : ($nMaxBitInDB == 0 ? 500 : $nMaxBitInDB/1000);

// If $nBitsPerDel have not been sent as a parameter to the program
// the program shall by itself find a suitable $nBitsPerDel
$nBitsPerDel = $nBitsPerDel == 0 ? ceil(($nMaxKiloBit)/$nVerPos) : $nBitsPerDel;

// If there are no valid $nVerPos sent to the program we must calculate 
// by our self a $nVerPos so the y-scale will be correct!
$nVerPos = $nVerPos == 5 ? ceil($nMaxKiloBit / $nBitsPerDel) : $nVerPos;

$nFontHeight = ImageFontHeight($font);
$nFontOffsetY = $nFontHeight / 2;
$nFontWidth = ImageFontWidth($font);
$nFontOffsetX = $nFontWidth / 2;
$nLeft = 4 * 2 + $nFontWidth * 6;
$nRight = 15;
$nTop = 2 + $nFontHeight * 1 + 4 * 2;
$nBottom = 2 + $nFontHeight * 2 + 3 * 2;
$nHeight = $nSizeY - $nTop - $nBottom;
$nWidth = $nSizeX - $nLeft - $nRight;
$nStepY = ($nHeight / $nVerPos);
$nStepX = ($nWidth / $nHorPos);


// Define header type
Header("Content-type: image/png");

// Create image
$im = ImageCreate($nSizeX, $nSizeY);

// Define colors
$clrWhite = ImageColorAllocate($im, 255, 255, 255);
$clrBlack = ImageColorAllocate($im, 0, 0, 0);
$clrBlue = ImageColorAllocate($im, 0, 0, 224);
$clrRed = ImageColorAllocate($im, 224, 0, 0);
$clrGreen = ImageColorAllocate($im, 0, 192, 0);
$clrTitle = ImageColorAllocate($im, 0, 0, 0); 
$clrGrid = ImageColorAllocate($im, 192, 192, 192);

//Sets style to gridlines
$arrGridStyle=array($clrGrid,$clrGrid,$clrGrid,$clrWhite,$clrWhite,$clrWhite);
$arrGridStyleRed=array($clrRed,$clrRed,$clrWhite,$clrWhite);
ImageSetStyle($im, $arrGridStyle);

// Draw outer border
ImageRectangle($im, 0, 0, $nSizeX-1, $nSizeY-1, $clrBlack);

// Write title and bottom text
ImageString($im, $font, 4, 2, $node_name." (".$node_ip.") interface=".$interface, $clrTitle);
ImageString($im, $font, $nSizeX-$nFontWidth*11-4, 2, "kbps", $clrTitle);
ImageString($im, $font, $nSizeX-$nFontWidth*6-4, 2, "in", $clrGreen);
ImageString($im, $font, $nSizeX-$nFontWidth*4-4, 2, "/", $clrTitle);
ImageString($im, $font, $nSizeX-$nFontWidth*3-4, 2, "out", $clrBlue);

// Draw horizontal grid lines and vertical numbers
$nSpeed = 0;
$nPosY = $nBottom;
for($pos=0; $pos<=$nVerPos; $pos++)
{
	ImageString($im, $font, 4, $nSizeY-$nPosY-$nFontOffsetY, "$nSpeed k", $clrBlack);	
	if( ($nSizeY-$nPosY-$nStepY/2) > $nTop )
	{
		if ($nHorGridMode == 2 || $nHorGridMode == 3 )
		{
			ImageLine($im, $nLeft+3, $nSizeY-$nPosY-$nStepY, $nSizeX-$nRight, $nSizeY-$nPosY-$nStepY, IMG_COLOR_STYLED);
		}
		
		if ($nHorGridMode == 3 ) 
		{
			ImageLine($im, $nLeft, $nSizeY-$nPosY-$nStepY/2, $nSizeX-$nRight, $nSizeY-$nPosY-$nStepY/2, IMG_COLOR_STYLED);
		}
	}	
	ImageLine($im, $nLeft-3, $nSizeY-$nPosY, $nLeft+3, $nSizeY-$nPosY, $clrBlack);
	
	$nSpeed += $nBitsPerDel;
	$nPosY += $nStepY;
}

// Draw vertical grid lines and horizontal numbers
$nMinutes = strftime("%M", $nStopTime);
$nHours = strftime("%H", $nStopTime);
$nMinutesOffset = ($nStepX/60) * $nMinutes;
$nHoursOffset = $nStepX/24/60 * (($nHours+($nMinutes/60))*60);
$nDaysOffset = $nStepX/24/60/7 * (($nHours+($nMinutes/60))*60*7);

switch($nPresentation)
{
	case 1:	$nPosTime = $nLeft + $nHorPos * $nStepX - $nMinutesOffset;
				break;

	case 2:	$nPosTime = $nLeft + $nHorPos * $nStepX - $nHoursOffset;
				break;

	case 3:	$nPosTime = $nLeft + $nHorPos * $nStepX - $nDaysOffset;
				break;

	default:	break;
}

$nPrint = 2;
$nSecondLine = 1;
$bSeperator = false;
$bDelimitationLine = true;

for($pos=0; $pos<=$nHorPos; $pos++)
{
	switch($nPresentation)
	{
		case 1:	$nXText = strftime("%H", $nStopTime - 3600 * $pos);		// What shall we print out??
					if( $nXText % 2 == 0 && $nPosTime > ($nLeft-1) )		// Only print every other hour
					{
						ImageString($im, $font, $nPosTime-$nFontWidth, $nSizeY-$nBottom+4, "$nXText", $clrBlack);
					}
					$bSeperator = $nXText == "0" ? true : false;
					break;

		case 2:	$nXText = strftime("%a", $nStopTime - 3600 * 24 * $pos);		// What shall we print out??
					if( ($nPosTime+$nStepX/2+$nFontWidth*1.5) < ($nSizeX-$nRight) && ($nPosTime+$nStepX/2-$nFontWidth*1.5) > $nLeft /*&& $nPosTime > ($nLeft-1)*/ )
					{
						if( ($nStepX-10 < $nFontWidth*3) ? ($nPrint % 2 == 0) : 1 )
						{
							ImageString($im, $font, $nPosTime+$nStepX/2-$nFontWidth*1.5, $nSizeY-$nBottom+4, "$nXText", $clrBlack);
						}
					}
					$nPrint = 3 - $nPrint;
					$bSeperator = strftime("%w", $nStopTime - 3600 * 24 * $pos) == "1" ? true : false;
					break;

		case 3:	$nXText = strftime("%V", $nStopTime - 3600 * 24 * $pos);		// What shall we print out??
					if( ($nXText) != strftime("%V", $nStopTime - 3600 * 24 * ($pos+1)) )
					{
						if( $nPosTime+$nStepX*7/2+$nFontWidth < $nSizeX-$nRight && $nPosTime+$nStepX*7/2-$nFontWidth > $nLeft /*&& $nPosTime > ($nLeft-1)*/ )
						{			
							ImageString($im, $font, $nPosTime+$nStepX*7/2-$nFontWidth*2, $nSizeY-$nBottom+4, "w $nXText", $clrBlack);
						}						
						$bDelimitationLine = true;
					}
					else
					{
						$bDelimitationLine = false;
					}					
					$nPrint = 3 - $nPrint;
					$bSeperator = strftime("%d", $nStopTime - 3600 * 24 * $pos) == "01" ? true : false;
					break;

		default:	break;
	}
	$nSecondLine = 3 - $nSecondLine;
   
	if( $nPosTime >= $nLeft )
	{
		// draw gridline if the gridmode is correct
		if ($nVerGridMode != 1 && ($nVerGridMode == 3 || $nSecondLine % 2 == 0) && $bDelimitationLine) 
		{	
			ImageLine($im, $nPosTime, $nTop, $nPosTime, $nTop+$nHeight, IMG_COLOR_STYLED);	
		}

		if($bSeperator && $nVerGridMode != 1)
		{
			ImageSetStyle($im, $arrGridStyleRed);
			ImageLine($im, $nPosTime, $nTop, $nPosTime, $nTop+$nHeight, IMG_COLOR_STYLED);	
			ImageSetStyle($im, $arrGridStyle);
		}
		
		if( $bDelimitationLine )
			ImageLine($im, $nPosTime, $nSizeY-$nBottom-3, $nPosTime, $nSizeY-$nBottom+3, $clrBlack);		
	}	
	$nPosTime -= $nStepX;	
}


// Draw graph
$nPixelPerSec = $nWidth / ($nStopTime-$nStartTime);
$nPixelPerK = $nHeight / ($nVerPos*$nBitsPerDel*1000);
$nInY = $nSizeY-$nBottom;
$nOutY = $nSizeY-$nBottom;

if( sizeof($arrTransfer) != 0 )
{	
	$nX = ($arrTransfer[0]["time"]-$nStartTime) * $nPixelPerSec + $nLeft;	
	$nXOld = $nX;
	$nInYOld = $nInY;
	$nOutYOld = $nOutY;
	$nDownTime = ($nStopTime-$nStartTime)/50;

	for($pos=1; $pos<$nCounter; $pos++)
	{
			$nX = ($arrTransfer[$pos]["time"]-$nStartTime) * $nPixelPerSec + $nLeft;

			if( $arrTransfer[$pos]["inbytes"] < $arrTransfer[$pos-1]["inbytes"] )
				$arrTransfer[$pos-1]["inbytes"] = 0;

			if( $arrTransfer[$pos]["outbytes"] < $arrTransfer[$pos-1]["outbytes"] )
				$arrTransfer[$pos-1]["outbytes"] = 0;

			if( $nDownTime < ($arrTransfer[$pos]["time"]-$arrTransfer[$pos-1]["time"]) )
			{
				$nXOld = $nX;
				$nInYOld = $nSizeY-$nBottom;
				$nOutYOld = $nSizeY-$nBottom;				
			}

			$nInY = $nSizeY - $nBottom - (($arrTransfer[$pos]["inbytes"]-$arrTransfer[$pos-1]["inbytes"])/($arrTransfer[$pos]["time"]-$arrTransfer[$pos-1]["time"]))*8 * $nPixelPerK;	
			$nOutY = $nSizeY - $nBottom - (($arrTransfer[$pos]["outbytes"]-$arrTransfer[$pos-1]["outbytes"])/($arrTransfer[$pos]["time"]-$arrTransfer[$pos-1]["time"]))*8 * $nPixelPerK;

			if ($nDrawMode == 1) // small line
			{		
				ImageLine($im, $nXOld, $nInYOld, $nX, $nInY, $clrGreen);
				ImageLine($im, $nXOld, $nOutYOld, $nX, $nOutY, $clrBlue);		
			}		
			else if($nDrawMode == 2) // filled small line
			{
				ImageLine($im, $nXOld, $nInYOld, $nX, $nInY, $clrGreen);
				ImageFilledRectangle($im, $nXOld, $nInY, $nX, $nSizeY-$nBottom, $clrGreen);
				ImageLine($im, $nXOld, $nOutYOld, $nX, $nOutY, $clrBlue);
			}
			else
			{
				ImageLine($im, $nXOld, $nInYOld, $nX, $nInY, $clrGreen);
				ImageLine($im, $nXOld, $nOutYOld, $nX, $nOutY, $clrBlue);
			}		

		$nXOld = $nX;
		$nInYOld = $nInY;
		$nOutYOld = $nOutY;
	}
}

// Write timeperiod
$strPeriodStart = strftime("%y-%m-%d %R", $nStartTime);
$strPeriodStop = strftime("%y-%m-%d %R", $nStopTime);
$timemode = $nPresentation == 1 ? "hour" : "day";
$timemode = $nPresentation == 3 ? "week" : $timemode;

//$timemode = $nPresentation;
ImageString($im, $font, $nSizeX/2-($nFontWidth*31)/2, $nSizeY-$nFontHeight-2, "$strPeriodStart - $strPeriodStop [$timemode]", $clrTitle);

// Draw delimitation lines
ImageLine($im, $nLeft, $nSizeY-$nBottom, $nLeft+$nWidth, $nSizeY-$nBottom, $clrBlack);
ImageLine($im, $nLeft, $nTop, $nLeft, $nTop+$nHeight, $clrBlack);

// Output Image
ImagePng($im);
ImageDestroy($im);
?>
Return current item: WaveWatcher