1
0

graph.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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_subnet = prepare_sql_subnet($ip);
  95. $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;";
  96. //error_log($sql);
  97. $result = $db->query($sql);
  98. // The SQL statement pulls the data out of the database ordered by IP address, that way we can average each
  99. // datapoint for each IP address to provide smoothing and then toss the smoothed value into the accumulator
  100. // to provide accurate total traffic rate.
  101. while ($row = $result->fetch())
  102. {
  103. if ($row['ip'] != $last_ip)
  104. {
  105. AverageAndAccumulate();
  106. $last_ip = $row['ip'];
  107. }
  108. $x = ($row['ts']-$timestamp)*(($width-XOFFSET)/$interval)+XOFFSET;
  109. $xint = (int) $x;
  110. //echo "xint: ".$xint."<br>";
  111. $Count[$xint]++;
  112. if ($row['total']/$row['sample_duration'] > $SentPeak)
  113. $SentPeak = $row['total']/$row['sample_duration'];
  114. $TotalSent += $row['total'];
  115. $total[$xint] += $row['total']/$row['sample_duration'];
  116. $icmp[$xint] += $row['icmp']/$row['sample_duration'];
  117. $udp[$xint] += $row['udp']/$row['sample_duration'];
  118. $tcp[$xint] += $row['tcp']/$row['sample_duration'];
  119. $ftp[$xint] += $row['ftp']/$row['sample_duration'];
  120. $http[$xint] += $row['http']/$row['sample_duration'];
  121. $p2p[$xint] += $row['p2p']/$row['sample_duration'];
  122. }
  123. // One more time for the last IP
  124. AverageAndAccumulate();
  125. // Pull the data out of Accumulator
  126. $total = $a_total;
  127. $icmp = $a_icmp;
  128. $udp = $a_udp;
  129. $tcp = $a_tcp;
  130. $ftp = $a_ftp;
  131. $http = $a_http;
  132. $p2p = $a_p2p;
  133. $YMax += $YMax*0.05; // Add an extra 5%
  134. // if a y scale was specified override YMax
  135. if (isset($yscale)&& $yscale > 0)
  136. $YMax = $yscale/8;
  137. // Plot the data
  138. header("Content-type: image/png");
  139. $im = imagecreate($width, $height);
  140. $white = imagecolorallocate($im, 255, 255, 255);
  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) - $icmp[$Counter], $Counter, $height-YOFFSET-1, $red);
  175. ImageLine($im, $Counter, ($height-YOFFSET) - $udp[$Counter], $Counter, ($height-YOFFSET) - $icmp[$Counter] - 1, $brown);
  176. ImageLine($im, $Counter, ($height-YOFFSET) - $tcp[$Counter], $Counter, ($height-YOFFSET) - $udp[$Counter] - 1, $green);
  177. ImageLine($im, $Counter, ($height-YOFFSET) - $p2p[$Counter], $Counter, ($height-YOFFSET) - $udp[$Counter] - 1, $purple);
  178. ImageLine($im, $Counter, ($height-YOFFSET) - $http[$Counter], $Counter, ($height-YOFFSET) - $p2p[$Counter] - 1, $blue);
  179. ImageLine($im, $Counter, ($height-YOFFSET) - $ftp[$Counter], $Counter, ($height-YOFFSET) - $http[$Counter] - 1, $lblue);
  180. }
  181. // else
  182. // echo $Counter." not set<br>";
  183. }
  184. // Margin Text
  185. if ($SentPeak < 1024/8)
  186. $txtPeakSendRate = sprintf("Peak Send Rate: %.1f KBits/sec", $SentPeak*8);
  187. else if ($SentPeak < (1024*1024)/8)
  188. $txtPeakSendRate = sprintf("Peak Send Rate: %.1f MBits/sec", ($SentPeak*8.0)/1024.0);
  189. else
  190. $txtPeakSendRate = sprintf("Peak Send Rate: %.1f GBits/sec", ($SentPeak*8.0)/(1024.0*1024.0));
  191. if ($TotalSent < 1024)
  192. $txtTotalSent = sprintf("Sent %.1f KBytes", $TotalSent);
  193. else if ($TotalSent < 1024*1024)
  194. $txtTotalSent = sprintf("Sent %.1f MBytes", $TotalSent/1024.0);
  195. else
  196. $txtTotalSent = sprintf("Sent %.1f GBytes", $TotalSent/(1024.0*1024.0));
  197. ImageString($im, 2, XOFFSET+5, $height-20, $txtTotalSent, $black);
  198. ImageString($im, 2, $width/2+XOFFSET/2, $height-20, $txtPeakSendRate, $black);
  199. // Draw X Axis
  200. ImageLine($im, 0, $height-YOFFSET, $width, $height-YOFFSET, $black);
  201. // Day/Month Seperator bars
  202. if ((24*60*60*($width-XOFFSET))/$interval > ($width-XOFFSET)/10)
  203. {
  204. $ts = getdate($timestamp);
  205. $MarkTime = mktime(0, 0, 0, $ts['mon'], $ts['mday'], $ts['year']);
  206. $x = ts2x($MarkTime);
  207. while ($x < XOFFSET)
  208. {
  209. $MarkTime += (24*60*60);
  210. $x = ts2x($MarkTime);
  211. }
  212. while ($x < ($width-10))
  213. {
  214. // Day Lines
  215. ImageLine($im, $x, 0, $x, $height-YOFFSET, $black);
  216. ImageLine($im, $x+1, 0, $x+1, $height-YOFFSET, $black);
  217. $txtDate = strftime("%a, %b %d", $MarkTime);
  218. ImageString($im, 2, $x-30, $height-YOFFSET+10, $txtDate, $black);
  219. // Calculate Next x
  220. $MarkTime += (24*60*60);
  221. $x = ts2x($MarkTime);
  222. }
  223. }
  224. else if ((24*60*60*30*($width-XOFFSET))/$interval > ($width-XOFFSET)/10)
  225. {
  226. // Monthly Bars
  227. $ts = getdate($timestamp);
  228. $month = $ts['mon'];
  229. $MarkTime = mktime(0, 0, 0, $month, 1, $ts['year']);
  230. $x = ts2x($MarkTime);
  231. while ($x < XOFFSET)
  232. {
  233. $month++;
  234. $MarkTime = mktime(0, 0, 0, $month, 1, $ts['year']);
  235. $x = ts2x($MarkTime);
  236. }
  237. while ($x < ($width-10))
  238. {
  239. // Day Lines
  240. ImageLine($im, $x, 0, $x, $height-YOFFSET, $black);
  241. ImageLine($im, $x+1, 0, $x+1, $height-YOFFSET, $black);
  242. $txtDate = strftime("%b, %Y", $MarkTime);
  243. ImageString($im, 2, $x-25, $height-YOFFSET+10, $txtDate, $black);
  244. // Calculate Next x
  245. $month++;
  246. $MarkTime = mktime(0, 0, 0, $month, 1, $ts['year']);
  247. $x = ts2x($MarkTime);
  248. }
  249. }
  250. else
  251. {
  252. // Year Bars
  253. $ts = getdate($timestamp);
  254. $year = $ts['year'];
  255. $MarkTime = mktime(0, 0, 0, 1, 1, $year);
  256. $x = ts2x($MarkTime);
  257. while ($x < XOFFSET)
  258. {
  259. $year++;
  260. $MarkTime = mktime(0, 0, 0, 1, 1, $year);
  261. $x = ts2x($MarkTime);
  262. }
  263. while ($x < ($width-10))
  264. {
  265. // Day Lines
  266. ImageLine($im, $x, 0, $x, $height-YOFFSET, $black);
  267. ImageLine($im, $x+1, 0, $x+1, $height-YOFFSET, $black);
  268. $txtDate = strftime("%b, %Y", $MarkTime);
  269. ImageString($im, 2, $x-25, $height-YOFFSET+10, $txtDate, $black);
  270. // Calculate Next x
  271. $year++;
  272. $MarkTime = mktime(0, 0, 0, 1, 1, $year);
  273. $x = ts2x($MarkTime);
  274. }
  275. }
  276. // Draw Major Tick Marks
  277. if ((6*60*60*($width-XOFFSET))/$interval > 10) // pixels per 6 hours is more than 2
  278. $MarkTimeStep = 6*60*60; // Major ticks are 6 hours
  279. else if ((24*60*60*($width-XOFFSET))/$interval > 10)
  280. $MarkTimeStep = 24*60*60; // Major ticks are 24 hours;
  281. else if ((24*60*60*30*($width-XOFFSET))/$interval > 10)
  282. {
  283. // Major tick marks are months
  284. $MarkTimeStep = 0; // Skip the standard way of drawing major tick marks below
  285. $ts = getdate($timestamp);
  286. $month = $ts['mon'];
  287. $MarkTime = mktime(0, 0, 0, $month, 1, $ts['year']);
  288. $x = ts2x($MarkTime);
  289. while ($x < XOFFSET)
  290. {
  291. $month++;
  292. $MarkTime = mktime(0, 0, 0, $month, 1, $ts['year']);
  293. $x = ts2x($MarkTime);
  294. }
  295. while ($x < ($width-10))
  296. {
  297. // Day Lines
  298. $date = getdate($MarkTime);
  299. if ($date['mon'] != 1)
  300. {
  301. ImageLine($im, $x, $height-YOFFSET-5, $x, $height-YOFFSET+5, $black);
  302. $txtDate = strftime("%b", $MarkTime);
  303. ImageString($im, 2, $x-5, $height-YOFFSET+10, $txtDate, $black);
  304. }
  305. // Calculate Next x
  306. $month++;
  307. $MarkTime = mktime(0, 0, 0, $month, 1, $ts['year']);
  308. $x = ts2x($MarkTime);
  309. }
  310. }
  311. else
  312. $MarkTimeStep = 0; // Skip Major Tick Marks
  313. if ($MarkTimeStep)
  314. {
  315. $ts = getdate($timestamp);
  316. $MarkTime = mktime(0, 0, 0, $ts['mon'], $ts['mday'], $ts['year']);
  317. $x = ts2x($MarkTime);
  318. while ($x < ($width-10))
  319. {
  320. if ($x > XOFFSET)
  321. {
  322. ImageLine($im, $x, $height-YOFFSET-5, $x, $height-YOFFSET+5, $black);
  323. }
  324. $MarkTime += $MarkTimeStep;
  325. $x = ts2x($MarkTime);
  326. }
  327. }
  328. // Draw Minor Tick marks
  329. if ((60*60*($width-XOFFSET))/$interval > 4) // pixels per hour is more than 2
  330. $MarkTimeStep = 60*60; // Minor ticks are 1 hour
  331. else if ((6*60*60*($width-XOFFSET))/$interval > 4)
  332. $MarkTimeStep = 6*60*60; // Minor ticks are 6 hours
  333. else if ((24*60*60*($width-XOFFSET))/$interval > 4)
  334. $MarkTimeStep = 24*60*60;
  335. else
  336. $MarkTimeStep = 0; // Skip minor tick marks
  337. if ($MarkTimeStep)
  338. {
  339. $ts = getdate($timestamp);
  340. $MarkTime = mktime(0, 0, 0, $ts['mon'], $ts['mday'], $ts['year']);
  341. $x = ts2x($MarkTime);
  342. while ($x < ($width-10))
  343. {
  344. if ($x > XOFFSET)
  345. {
  346. ImageLine($im, $x, $height-YOFFSET, $x, $height-YOFFSET+5, $black);
  347. }
  348. $MarkTime += $MarkTimeStep;
  349. $x = ts2x($MarkTime);
  350. }
  351. }
  352. // Draw Y Axis
  353. ImageLine($im, XOFFSET, 0, XOFFSET, $height, $black);
  354. $YLegend = 'k';
  355. $Divisor = 1;
  356. if ($YMax*8 > 1024*2)
  357. {
  358. $Divisor = 1024; // Display in m
  359. $YLegend = 'm';
  360. }
  361. if ($YMax*8 > 1024*1024*2)
  362. {
  363. $Divisor = 1024*1024; // Display in g
  364. $YLegend = 'g';
  365. }
  366. if ($YMax*8 > 1024*1024*1024*2)
  367. {
  368. $Divisor = 1024*1024*1024; // Display in t
  369. $YLegend = 't';
  370. }
  371. $YStep = $YMax/10;
  372. if ($YStep < 1)
  373. $YStep=1;
  374. $YTic=$YStep;
  375. while ($YTic <= ($YMax - $YMax/10))
  376. {
  377. $y = ($height-YOFFSET)-(($YTic*($height-YOFFSET))/$YMax);
  378. ImageLine($im, XOFFSET, $y, $width, $y, $black);
  379. $txtYLegend = sprintf("%4.1f %sbits/s", (8.0*$YTic)/$Divisor, $YLegend);
  380. ImageString($im, 2, 3, $y-7, $txtYLegend, $black);
  381. $YTic += $YStep;
  382. }
  383. imagepng($im);
  384. imagedestroy($im);