lede-packages-rs

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

graph.php (12778B)


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