You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

457 lines
15 KiB

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