2 require("include.php");
4 // Returns x location of any given timestamp
7 global $timestamp, $width, $interval;
8 return(($ts-$timestamp)*(($width-XOFFSET) / $interval) + XOFFSET);
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()
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;
17 foreach ($Count as $key => $number)
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;
28 foreach ($Count as $key => $number)
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];
38 if ($a_total[$key] > $YMax)
39 $YMax = $a_total[$key];
42 unset($GLOBALS['total'], $GLOBALS['icmp'], $GLOBALS['udp'], $GLOBALS['tcp'], $GLOBALS['ftp'], $GLOBALS['http'], $GLOBALS['p2p'], $GLOBALS['Count']);
59 if (isset($_GET['width']))
60 $width = $_GET['width'];
64 if (isset($_GET['height']))
65 $height = $_GET['height'];
67 $height = DFLT_HEIGHT;
69 if (isset($_GET['interval']))
70 $interval = $_GET['interval'];
72 $interval = DFLT_INTERVAL;
74 if (isset($_GET['ip']))
79 if (isset($_GET['sensor_name']))
80 $sensor_name = $_GET['sensor_name'];
84 if (isset($_GET['timestamp']))
85 $timestamp = $_GET['timestamp'];
87 $timestamp = time() - $interval + (0.05*$interval);
89 if (isset($_GET['table']))
90 $table = $_GET['table'];
94 if (isset($_GET['yscale']))
95 $yscale = $_GET['yscale'];
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);
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.
123 while ($row = pg_fetch_array($result))
125 if ($row['ip'] != $last_ip)
127 AverageAndAccumulate();
128 $last_ip = $row['ip'];
131 $x = ($row['ts']-$timestamp)*(($width-XOFFSET)/$interval)+XOFFSET;
134 //echo "xint: ".$xint."<br>";
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'];
149 // One more time for the last IP
150 AverageAndAccumulate();
152 // Pull the data out of Accumulator
161 $YMax += $YMax*0.05; // Add an extra 5%
163 // if a y scale was specified override YMax
169 header("Content-type: image/png");
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);
182 for($Counter=XOFFSET+1; $Counter < $width; $Counter++)
184 if (isset($total[$Counter]))
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;
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];
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);
219 // echo $Counter." not set<br>";
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);
228 $txtPeakSendRate = sprintf("Peak Send Rate: %.1f GBits/sec", ($SentPeak*8.0)/(1024.0*1024.0));
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);
235 $txtTotalSent = sprintf("Sent %.1f GBytes", $TotalSent/(1024.0*1024.0));
237 ImageString($im, 2, XOFFSET+5, $height-20, $txtTotalSent, $black);
238 ImageString($im, 2, $width/2+XOFFSET/2, $height-20, $txtPeakSendRate, $black);
242 ImageLine($im, 0, $height-YOFFSET, $width, $height-YOFFSET, $black);
244 // Day/Month Seperator bars
246 if ((24*60*60*($width-XOFFSET))/$interval > ($width-XOFFSET)/10)
248 $ts = getdate($timestamp);
249 $MarkTime = mktime(0, 0, 0, $ts['mon'], $ts['mday'], $ts['year']);
251 $x = ts2x($MarkTime);
254 $MarkTime += (24*60*60);
255 $x = ts2x($MarkTime);
258 while ($x < ($width-10))
261 ImageLine($im, $x, 0, $x, $height-YOFFSET, $black);
262 ImageLine($im, $x+1, 0, $x+1, $height-YOFFSET, $black);
264 $txtDate = strftime("%a, %b %d", $MarkTime);
265 ImageString($im, 2, $x-30, $height-YOFFSET+10, $txtDate, $black);
268 $MarkTime += (24*60*60);
269 $x = ts2x($MarkTime);
272 else if ((24*60*60*30*($width-XOFFSET))/$interval > ($width-XOFFSET)/10)
275 $ts = getdate($timestamp);
277 $MarkTime = mktime(0, 0, 0, $month, 1, $ts['year']);
279 $x = ts2x($MarkTime);
283 $MarkTime = mktime(0, 0, 0, $month, 1, $ts['year']);
284 $x = ts2x($MarkTime);
287 while ($x < ($width-10))
290 ImageLine($im, $x, 0, $x, $height-YOFFSET, $black);
291 ImageLine($im, $x+1, 0, $x+1, $height-YOFFSET, $black);
293 $txtDate = strftime("%b, %Y", $MarkTime);
294 ImageString($im, 2, $x-25, $height-YOFFSET+10, $txtDate, $black);
298 $MarkTime = mktime(0, 0, 0, $month, 1, $ts['year']);
299 $x = ts2x($MarkTime);
305 $ts = getdate($timestamp);
307 $MarkTime = mktime(0, 0, 0, 1, 1, $year);
309 $x = ts2x($MarkTime);
313 $MarkTime = mktime(0, 0, 0, 1, 1, $year);
314 $x = ts2x($MarkTime);
317 while ($x < ($width-10))
320 ImageLine($im, $x, 0, $x, $height-YOFFSET, $black);
321 ImageLine($im, $x+1, 0, $x+1, $height-YOFFSET, $black);
323 $txtDate = strftime("%b, %Y", $MarkTime);
324 ImageString($im, 2, $x-25, $height-YOFFSET+10, $txtDate, $black);
328 $MarkTime = mktime(0, 0, 0, 1, 1, $year);
329 $x = ts2x($MarkTime);
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)
340 // Major tick marks are months
341 $MarkTimeStep = 0; // Skip the standard way of drawing major tick marks below
343 $ts = getdate($timestamp);
345 $MarkTime = mktime(0, 0, 0, $month, 1, $ts['year']);
347 $x = ts2x($MarkTime);
351 $MarkTime = mktime(0, 0, 0, $month, 1, $ts['year']);
352 $x = ts2x($MarkTime);
355 while ($x < ($width-10))
358 $date = getdate($MarkTime);
359 if ($date['mon'] != 1)
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);
368 $MarkTime = mktime(0, 0, 0, $month, 1, $ts['year']);
369 $x = ts2x($MarkTime);
373 $MarkTimeStep = 0; // Skip Major Tick Marks
377 $ts = getdate($timestamp);
378 $MarkTime = mktime(0, 0, 0, $ts['mon'], $ts['mday'], $ts['year']);
379 $x = ts2x($MarkTime);
381 while ($x < ($width-10))
385 ImageLine($im, $x, $height-YOFFSET-5, $x, $height-YOFFSET+5, $black);
387 $MarkTime += $MarkTimeStep;
388 $x = ts2x($MarkTime);
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;
400 $MarkTimeStep = 0; // Skip minor tick marks
404 $ts = getdate($timestamp);
405 $MarkTime = mktime(0, 0, 0, $ts['mon'], $ts['mday'], $ts['year']);
406 $x = ts2x($MarkTime);
408 while ($x < ($width-10))
412 ImageLine($im, $x, $height-YOFFSET, $x, $height-YOFFSET+5, $black);
414 $MarkTime += $MarkTimeStep;
415 $x = ts2x($MarkTime);
420 ImageLine($im, XOFFSET, 0, XOFFSET, $height, $black);
424 if ($YMax*8 > 1024*2)
426 $Divisor = 1024; // Display in m
430 if ($YMax*8 > 1024*1024*2)
432 $Divisor = 1024*1024; // Display in g
436 if ($YMax*8 > 1024*1024*1024*2)
438 $Divisor = 1024*1024*1024; // Display in t
447 while ($YTic <= ($YMax - $YMax/10))
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);