"Fossies" - the Fresh Open Source Software Archive

Member "bandwidthd-2.0.1/phphtdocs/graph.php" (8 Jan 2005, 15558 Bytes) of package /linux/privat/old/bandwidthd-2.0.1.tgz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "graph.php" see the Fossies "Dox" file reference documentation.

    1 <?
    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 
   55 $db = ConnectDb();
   56 
   57 // Get parameters
   58 
   59 if (isset($_GET['width']))
   60     $width = $_GET['width'];
   61 else
   62     $width = DFLT_WIDTH;
   63 
   64 if (isset($_GET['height']))
   65     $height = $_GET['height'];
   66 else
   67     $height = DFLT_HEIGHT;
   68 
   69 if (isset($_GET['interval']))
   70     $interval = $_GET['interval'];
   71 else
   72     $interval = DFLT_INTERVAL;
   73 
   74 if (isset($_GET['ip']))
   75     $ip = $_GET['ip'];
   76 else
   77     exit(1);
   78 
   79 if (isset($_GET['sensor_name']))
   80     $sensor_name = $_GET['sensor_name'];
   81 else
   82     exit(1);
   83 
   84 if (isset($_GET['timestamp']))
   85     $timestamp = $_GET['timestamp'];
   86 else
   87     $timestamp = time() - $interval + (0.05*$interval);
   88 
   89 if (isset($_GET['table']))
   90     $table = $_GET['table'];
   91 else
   92     $table = "bd_rx_log";
   93 
   94 if (isset($_GET['yscale']))
   95     $yscale = $_GET['yscale'];
   96 
   97 $total = array();
   98 $icmp = array();
   99 $udp = array();
  100 $tcp = array();
  101 $ftp = array();
  102 $http = array();
  103 $p2p = array();
  104 $Count = array();
  105 
  106 // Accumulator
  107 $a_total = array();
  108 $a_icmp = array();
  109 $a_udp = array();
  110 $a_tcp = array();
  111 $a_ftp = array();
  112 $a_http = array();
  113 $a_p2p = array();
  114 
  115 $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;";
  116 //echo $sql."<br>"; exit(1);
  117 $result = pg_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 = pg_fetch_array($result))
  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))
  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 $yellow = ImageColorAllocate($im, 255, 255, 0);
  174 $purple = ImageColorAllocate($im, 255, 0, 255);
  175 $green  = ImageColorAllocate($im, 0, 255, 0);
  176 $blue   = ImageColorAllocate($im, 0, 0, 255);
  177 $lblue  = ImageColorAllocate($im, 128, 128, 255);
  178 $brown  = ImageColorAllocate($im, 128, 0, 0);
  179 $red    = ImageColorAllocate($im, 255, 0, 0);
  180 $black  = ImageColorAllocate($im, 0, 0, 0);
  181 
  182 for($Counter=XOFFSET+1; $Counter < $width; $Counter++)
  183     {
  184     if (isset($total[$Counter]))
  185         {
  186         // Convert the bytes/sec to y coords
  187         $total[$Counter] = ($total[$Counter]*($height-YOFFSET))/$YMax;
  188         $tcp[$Counter] = ($tcp[$Counter]*($height-YOFFSET))/$YMax;
  189         $ftp[$Counter] = ($ftp[$Counter]*($height-YOFFSET))/$YMax;
  190         $http[$Counter] = ($http[$Counter]*($height-YOFFSET))/$YMax;
  191         $p2p[$Counter] = ($p2p[$Counter]*($height-YOFFSET))/$YMax;
  192         $udp[$Counter] = ($udp[$Counter]*($height-YOFFSET))/$YMax;
  193         $icmp[$Counter] = ($icmp[$Counter]*($height-YOFFSET))/$YMax;
  194 
  195         // Stack 'em up!
  196         // Total is stacked from the bottom
  197         // Icmp is on the bottom too
  198         // Udp is stacked on top of icmp
  199         $udp[$Counter] += $icmp[$Counter];
  200         // TCP and p2p are stacked on top of Udp
  201         $tcp[$Counter] += $udp[$Counter];
  202         $p2p[$Counter] += $udp[$Counter];
  203         // Http is stacked on top of p2p
  204         $http[$Counter] += $p2p[$Counter];
  205         // Ftp is stacked on top of http
  206         $ftp[$Counter] += $http[$Counter];
  207 
  208         // Plot them!
  209         //echo "$Counter:".$Counter." (h-y)-t:".($height-YOFFSET) - $total[$Counter]." h-YO-1:".$height-YOFFSET-1;
  210         ImageLine($im, $Counter, ($height-YOFFSET) - $total[$Counter], $Counter, $height-YOFFSET-1, $yellow);
  211         ImageLine($im, $Counter, ($height-YOFFSET) - $icmp[$Counter], $Counter, $height-YOFFSET-1, $red);
  212         ImageLine($im, $Counter, ($height-YOFFSET) - $udp[$Counter], $Counter, ($height-YOFFSET) - $icmp[$Counter] - 1, $brown);
  213         ImageLine($im, $Counter, ($height-YOFFSET) - $tcp[$Counter], $Counter, ($height-YOFFSET) - $udp[$Counter] - 1, $green);
  214         ImageLine($im, $Counter, ($height-YOFFSET) - $p2p[$Counter], $Counter, ($height-YOFFSET) - $udp[$Counter] - 1, $purple);
  215         ImageLine($im, $Counter, ($height-YOFFSET) - $http[$Counter], $Counter, ($height-YOFFSET) - $p2p[$Counter] - 1, $blue);
  216         ImageLine($im, $Counter, ($height-YOFFSET) - $ftp[$Counter], $Counter, ($height-YOFFSET) - $http[$Counter] - 1, $lblue);
  217         }
  218 //  else
  219 //      echo $Counter." not set<br>";
  220     }                                                                                                                             
  221 
  222 // Margin Text
  223 if ($SentPeak < 1024/8)
  224     $txtPeakSendRate = sprintf("Peak Send Rate: %.1f KBits/sec", $SentPeak*8);
  225 else if ($SentPeak < (1024*1024)/8)
  226     $txtPeakSendRate = sprintf("Peak Send Rate: %.1f MBits/sec", ($SentPeak*8.0)/1024.0);
  227 else 
  228     $txtPeakSendRate = sprintf("Peak Send Rate: %.1f GBits/sec", ($SentPeak*8.0)/(1024.0*1024.0));
  229                                                                                                                              
  230 if ($TotalSent < 1024)
  231     $txtTotalSent = sprintf("Sent %.1f KBytes", $TotalSent);
  232 else if ($TotalSent < 1024*1024)
  233     $txtTotalSent = sprintf("Sent %.1f MBytes", $TotalSent/1024.0);
  234 else 
  235     $txtTotalSent = sprintf("Sent %.1f GBytes", $TotalSent/(1024.0*1024.0));
  236                                                                                                                              
  237 ImageString($im, 2, XOFFSET+5,  $height-20, $txtTotalSent, $black);
  238 ImageString($im, 2, $width/2+XOFFSET/2,  $height-20, $txtPeakSendRate, $black);
  239 
  240 // Draw X Axis
  241 
  242 ImageLine($im, 0, $height-YOFFSET, $width, $height-YOFFSET, $black);
  243 
  244 // Day/Month Seperator bars
  245 
  246 if ((24*60*60*($width-XOFFSET))/$interval > ($width-XOFFSET)/10)
  247     {
  248     $ts = getdate($timestamp);
  249     $MarkTime = mktime(0, 0, 0, $ts['mon'], $ts['mday'], $ts['year']);
  250     
  251     $x = ts2x($MarkTime);
  252     while ($x < XOFFSET)
  253         {
  254         $MarkTime += (24*60*60);
  255         $x = ts2x($MarkTime);
  256         }
  257                                                                                                                              
  258     while ($x < ($width-10))
  259         {
  260         // Day Lines
  261         ImageLine($im, $x, 0, $x, $height-YOFFSET, $black);
  262         ImageLine($im, $x+1, 0, $x+1, $height-YOFFSET, $black);
  263                                                                                                                              
  264         $txtDate = strftime("%a, %b %d", $MarkTime);
  265         ImageString($im, 2, $x-30,  $height-YOFFSET+10, $txtDate, $black);
  266                                                                                                                              
  267         // Calculate Next x
  268         $MarkTime += (24*60*60);
  269         $x = ts2x($MarkTime);
  270         }
  271     }
  272 else if ((24*60*60*30*($width-XOFFSET))/$interval > ($width-XOFFSET)/10)
  273     {
  274     // Monthly Bars
  275     $ts = getdate($timestamp);
  276     $month = $ts['mon'];
  277     $MarkTime = mktime(0, 0, 0, $month, 1, $ts['year']);
  278     
  279     $x = ts2x($MarkTime);
  280     while ($x < XOFFSET)
  281         {
  282         $month++;
  283         $MarkTime = mktime(0, 0, 0, $month, 1, $ts['year']);
  284         $x = ts2x($MarkTime);
  285         }
  286                                                                                                                              
  287     while ($x < ($width-10))
  288         {
  289         // Day Lines
  290         ImageLine($im, $x, 0, $x, $height-YOFFSET, $black);
  291         ImageLine($im, $x+1, 0, $x+1, $height-YOFFSET, $black);
  292                                                                                                                              
  293         $txtDate = strftime("%b, %Y", $MarkTime);
  294         ImageString($im, 2, $x-25,  $height-YOFFSET+10, $txtDate, $black);
  295                                                                                                                              
  296         // Calculate Next x
  297         $month++;
  298         $MarkTime = mktime(0, 0, 0, $month, 1, $ts['year']);
  299         $x = ts2x($MarkTime);
  300         }
  301     }
  302 else
  303     {
  304     // Year Bars
  305     $ts = getdate($timestamp);
  306     $year = $ts['year'];
  307     $MarkTime = mktime(0, 0, 0, 1, 1, $year);
  308                                                                                                                              
  309     $x = ts2x($MarkTime);
  310     while ($x < XOFFSET)
  311         {
  312         $year++;
  313         $MarkTime = mktime(0, 0, 0, 1, 1, $year);
  314         $x = ts2x($MarkTime);
  315         }
  316                                                                                                                              
  317     while ($x < ($width-10))
  318         {
  319         // Day Lines
  320         ImageLine($im, $x, 0, $x, $height-YOFFSET, $black);
  321         ImageLine($im, $x+1, 0, $x+1, $height-YOFFSET, $black);
  322                                                                                                                              
  323         $txtDate = strftime("%b, %Y", $MarkTime);
  324         ImageString($im, 2, $x-25,  $height-YOFFSET+10, $txtDate, $black);
  325                                                                                                                              
  326         // Calculate Next x
  327         $year++;
  328         $MarkTime = mktime(0, 0, 0, 1, 1, $year);
  329         $x = ts2x($MarkTime);
  330         }   
  331     }
  332 
  333 // Draw Major Tick Marks
  334 if ((6*60*60*($width-XOFFSET))/$interval > 10) // pixels per 6 hours is more than 2
  335     $MarkTimeStep = 6*60*60; // Major ticks are 6 hours
  336 else if ((24*60*60*($width-XOFFSET))/$interval > 10)
  337     $MarkTimeStep = 24*60*60; // Major ticks are 24 hours;
  338 else if ((24*60*60*30*($width-XOFFSET))/$interval > 10)
  339     {
  340     // Major tick marks are months
  341     $MarkTimeStep = 0; // Skip the standard way of drawing major tick marks below
  342 
  343     $ts = getdate($timestamp);
  344     $month = $ts['mon'];
  345     $MarkTime = mktime(0, 0, 0, $month, 1, $ts['year']);
  346                                                                                                                              
  347     $x = ts2x($MarkTime);
  348     while ($x < XOFFSET)
  349         {
  350         $month++;
  351         $MarkTime = mktime(0, 0, 0, $month, 1, $ts['year']);
  352         $x = ts2x($MarkTime);
  353         }
  354                                                                                                                              
  355     while ($x < ($width-10))
  356         {
  357         // Day Lines
  358         $date = getdate($MarkTime);
  359         if ($date['mon'] != 1)
  360             {
  361             ImageLine($im, $x, $height-YOFFSET-5, $x, $height-YOFFSET+5, $black);                                                                                                                      
  362             $txtDate = strftime("%b", $MarkTime);
  363             ImageString($im, 2, $x-5,  $height-YOFFSET+10, $txtDate, $black);
  364             }
  365                                                                                                                    
  366         // Calculate Next x
  367         $month++;
  368         $MarkTime = mktime(0, 0, 0, $month, 1, $ts['year']);
  369         $x = ts2x($MarkTime);
  370         }
  371     }
  372 else
  373     $MarkTimeStep = 0; // Skip Major Tick Marks
  374 
  375 if ($MarkTimeStep)
  376     {
  377     $ts = getdate($timestamp);
  378     $MarkTime = mktime(0, 0, 0, $ts['mon'], $ts['mday'], $ts['year']);
  379     $x = ts2x($MarkTime);
  380 
  381     while ($x < ($width-10))
  382         {
  383         if ($x > XOFFSET) 
  384             {
  385             ImageLine($im, $x, $height-YOFFSET-5, $x, $height-YOFFSET+5, $black);
  386             }
  387         $MarkTime += $MarkTimeStep;
  388         $x = ts2x($MarkTime);
  389         }
  390     }
  391 
  392 // Draw Minor Tick marks
  393 if ((60*60*($width-XOFFSET))/$interval > 4) // pixels per hour is more than 2
  394     $MarkTimeStep = 60*60;  // Minor ticks are 1 hour
  395 else if ((6*60*60*($width-XOFFSET))/$interval > 4)
  396     $MarkTimeStep = 6*60*60; // Minor ticks are 6 hours
  397 else if ((24*60*60*($width-XOFFSET))/$interval > 4)
  398     $MarkTimeStep = 24*60*60;
  399 else
  400     $MarkTimeStep = 0; // Skip minor tick marks
  401 
  402 if ($MarkTimeStep)
  403     {
  404     $ts = getdate($timestamp);
  405     $MarkTime = mktime(0, 0, 0, $ts['mon'], $ts['mday'], $ts['year']);
  406     $x = ts2x($MarkTime);
  407 
  408     while ($x < ($width-10))
  409         {
  410         if ($x > XOFFSET) 
  411             {
  412             ImageLine($im, $x, $height-YOFFSET, $x, $height-YOFFSET+5, $black);
  413             }
  414         $MarkTime += $MarkTimeStep;
  415         $x = ts2x($MarkTime);
  416         }
  417     }
  418 
  419 // Draw Y Axis
  420 ImageLine($im, XOFFSET, 0, XOFFSET, $height, $black);
  421 
  422 $YLegend = 'k';
  423 $Divisor = 1;
  424 if ($YMax*8 > 1024*2)
  425     {
  426     $Divisor = 1024;    // Display in m
  427     $YLegend = 'm';
  428     }
  429 
  430 if ($YMax*8 > 1024*1024*2)
  431     {
  432     $Divisor = 1024*1024; // Display in g
  433     $YLegend = 'g';
  434     }
  435 
  436 if ($YMax*8 > 1024*1024*1024*2)
  437     {
  438     $Divisor = 1024*1024*1024; // Display in t
  439     $YLegend = 't';
  440     }
  441                                                                                                                              
  442 $YStep = $YMax/10;
  443 if ($YStep < 1)
  444     $YStep=1;
  445 $YTic=$YStep;
  446                                                                                                                              
  447 while ($YTic <= ($YMax - $YMax/10))
  448     {
  449     $y = ($height-YOFFSET)-(($YTic*($height-YOFFSET))/$YMax);
  450     ImageLine($im, XOFFSET, $y, $width, $y, $black);
  451     $txtYLegend = sprintf("%4.1f %sbits/s", (8.0*$YTic)/$Divisor, $YLegend);
  452     ImageString($im, 2, 3, $y-7, $txtYLegend, $black);
  453     $YTic += $YStep;
  454     }
  455 
  456 imagepng($im); 
  457 imagedestroy($im);