lede-packages-rs

git clone git://archive.git.mtrnord.blog/MTRNord/lede-packages-rs.git
Log | Files | Refs | README | LICENSE

graph.php (12782B)


      1 <?php
      2 require("include.php");
      3 
      4 // Returns x location of any given timestamp
      5 function ts2x($ts)
      6 	{
      7 	global $timestamp, $width, $interval;
      8 	return(($ts-$timestamp)*(($width-XOFFSET) / $interval) + XOFFSET);
      9 	}
     10 
     11 // If we have multiple IP's in a result set we need to total the average of each IP's samples
     12 function AverageAndAccumulate()
     13 	{
     14 	global $Count, $total, $icmp, $udp, $tcp, $ftp, $http, $p2p, $YMax;
     15 	global $a_total, $a_icmp, $a_udp, $a_tcp, $a_ftp, $a_http, $a_p2p;
     16 
     17 	foreach ($Count as $key => $number)
     18     	{
     19 	    $total[$key] /= $number;
     20     	$icmp[$key] /= $number;
     21     	$udp[$key] /= $number;
     22     	$tcp[$key] /= $number;
     23     	$ftp[$key] /= $number;
     24     	$http[$key] /= $number;
     25     	$p2p[$key] /= $number;
     26     	}
     27 
     28 	foreach ($Count as $key => $number)
     29 		{
     30 		$a_total[$key] += $total[$key];
     31 		$a_icmp[$key] += $icmp[$key];
     32 		$a_udp[$key] += $udp[$key];
     33 		$a_tcp[$key] += $tcp[$key];
     34 		$a_ftp[$key] += $ftp[$key];
     35 		$a_http[$key] += $http[$key];
     36 		$a_p2p[$key] += $p2p[$key];
     37 
     38 		if ($a_total[$key] > $YMax)
     39 			$YMax = $a_total[$key];
     40 		}
     41 
     42 	unset($GLOBALS['total'], $GLOBALS['icmp'], $GLOBALS['udp'], $GLOBALS['tcp'], $GLOBALS['ftp'], $GLOBALS['http'], $GLOBALS['p2p'], $GLOBALS['Count']);
     43 
     44 	$total = array();
     45 	$icmp = array();
     46 	$udp = array();
     47 	$tcp = array();
     48 	$ftp = array();
     49 	$http = array();
     50 	$p2p = array();
     51 	$Count = array();
     52 	}
     53 
     54 $db = ConnectDb();
     55 
     56 // Get parameters
     57 
     58 if (isset($_GET['width']))
     59     $width = $_GET['width'];
     60 else
     61 	$width = DFLT_WIDTH;
     62 
     63 if (isset($_GET['height']))
     64     $height = $_GET['height'];
     65 else
     66 	$height = DFLT_HEIGHT;
     67 
     68 if (isset($_GET['interval']))
     69     $interval = $_GET['interval'];
     70 else
     71 	$interval = DFLT_INTERVAL;
     72 
     73 if (isset($_GET['ip']))
     74     $ip = $_GET['ip'];
     75 else
     76 	exit(1);
     77 
     78 if (isset($_GET['sensor_name']))
     79 	$sensor_name = $_GET['sensor_name'];
     80 else
     81 	exit(1);
     82 
     83 if (isset($_GET['timestamp']))
     84     $timestamp = $_GET['timestamp'];
     85 else
     86 	$timestamp = time() - $interval + (0.05*$interval);
     87 
     88 if (isset($_GET['table']))
     89     $table = $_GET['table'];
     90 else
     91 	$table = "bd_rx_log";
     92 
     93 if (isset($_GET['yscale']))
     94     $yscale = $_GET['yscale'];
     95 
     96 $total = array();
     97 $icmp = array();
     98 $udp = array();
     99 $tcp = array();
    100 $ftp = array();
    101 $http = array();
    102 $p2p = array();
    103 $Count = array();
    104 
    105 // Accumulator
    106 $a_total = array();
    107 $a_icmp = array();
    108 $a_udp = array();
    109 $a_tcp = array();
    110 $a_ftp = array();
    111 $a_http = array();
    112 $a_p2p = array();
    113 
    114 $sql = "select *, extract(epoch from timestamp) as ts from sensors, $table where sensors.sensor_id = ".$table.".sensor_id and ip <<= '$ip' and sensor_name = '$sensor_name' and timestamp > $timestamp::abstime and timestamp < ".($timestamp+$interval)."::abstime order by ip;";
    115 //echo $sql."<br>"; exit(1);
    116 $result = pg_query($sql);
    117 
    118 // The SQL statement pulls the data out of the database ordered by IP address, that way we can average each
    119 // datapoint for each IP address to provide smoothing and then toss the smoothed value into the accumulator
    120 // to provide accurate total traffic rate.
    121 
    122 while ($row = pg_fetch_array($result))
    123 	{
    124 	if ($row['ip'] != $last_ip)
    125 		{
    126 		AverageAndAccumulate();
    127 		$last_ip = $row['ip'];
    128 		}
    129 
    130 	$x = ($row['ts']-$timestamp)*(($width-XOFFSET)/$interval)+XOFFSET;
    131 	$xint = (int) $x;
    132 
    133 	//echo "xint: ".$xint."<br>";
    134 	$Count[$xint]++;
    135 
    136 	if ($row['total']/$row['sample_duration'] > $SentPeak)
    137 		$SentPeak = $row['total']/$row['sample_duration'];
    138 	$TotalSent += $row['total'];
    139 	$total[$xint] += $row['total']/$row['sample_duration'];
    140 	$icmp[$xint] += $row['icmp']/$row['sample_duration'];
    141 	$udp[$xint] += $row['udp']/$row['sample_duration'];
    142 	$tcp[$xint] += $row['tcp']/$row['sample_duration'];
    143 	$ftp[$xint] += $row['ftp']/$row['sample_duration'];
    144 	$http[$xint] += $row['http']/$row['sample_duration'];
    145 	$p2p[$xint] += $row['p2p']/$row['sample_duration'];
    146 	}
    147 
    148 // One more time for the last IP
    149 AverageAndAccumulate();
    150 
    151 // Pull the data out of Accumulator
    152 $total = $a_total;
    153 $icmp = $a_icmp;
    154 $udp = $a_udp;
    155 $tcp = $a_tcp;
    156 $ftp = $a_ftp;
    157 $http = $a_http;
    158 $p2p = $a_p2p;
    159 
    160 $YMax += $YMax*0.05;    // Add an extra 5%
    161 
    162 // if a y scale was specified override YMax
    163 if (isset($yscale))
    164     $YMax = $yscale/8;
    165 
    166 // Plot the data
    167 
    168 header("Content-type: image/png");
    169 
    170 $im = imagecreate($width, $height);
    171 $white = imagecolorallocate($im, 255, 255, 255);
    172 $purple = ImageColorAllocate($im, 255, 0, 255);
    173 $green  = ImageColorAllocate($im, 0, 255, 0);
    174 $blue   = ImageColorAllocate($im, 0, 0, 255);
    175 $lblue  = ImageColorAllocate($im, 128, 128, 255);
    176 $brown  = ImageColorAllocate($im, 128, 0, 0);
    177 $red    = ImageColorAllocate($im, 255, 0, 0);
    178 $black  = ImageColorAllocate($im, 0, 0, 0);
    179 
    180 for($Counter=XOFFSET+1; $Counter < $width; $Counter++)
    181 	{
    182 	if (isset($total[$Counter]))
    183 		{
    184 		// Convert the bytes/sec to y coords
    185         $total[$Counter] = ($total[$Counter]*($height-YOFFSET))/$YMax;
    186 		$tcp[$Counter] = ($tcp[$Counter]*($height-YOFFSET))/$YMax;
    187         $ftp[$Counter] = ($ftp[$Counter]*($height-YOFFSET))/$YMax;
    188 		$http[$Counter] = ($http[$Counter]*($height-YOFFSET))/$YMax;
    189 		$p2p[$Counter] = ($p2p[$Counter]*($height-YOFFSET))/$YMax;
    190         $udp[$Counter] = ($udp[$Counter]*($height-YOFFSET))/$YMax;
    191 		$icmp[$Counter] = ($icmp[$Counter]*($height-YOFFSET))/$YMax;
    192 
    193 		// Stack 'em up!
    194 		// Total is stacked from the bottom
    195 		// Icmp is on the bottom too
    196 		// Udp is stacked on top of icmp
    197 		$udp[$Counter] += $icmp[$Counter];
    198 		// TCP and p2p are stacked on top of Udp
    199 		$tcp[$Counter] += $udp[$Counter];
    200 		$p2p[$Counter] += $udp[$Counter];
    201  		// Http is stacked on top of p2p
    202 		$http[$Counter] += $p2p[$Counter];
    203 		// Ftp is stacked on top of http
    204         $ftp[$Counter] += $http[$Counter];
    205 
    206 		// Plot them!
    207 		//echo "$Counter:".$Counter." (h-y)-t:".($height-YOFFSET) - $total[$Counter]." h-YO-1:".$height-YOFFSET-1;
    208         ImageLine($im, $Counter, ($height-YOFFSET) - $icmp[$Counter], $Counter, $height-YOFFSET-1, $red);
    209         ImageLine($im, $Counter, ($height-YOFFSET) - $udp[$Counter], $Counter, ($height-YOFFSET) - $icmp[$Counter] - 1, $brown);
    210         ImageLine($im, $Counter, ($height-YOFFSET) - $tcp[$Counter], $Counter, ($height-YOFFSET) - $udp[$Counter] - 1, $green);
    211         ImageLine($im, $Counter, ($height-YOFFSET) - $p2p[$Counter], $Counter, ($height-YOFFSET) - $udp[$Counter] - 1, $purple);
    212         ImageLine($im, $Counter, ($height-YOFFSET) - $http[$Counter], $Counter, ($height-YOFFSET) - $p2p[$Counter] - 1, $blue);
    213         ImageLine($im, $Counter, ($height-YOFFSET) - $ftp[$Counter], $Counter, ($height-YOFFSET) - $http[$Counter] - 1, $lblue);
    214 		}
    215 //	else
    216 //		echo $Counter." not set<br>";
    217 	}
    218 
    219 // Margin Text
    220 if ($SentPeak < 1024/8)
    221 	$txtPeakSendRate = sprintf("Peak Send Rate: %.1f KBits/sec", $SentPeak*8);
    222 else if ($SentPeak < (1024*1024)/8)
    223     $txtPeakSendRate = sprintf("Peak Send Rate: %.1f MBits/sec", ($SentPeak*8.0)/1024.0);
    224 else
    225 	$txtPeakSendRate = sprintf("Peak Send Rate: %.1f GBits/sec", ($SentPeak*8.0)/(1024.0*1024.0));
    226 
    227 if ($TotalSent < 1024)
    228 	$txtTotalSent = sprintf("Sent %.1f KBytes", $TotalSent);
    229 else if ($TotalSent < 1024*1024)
    230 	$txtTotalSent = sprintf("Sent %.1f MBytes", $TotalSent/1024.0);
    231 else
    232 	$txtTotalSent = sprintf("Sent %.1f GBytes", $TotalSent/(1024.0*1024.0));
    233 
    234 ImageString($im, 2, XOFFSET+5,  $height-20, $txtTotalSent, $black);
    235 ImageString($im, 2, $width/2+XOFFSET/2,  $height-20, $txtPeakSendRate, $black);
    236 
    237 // Draw X Axis
    238 
    239 ImageLine($im, 0, $height-YOFFSET, $width, $height-YOFFSET, $black);
    240 
    241 // Day/Month Seperator bars
    242 
    243 if ((24*60*60*($width-XOFFSET))/$interval > ($width-XOFFSET)/10)
    244 	{
    245 	$ts = getdate($timestamp);
    246 	$MarkTime = mktime(0, 0, 0, $ts['mon'], $ts['mday'], $ts['year']);
    247 
    248     $x = ts2x($MarkTime);
    249     while ($x < XOFFSET)
    250     	{
    251         $MarkTime += (24*60*60);
    252 	    $x = ts2x($MarkTime);
    253         }
    254 
    255     while ($x < ($width-10))
    256     	{
    257         // Day Lines
    258         ImageLine($im, $x, 0, $x, $height-YOFFSET, $black);
    259         ImageLine($im, $x+1, 0, $x+1, $height-YOFFSET, $black);
    260 
    261         $txtDate = strftime("%a, %b %d", $MarkTime);
    262         ImageString($im, 2, $x-30,  $height-YOFFSET+10, $txtDate, $black);
    263 
    264         // Calculate Next x
    265         $MarkTime += (24*60*60);
    266 	    $x = ts2x($MarkTime);
    267         }
    268 	}
    269 else if ((24*60*60*30*($width-XOFFSET))/$interval > ($width-XOFFSET)/10)
    270 	{
    271 	// Monthly Bars
    272 	$ts = getdate($timestamp);
    273 	$month = $ts['mon'];
    274 	$MarkTime = mktime(0, 0, 0, $month, 1, $ts['year']);
    275 
    276     $x = ts2x($MarkTime);
    277     while ($x < XOFFSET)
    278     	{
    279 		$month++;
    280         $MarkTime = mktime(0, 0, 0, $month, 1, $ts['year']);
    281 	    $x = ts2x($MarkTime);
    282         }
    283 
    284     while ($x < ($width-10))
    285     	{
    286         // Day Lines
    287         ImageLine($im, $x, 0, $x, $height-YOFFSET, $black);
    288         ImageLine($im, $x+1, 0, $x+1, $height-YOFFSET, $black);
    289 
    290         $txtDate = strftime("%b, %Y", $MarkTime);
    291         ImageString($im, 2, $x-25,  $height-YOFFSET+10, $txtDate, $black);
    292 
    293         // Calculate Next x
    294 		$month++;
    295         $MarkTime = mktime(0, 0, 0, $month, 1, $ts['year']);
    296 	    $x = ts2x($MarkTime);
    297         }
    298 	}
    299 else
    300 	{
    301 	// Year Bars
    302     $ts = getdate($timestamp);
    303     $year = $ts['year'];
    304     $MarkTime = mktime(0, 0, 0, 1, 1, $year);
    305 
    306     $x = ts2x($MarkTime);
    307     while ($x < XOFFSET)
    308         {
    309         $year++;
    310         $MarkTime = mktime(0, 0, 0, 1, 1, $year);
    311         $x = ts2x($MarkTime);
    312         }
    313 
    314     while ($x < ($width-10))
    315         {
    316         // Day Lines
    317         ImageLine($im, $x, 0, $x, $height-YOFFSET, $black);
    318         ImageLine($im, $x+1, 0, $x+1, $height-YOFFSET, $black);
    319 
    320         $txtDate = strftime("%b, %Y", $MarkTime);
    321         ImageString($im, 2, $x-25,  $height-YOFFSET+10, $txtDate, $black);
    322 
    323         // Calculate Next x
    324         $year++;
    325         $MarkTime = mktime(0, 0, 0, 1, 1, $year);
    326         $x = ts2x($MarkTime);
    327         }
    328 	}
    329 
    330 // Draw Major Tick Marks
    331 if ((6*60*60*($width-XOFFSET))/$interval > 10) // pixels per 6 hours is more than 2
    332 	$MarkTimeStep = 6*60*60; // Major ticks are 6 hours
    333 else if ((24*60*60*($width-XOFFSET))/$interval > 10)
    334 	$MarkTimeStep = 24*60*60; // Major ticks are 24 hours;
    335 else if ((24*60*60*30*($width-XOFFSET))/$interval > 10)
    336 	{
    337 	// Major tick marks are months
    338 	$MarkTimeStep = 0; // Skip the standard way of drawing major tick marks below
    339 
    340     $ts = getdate($timestamp);
    341     $month = $ts['mon'];
    342     $MarkTime = mktime(0, 0, 0, $month, 1, $ts['year']);
    343 
    344     $x = ts2x($MarkTime);
    345     while ($x < XOFFSET)
    346         {
    347         $month++;
    348         $MarkTime = mktime(0, 0, 0, $month, 1, $ts['year']);
    349         $x = ts2x($MarkTime);
    350         }
    351 
    352     while ($x < ($width-10))
    353         {
    354         // Day Lines
    355 		$date = getdate($MarkTime);
    356 		if ($date['mon'] != 1)
    357 			{
    358 	        ImageLine($im, $x, $height-YOFFSET-5, $x, $height-YOFFSET+5, $black);
    359     	    $txtDate = strftime("%b", $MarkTime);
    360         	ImageString($im, 2, $x-5,  $height-YOFFSET+10, $txtDate, $black);
    361           	}
    362 
    363         // Calculate Next x
    364         $month++;
    365         $MarkTime = mktime(0, 0, 0, $month, 1, $ts['year']);
    366         $x = ts2x($MarkTime);
    367         }
    368 	}
    369 else
    370 	$MarkTimeStep = 0; // Skip Major Tick Marks
    371 
    372 if ($MarkTimeStep)
    373 	{
    374 	$ts = getdate($timestamp);
    375 	$MarkTime = mktime(0, 0, 0, $ts['mon'], $ts['mday'], $ts['year']);
    376 	$x = ts2x($MarkTime);
    377 
    378 	while ($x < ($width-10))
    379 		{
    380     	if ($x > XOFFSET)
    381 			{
    382     	    ImageLine($im, $x, $height-YOFFSET-5, $x, $height-YOFFSET+5, $black);
    383 	        }
    384 		$MarkTime += $MarkTimeStep;
    385 	    $x = ts2x($MarkTime);
    386 	}
    387 	}
    388 
    389 // Draw Minor Tick marks
    390 if ((60*60*($width-XOFFSET))/$interval > 4) // pixels per hour is more than 2
    391 	$MarkTimeStep = 60*60;  // Minor ticks are 1 hour
    392 else if ((6*60*60*($width-XOFFSET))/$interval > 4)
    393 	$MarkTimeStep = 6*60*60; // Minor ticks are 6 hours
    394 else if ((24*60*60*($width-XOFFSET))/$interval > 4)
    395 	$MarkTimeStep = 24*60*60;
    396 else
    397 	$MarkTimeStep = 0; // Skip minor tick marks
    398 
    399 if ($MarkTimeStep)
    400 	{
    401 	$ts = getdate($timestamp);
    402 	$MarkTime = mktime(0, 0, 0, $ts['mon'], $ts['mday'], $ts['year']);
    403 	$x = ts2x($MarkTime);
    404 
    405 	while ($x < ($width-10))
    406 		{
    407     	if ($x > XOFFSET)
    408 			{
    409     	    ImageLine($im, $x, $height-YOFFSET, $x, $height-YOFFSET+5, $black);
    410 	        }
    411 		$MarkTime += $MarkTimeStep;
    412 	    $x = ts2x($MarkTime);
    413 		}
    414 	}
    415 
    416 // Draw Y Axis
    417 ImageLine($im, XOFFSET, 0, XOFFSET, $height, $black);
    418 
    419 $YLegend = 'k';
    420 $Divisor = 1;
    421 if ($YMax*8 > 1024*2)
    422 	{
    423     $Divisor = 1024;    // Display in m
    424     $YLegend = 'm';
    425     }
    426 
    427 if ($YMax*8 > 1024*1024*2)
    428 	{
    429     $Divisor = 1024*1024; // Display in g
    430     $YLegend = 'g';
    431 	}
    432 
    433 if ($YMax*8 > 1024*1024*1024*2)
    434 	{
    435     $Divisor = 1024*1024*1024; // Display in t
    436     $YLegend = 't';
    437     }
    438 
    439 $YStep = $YMax/10;
    440 if ($YStep < 1)
    441 	$YStep=1;
    442 $YTic=$YStep;
    443 
    444 while ($YTic <= ($YMax - $YMax/10))
    445 	{
    446     $y = ($height-YOFFSET)-(($YTic*($height-YOFFSET))/$YMax);
    447 	ImageLine($im, XOFFSET, $y, $width, $y, $black);
    448     $txtYLegend = sprintf("%4.1f %sbits/s", (8.0*$YTic)/$Divisor, $YLegend);
    449     ImageString($im, 2, 3, $y-7, $txtYLegend, $black);
    450 	$YTic += $YStep;
    451 	}
    452 
    453 imagepng($im);
    454 imagedestroy($im);