graph.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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. $purple = ImageColorAllocate($im, 255, 0, 255);
  141. $green = ImageColorAllocate($im, 0, 255, 0);
  142. $blue = ImageColorAllocate($im, 0, 0, 255);
  143. $lblue = ImageColorAllocate($im, 128, 128, 255);
  144. $brown = ImageColorAllocate($im, 128, 0, 0);
  145. $red = ImageColorAllocate($im, 255, 0, 0);
  146. $black = ImageColorAllocate($im, 0, 0, 0);
  147. for($Counter=XOFFSET+1; $Counter < $width; $Counter++)
  148. {
  149. if (isset($total[$Counter]))
  150. {
  151. // Convert the bytes/sec to y coords
  152. $total[$Counter] = ($total[$Counter]*($height-YOFFSET))/$YMax;
  153. $tcp[$Counter] = ($tcp[$Counter]*($height-YOFFSET))/$YMax;
  154. $ftp[$Counter] = ($ftp[$Counter]*($height-YOFFSET))/$YMax;
  155. $http[$Counter] = ($http[$Counter]*($height-YOFFSET))/$YMax;
  156. $p2p[$Counter] = ($p2p[$Counter]*($height-YOFFSET))/$YMax;
  157. $udp[$Counter] = ($udp[$Counter]*($height-YOFFSET))/$YMax;
  158. $icmp[$Counter] = ($icmp[$Counter]*($height-YOFFSET))/$YMax;
  159. // Stack 'em up!
  160. // Total is stacked from the bottom
  161. // Icmp is on the bottom too
  162. // Udp is stacked on top of icmp
  163. $udp[$Counter] += $icmp[$Counter];
  164. // TCP and p2p are stacked on top of Udp
  165. $tcp[$Counter] += $udp[$Counter];
  166. $p2p[$Counter] += $udp[$Counter];
  167. // Http is stacked on top of p2p
  168. $http[$Counter] += $p2p[$Counter];
  169. // Ftp is stacked on top of http
  170. $ftp[$Counter] += $http[$Counter];
  171. // Plot them!
  172. //echo "$Counter:".$Counter." (h-y)-t:".($height-YOFFSET) - $total[$Counter]." h-YO-1:".$height-YOFFSET-1;
  173. ImageLine($im, $Counter, ($height-YOFFSET) - $icmp[$Counter], $Counter, $height-YOFFSET-1, $red);
  174. ImageLine($im, $Counter, ($height-YOFFSET) - $udp[$Counter], $Counter, ($height-YOFFSET) - $icmp[$Counter] - 1, $brown);
  175. ImageLine($im, $Counter, ($height-YOFFSET) - $tcp[$Counter], $Counter, ($height-YOFFSET) - $udp[$Counter] - 1, $green);
  176. ImageLine($im, $Counter, ($height-YOFFSET) - $p2p[$Counter], $Counter, ($height-YOFFSET) - $udp[$Counter] - 1, $purple);
  177. ImageLine($im, $Counter, ($height-YOFFSET) - $http[$Counter], $Counter, ($height-YOFFSET) - $p2p[$Counter] - 1, $blue);
  178. ImageLine($im, $Counter, ($height-YOFFSET) - $ftp[$Counter], $Counter, ($height-YOFFSET) - $http[$Counter] - 1, $lblue);
  179. }
  180. // else
  181. // echo $Counter." not set<br>";
  182. }
  183. // Margin Text
  184. if ($SentPeak < 1024/8)
  185. $txtPeakSendRate = sprintf("Peak Send Rate: %.1f KBits/sec", $SentPeak*8);
  186. else if ($SentPeak < (1024*1024)/8)
  187. $txtPeakSendRate = sprintf("Peak Send Rate: %.1f MBits/sec", ($SentPeak*8.0)/1024.0);
  188. else
  189. $txtPeakSendRate = sprintf("Peak Send Rate: %.1f GBits/sec", ($SentPeak*8.0)/(1024.0*1024.0));
  190. if ($TotalSent < 1024)
  191. $txtTotalSent = sprintf("Sent %.1f KBytes", $TotalSent);
  192. else if ($TotalSent < 1024*1024)
  193. $txtTotalSent = sprintf("Sent %.1f MBytes", $TotalSent/1024.0);
  194. else
  195. $txtTotalSent = sprintf("Sent %.1f GBytes", $TotalSent/(1024.0*1024.0));
  196. ImageString($im, 2, XOFFSET+5, $height-20, $txtTotalSent, $black);
  197. ImageString($im, 2, $width/2+XOFFSET/2, $height-20, $txtPeakSendRate, $black);
  198. // Draw X Axis
  199. ImageLine($im, 0, $height-YOFFSET, $width, $height-YOFFSET, $black);
  200. // Day/Month Seperator bars
  201. if ((24*60*60*($width-XOFFSET))/$interval > ($width-XOFFSET)/10)
  202. {
  203. $ts = getdate($timestamp);
  204. $MarkTime = mktime(0, 0, 0, $ts['mon'], $ts['mday'], $ts['year']);
  205. $x = ts2x($MarkTime);
  206. while ($x < XOFFSET)
  207. {
  208. $MarkTime += (24*60*60);
  209. $x = ts2x($MarkTime);
  210. }
  211. while ($x < ($width-10))
  212. {
  213. // Day Lines
  214. ImageLine($im, $x, 0, $x, $height-YOFFSET, $black);
  215. ImageLine($im, $x+1, 0, $x+1, $height-YOFFSET, $black);
  216. $txtDate = strftime("%a, %b %d", $MarkTime);
  217. ImageString($im, 2, $x-30, $height-YOFFSET+10, $txtDate, $black);
  218. // Calculate Next x
  219. $MarkTime += (24*60*60);
  220. $x = ts2x($MarkTime);
  221. }
  222. }
  223. else if ((24*60*60*30*($width-XOFFSET))/$interval > ($width-XOFFSET)/10)
  224. {
  225. // Monthly Bars
  226. $ts = getdate($timestamp);
  227. $month = $ts['mon'];
  228. $MarkTime = mktime(0, 0, 0, $month, 1, $ts['year']);
  229. $x = ts2x($MarkTime);
  230. while ($x < XOFFSET)
  231. {
  232. $month++;
  233. $MarkTime = mktime(0, 0, 0, $month, 1, $ts['year']);
  234. $x = ts2x($MarkTime);
  235. }
  236. while ($x < ($width-10))
  237. {
  238. // Day Lines
  239. ImageLine($im, $x, 0, $x, $height-YOFFSET, $black);
  240. ImageLine($im, $x+1, 0, $x+1, $height-YOFFSET, $black);
  241. $txtDate = strftime("%b, %Y", $MarkTime);
  242. ImageString($im, 2, $x-25, $height-YOFFSET+10, $txtDate, $black);
  243. // Calculate Next x
  244. $month++;
  245. $MarkTime = mktime(0, 0, 0, $month, 1, $ts['year']);
  246. $x = ts2x($MarkTime);
  247. }
  248. }
  249. else
  250. {
  251. // Year Bars
  252. $ts = getdate($timestamp);
  253. $year = $ts['year'];
  254. $MarkTime = mktime(0, 0, 0, 1, 1, $year);
  255. $x = ts2x($MarkTime);
  256. while ($x < XOFFSET)
  257. {
  258. $year++;
  259. $MarkTime = mktime(0, 0, 0, 1, 1, $year);
  260. $x = ts2x($MarkTime);
  261. }
  262. while ($x < ($width-10))
  263. {
  264. // Day Lines
  265. ImageLine($im, $x, 0, $x, $height-YOFFSET, $black);
  266. ImageLine($im, $x+1, 0, $x+1, $height-YOFFSET, $black);
  267. $txtDate = strftime("%b, %Y", $MarkTime);
  268. ImageString($im, 2, $x-25, $height-YOFFSET+10, $txtDate, $black);
  269. // Calculate Next x
  270. $year++;
  271. $MarkTime = mktime(0, 0, 0, 1, 1, $year);
  272. $x = ts2x($MarkTime);
  273. }
  274. }
  275. // Draw Major Tick Marks
  276. if ((6*60*60*($width-XOFFSET))/$interval > 10) // pixels per 6 hours is more than 2
  277. $MarkTimeStep = 6*60*60; // Major ticks are 6 hours
  278. else if ((24*60*60*($width-XOFFSET))/$interval > 10)
  279. $MarkTimeStep = 24*60*60; // Major ticks are 24 hours;
  280. else if ((24*60*60*30*($width-XOFFSET))/$interval > 10)
  281. {
  282. // Major tick marks are months
  283. $MarkTimeStep = 0; // Skip the standard way of drawing major tick marks below
  284. $ts = getdate($timestamp);
  285. $month = $ts['mon'];
  286. $MarkTime = mktime(0, 0, 0, $month, 1, $ts['year']);
  287. $x = ts2x($MarkTime);
  288. while ($x < XOFFSET)
  289. {
  290. $month++;
  291. $MarkTime = mktime(0, 0, 0, $month, 1, $ts['year']);
  292. $x = ts2x($MarkTime);
  293. }
  294. while ($x < ($width-10))
  295. {
  296. // Day Lines
  297. $date = getdate($MarkTime);
  298. if ($date['mon'] != 1)
  299. {
  300. ImageLine($im, $x, $height-YOFFSET-5, $x, $height-YOFFSET+5, $black);
  301. $txtDate = strftime("%b", $MarkTime);
  302. ImageString($im, 2, $x-5, $height-YOFFSET+10, $txtDate, $black);
  303. }
  304. // Calculate Next x
  305. $month++;
  306. $MarkTime = mktime(0, 0, 0, $month, 1, $ts['year']);
  307. $x = ts2x($MarkTime);
  308. }
  309. }
  310. else
  311. $MarkTimeStep = 0; // Skip Major Tick Marks
  312. if ($MarkTimeStep)
  313. {
  314. $ts = getdate($timestamp);
  315. $MarkTime = mktime(0, 0, 0, $ts['mon'], $ts['mday'], $ts['year']);
  316. $x = ts2x($MarkTime);
  317. while ($x < ($width-10))
  318. {
  319. if ($x > XOFFSET)
  320. {
  321. ImageLine($im, $x, $height-YOFFSET-5, $x, $height-YOFFSET+5, $black);
  322. }
  323. $MarkTime += $MarkTimeStep;
  324. $x = ts2x($MarkTime);
  325. }
  326. }
  327. // Draw Minor Tick marks
  328. if ((60*60*($width-XOFFSET))/$interval > 4) // pixels per hour is more than 2
  329. $MarkTimeStep = 60*60; // Minor ticks are 1 hour
  330. else if ((6*60*60*($width-XOFFSET))/$interval > 4)
  331. $MarkTimeStep = 6*60*60; // Minor ticks are 6 hours
  332. else if ((24*60*60*($width-XOFFSET))/$interval > 4)
  333. $MarkTimeStep = 24*60*60;
  334. else
  335. $MarkTimeStep = 0; // Skip minor tick marks
  336. if ($MarkTimeStep)
  337. {
  338. $ts = getdate($timestamp);
  339. $MarkTime = mktime(0, 0, 0, $ts['mon'], $ts['mday'], $ts['year']);
  340. $x = ts2x($MarkTime);
  341. while ($x < ($width-10))
  342. {
  343. if ($x > XOFFSET)
  344. {
  345. ImageLine($im, $x, $height-YOFFSET, $x, $height-YOFFSET+5, $black);
  346. }
  347. $MarkTime += $MarkTimeStep;
  348. $x = ts2x($MarkTime);
  349. }
  350. }
  351. // Draw Y Axis
  352. ImageLine($im, XOFFSET, 0, XOFFSET, $height, $black);
  353. $YLegend = 'k';
  354. $Divisor = 1;
  355. if ($YMax*8 > 1024*2)
  356. {
  357. $Divisor = 1024; // Display in m
  358. $YLegend = 'm';
  359. }
  360. if ($YMax*8 > 1024*1024*2)
  361. {
  362. $Divisor = 1024*1024; // Display in g
  363. $YLegend = 'g';
  364. }
  365. if ($YMax*8 > 1024*1024*1024*2)
  366. {
  367. $Divisor = 1024*1024*1024; // Display in t
  368. $YLegend = 't';
  369. }
  370. $YStep = $YMax/10;
  371. if ($YStep < 1)
  372. $YStep=1;
  373. $YTic=$YStep;
  374. while ($YTic <= ($YMax - $YMax/10))
  375. {
  376. $y = ($height-YOFFSET)-(($YTic*($height-YOFFSET))/$YMax);
  377. ImageLine($im, XOFFSET, $y, $width, $y, $black);
  378. $txtYLegend = sprintf("%4.1f %sbits/s", (8.0*$YTic)/$Divisor, $YLegend);
  379. ImageString($im, 2, 3, $y-7, $txtYLegend, $black);
  380. $YTic += $YStep;
  381. }
  382. imagepng($im);
  383. imagedestroy($im);