PageRenderTime 52ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/webfrontend/www/functions.inc.php

https://github.com/mobilipia/IPTV-Analyzer
PHP | 1280 lines | 908 code | 196 blank | 176 comment | 102 complexity | a9f302b6ee9df5ab0bcb3b55fe931c4a MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php /* -*- Mode: php; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  2. #define("CFGFILE", "/etc/tvprobe/webfrontend.ini");
  3. #define("CFGFILE", "../webfrontend.ini");
  4. define("CFGFILE", "webfrontend.ini");
  5. function getMicroTime()
  6. {
  7. list($usec, $sec) = explode(' ', microtime());
  8. return ((float)$usec + (float)$sec);
  9. #$text='Generated in '. sprintf('%0.3f', $timeEnd - $timeStart) . ' sec';
  10. }
  11. function displayTimingInfo($timeStart, $timeEnd, $display=TRUE, $infotxt=NULL)
  12. {
  13. $text='Elapsed time '. sprintf('%0.3f', $timeEnd - $timeStart) . ' sec';
  14. if (!$display) {
  15. echo "<!--\n";
  16. }
  17. echo "<br>";
  18. if ($infotxt) {
  19. echo "($infotxt) ";
  20. }
  21. echo "$text<br>\n";
  22. if (!$display) {
  23. echo "\n-->";
  24. }
  25. }
  26. # Function that shows secs in a human-readable format
  27. # coded by Jesper N Henriksen <jnh@comx.dk>
  28. function timespan($sum)
  29. {
  30. $seconds=$sum%60;
  31. $minutes=$sum/60%60;
  32. $hours=$sum/3600%24;
  33. $days=floor($sum/86400);
  34. $str_parts=array();
  35. if($days)
  36. {
  37. $str_parts[]=$days.' day'.($days==1?'':'s');
  38. }
  39. if($hours)
  40. {
  41. $str_parts[]=$hours.' hour'.($hours==1?'':'s');
  42. }
  43. if($minutes)
  44. {
  45. $str_parts[]=$minutes.' minute'.($minutes==1?'':'s');
  46. }
  47. if($seconds)
  48. {
  49. $str_parts[]=$seconds.' second'.($seconds==1?'':'s');
  50. }
  51. return(implode(', ',$str_parts));
  52. }
  53. function load_config($file = CFGFILE)
  54. {
  55. global $CONFIG;
  56. /* Here the process_sections parameter is set to TRUE. Thus, a
  57. multidimensional array is returned, with the section names and
  58. settings included.
  59. */
  60. $CONFIG = parse_ini_file($file, TRUE);
  61. /* TODO: Validate contents... here so its not needed everytime
  62. where its used. */
  63. return $CONFIG;
  64. }
  65. function config_read_value($key, $subkey)
  66. {
  67. global $CONFIG;
  68. #echo "<br>" . __FUNCTION__ . ":"; print_r($CONFIG);
  69. if (!is_array($CONFIG)) {
  70. $err = "Must load config file before using this function!";
  71. trigger_error("$err", E_USER_ERROR);
  72. }
  73. if (!is_array($CONFIG[$key])) {
  74. $err = "Config is missing section [$key]";
  75. trigger_error("$err", E_USER_ERROR);
  76. }
  77. $value = $CONFIG[$key][$subkey];
  78. if (!isset($value)) {
  79. $err = "Config error, cannot read value [$key][$subkey]";
  80. trigger_error("$err", E_USER_ERROR);
  81. }
  82. return $value;
  83. }
  84. # TODO: Look into using PHP API 'mysqli' instead of 'mysql',
  85. # as it allows usage of mysql prepare and execute statements.
  86. function db_connect()
  87. {
  88. global $CONFIG;
  89. global $DBH;
  90. /* load the config file if not loaded */
  91. if (!is_array($CONFIG)) {
  92. $config = load_config();
  93. if (!is_array($CONFIG)) {
  94. $err = "No config file loaded!";
  95. trigger_error("$err", E_USER_ERROR);
  96. }
  97. }
  98. #echo "<br>config:"; print_r($config);
  99. #echo "<br>CONFIG:"; print_r($CONFIG);
  100. $key = 'DB droplog';
  101. if (!is_array($config[$key])) {
  102. $err = "Config MUST have a database section [$key]";
  103. trigger_error("$err", E_USER_ERROR);
  104. }
  105. $dbhost = config_read_value($key, 'dbhost');
  106. $dbname = config_read_value($key, 'dbname');
  107. $dbuser = config_read_value($key, 'dbuser');
  108. $dbpass = config_read_value($key, 'dbpass');
  109. $DBH = mysql_connect($dbhost, $dbuser, $dbpass);
  110. if (!$DBH) {
  111. die('Could not connect: ' . mysql_error());
  112. }
  113. //echo 'Connected successfully<br>';
  114. $db_selected = mysql_select_db($dbname, $DBH);
  115. if (!$db_selected) {
  116. die ("Unable to use dbname:$dbname err: " . mysql_error());
  117. }
  118. return $DBH;
  119. }
  120. function db_disconnect($link=NULL)
  121. {
  122. if (isset($link)) {
  123. mysql_close($link);
  124. } else {
  125. mysql_close();
  126. }
  127. }
  128. function cleanup_input()
  129. {
  130. //Q: Does this work??? as the values are not returned?!
  131. // rephrased; do we really modify the global values?
  132. // Cleanup input
  133. $_GET = array_map('trim', $_GET);
  134. $_POST = array_map('trim', $_POST);
  135. $_COOKIE = array_map('trim', $_COOKIE);
  136. $_REQUEST = array_map('trim', $_REQUEST);
  137. if(get_magic_quotes_gpc()):
  138. $_GET = array_map('stripslashes', $_GET);
  139. $_POST = array_map('stripslashes', $_POST);
  140. $_COOKIE = array_map('stripslashes', $_COOKIE);
  141. $_REQUEST = array_map('stripslashes', $_REQUEST);
  142. endif;
  143. }
  144. function cleanup_input_request()
  145. {
  146. $_REQUEST = array_map('trim', $_REQUEST);
  147. if(get_magic_quotes_gpc()):
  148. $_REQUEST = array_map('stripslashes', $_REQUEST);
  149. endif;
  150. return $_REQUEST;
  151. }
  152. #####
  153. # Helper function for generating a "BETWEEN" query string
  154. #
  155. function helper_between_query($fromT=NULL, $toT=NULL, $interval=NULL, $col_name="record_time")
  156. {
  157. $statement = "";
  158. $valid = false;
  159. $between = " AND $col_name BETWEEN ";
  160. if (isset($fromT)) {
  161. $between .= "'" . mysql_real_escape_string($fromT) . "' AND ";
  162. if (isset($toT)) {
  163. $to = sprintf(" '%s' ", mysql_real_escape_string($toT));
  164. $between .= $to;
  165. $valid = true;
  166. }
  167. elseif (isset($interval)) {
  168. $between .= "DATE_ADD($col_name, INTERVAL $interval)";
  169. $valid = true;
  170. }
  171. } else {
  172. # From time is NOT set
  173. if (isset($toT) || isset($interval)) {
  174. echo "ERROR: Need \$fromT in order to use \$toT or \$interval<br>\n";
  175. }
  176. }
  177. if ($valid == true) {
  178. $statement = $between;
  179. }
  180. return $statement;
  181. }
  182. #####
  183. # Probe level data
  184. function probe_data_query_ts($probeid, $bucketsz=86400,
  185. $tsFrom=NULL, $tsTo=NULL)
  186. {
  187. if (is_numeric($tsFrom)) {
  188. $fromT = date('Y-m-d H:i:s', $tsFrom);
  189. }
  190. if (is_numeric($tsTo)) {
  191. $toT = date('Y-m-d H:i:s', $tsTo);
  192. }
  193. $result = & probe_data_query($probeid, $bucketsz, $fromT, $toT);
  194. return $result;
  195. }
  196. function probe_data_query($probeid, $bucketsz=86400,
  197. $fromT=NULL, $toT=NULL, $interval=NULL)
  198. {
  199. // Query
  200. $sec = $bucketsz;
  201. #$sec = 4 * 60 * 60;
  202. $query2 = sprintf(
  203. "SELECT UNIX_TIMESTAMP(record_time) DIV $sec as bucket,
  204. sum(delta_skips) as skips,
  205. sum(delta_discon) as drops,
  206. probes.name,
  207. UNIX_TIMESTAMP(record_time) as timestamp,
  208. UNIX_TIMESTAMP(min(record_time)) as timemin,
  209. UNIX_TIMESTAMP(max(record_time)) as timemax,
  210. TIMESTAMPDIFF(SECOND, min(record_time), max(record_time)) as period,
  211. count(record_time) as records
  212. FROM log_event, probes
  213. WHERE probes.id = probe_id
  214. AND probes.id = '%s' ",
  215. mysql_real_escape_string($probeid));
  216. $query2 .= query_str_remove_channels();
  217. $query2 .= helper_between_query($fromT, $toT, $interval, "record_time");
  218. $query2 .= " GROUP BY bucket";
  219. #print_r($query2);
  220. $db_result = mysql_query($query2);
  221. if (!$db_result) {
  222. $message = 'Invalid query: ' . mysql_error() . "\n<br>";
  223. $message .= 'Whole query: ' . $query2;
  224. die($message);
  225. }
  226. # DEBUG
  227. #echo $query2 . "<br>";
  228. $result = array();
  229. while ($row = mysql_fetch_assoc($db_result)) {
  230. #$id = $row['datoen'];
  231. #$result[$id] = $row;
  232. array_push($result, $row);
  233. }
  234. return $result;
  235. }
  236. function probe_data_show_table($result)
  237. {
  238. echo "<TABLE border=\"1\">";
  239. echo "<TR>";
  240. echo "<TD>bucket</TD>";
  241. echo "<TD>date</TD>";
  242. echo "<TD>timestamp</TD>";
  243. echo "<TD>timemin</TD>";
  244. echo "<TD>timemax</TD>";
  245. echo "<TD>period</TD>";
  246. echo "<TD>probename</TD>";
  247. echo "<TD>skips</TD>";
  248. echo "<TD>drops</TD>";
  249. echo "</TR>";
  250. foreach ($result as $row) {
  251. echo "<TR>";
  252. echo "<TD>" . $row['bucket'] . "</TD>";
  253. echo "<TD>" . date('Y-m-d H:i:s', $row['timestamp']) . "</TD>";
  254. echo "<TD>" . $row['timestamp'] . "</TD>";
  255. echo "<TD>" . $row['timemin'] . "</TD>";
  256. echo "<TD>" . $row['timemax'] . "</TD>";
  257. echo "<TD>" . $row['period'] . "</TD>";
  258. echo "<TD>" . $row['name'] . "</TD>";
  259. echo "<TD>" . $row['skips'] . "</TD>";
  260. echo "<TD>" . $row['drops'] . "</TD>";
  261. echo "</TR>\n";
  262. }
  263. echo "</TABLE>\n\n";
  264. }
  265. #####
  266. # Multicast channels per probe in a given time period
  267. # GROUP BY multicast_dst
  268. #
  269. function multicast_probe_data_query_ts($probeid,
  270. $tsFrom=NULL, $tsTo=NULL)
  271. {
  272. if (is_numeric($tsFrom)) {
  273. $fromT = date('Y-m-d H:i:s', $tsFrom);
  274. }
  275. if (is_numeric($tsTo)) {
  276. $toT = date('Y-m-d H:i:s', $tsTo);
  277. }
  278. $result = & multicast_probe_data_query($probeid, $fromT, $toT);
  279. return $result;
  280. }
  281. function multicast_probe_data_query($probeid,
  282. $fromT=NULL, $toT=NULL, $interval=NULL)
  283. {
  284. // Query
  285. $query = sprintf(
  286. "SELECT multicast_dst, count(multicast_dst) as records,
  287. sum(delta_skips) as skips,
  288. sum(delta_discon) as drops,
  289. probes.name, probes.switch, probes.id, ip_src
  290. FROM log_event, probes
  291. WHERE probes.id = probe_id
  292. AND delta_discon > 0
  293. AND probes.id = '%s' ",
  294. mysql_real_escape_string($probeid));
  295. $query .= helper_between_query($fromT, $toT, $interval, "record_time");
  296. $query .= " GROUP BY multicast_dst";
  297. $query .= " ORDER BY drops DESC";
  298. //print_r($query);
  299. $db_result = mysql_query($query);
  300. if (!$db_result) {
  301. $message = 'Invalid query: ' . mysql_error() . "\n<br>";
  302. $message .= 'Whole query: ' . $query;
  303. die($message);
  304. }
  305. $result = array();
  306. while ($row = mysql_fetch_assoc($db_result)) {
  307. #$id = $row['datoen'];
  308. #$result[$id] = $row;
  309. array_push($result, $row);
  310. }
  311. return $result;
  312. }
  313. #####
  314. # Selecting a single multicast channel in a given time period
  315. # with the possible for limiting it per probeid.
  316. #
  317. function one_channel_data_query_ts($channel, $probeid=NULL, $bucketsz=3600,
  318. $tsFrom=NULL, $tsTo=NULL)
  319. {
  320. if (is_numeric($tsFrom))
  321. $fromT = date('Y-m-d H:i:s', $tsFrom);
  322. if (is_numeric($tsTo))
  323. $toT = date('Y-m-d H:i:s', $tsTo);
  324. $result = & one_channel_data_query($channel, $probeid, $bucketsz,
  325. $fromT, $toT);
  326. return $result;
  327. }
  328. function one_channel_data_query($channel, $probeid=NULL, $bucketsz=3600,
  329. $fromT=NULL, $toT=NULL, $interval=NULL)
  330. {
  331. // Query
  332. $query = sprintf(
  333. "SELECT UNIX_TIMESTAMP(record_time) DIV $bucketsz as bucket,
  334. probe_id,
  335. multicast_dst,
  336. sum(delta_skips) as skips,
  337. sum(delta_discon) as drops,
  338. count(multicast_dst) as records,
  339. UNIX_TIMESTAMP(record_time) as timestamp,
  340. UNIX_TIMESTAMP(min(record_time)) as timemin,
  341. UNIX_TIMESTAMP(max(record_time)) as timemax,
  342. TIMESTAMPDIFF(SECOND, min(record_time), max(record_time)) as period
  343. FROM log_event
  344. WHERE multicast_dst = '%s' ",
  345. mysql_real_escape_string($channel));
  346. // Limit to a probeid if given as argument
  347. if (is_numeric($probeid)) {
  348. $query .= sprintf(" AND probe_id = '%s' ",
  349. mysql_real_escape_string($probeid));
  350. }
  351. $query .= helper_between_query($fromT, $toT, $interval, "record_time");
  352. $query .= " GROUP BY bucket";
  353. $query .= " ORDER BY probe_id, timestamp";
  354. //print_r($query);
  355. $db_result = mysql_query($query);
  356. if (!$db_result) {
  357. $message = 'Invalid query: ' . mysql_error() . "\n<br>";
  358. $message .= 'Whole query: ' . $query;
  359. die($message);
  360. }
  361. $result = array();
  362. while ($row = mysql_fetch_assoc($db_result)) {
  363. #$id = $row['datoen'];
  364. #$result[$id] = $row;
  365. array_push($result, $row);
  366. }
  367. return $result;
  368. }
  369. #####
  370. # Selecting getting overview info on a single multicast channel
  371. # - in a given time period
  372. # - on all probes.
  373. #
  374. function one_channel_info_query_ts($channel, $tsFrom=NULL, $tsTo=NULL)
  375. {
  376. if (is_numeric($tsFrom)) {
  377. $fromT = date('Y-m-d H:i:s', $tsFrom);
  378. }
  379. if (is_numeric($tsTo)) {
  380. $toT = date('Y-m-d H:i:s', $tsTo);
  381. }
  382. $result = & one_channel_info_query($channel, $fromT, $toT);
  383. return $result;
  384. }
  385. function one_channel_info_query($channel, $fromT=NULL, $toT=NULL, $interval=NULL)
  386. {
  387. // Query
  388. $query = sprintf(
  389. "SELECT probe_id,
  390. daemon_session_id,
  391. probes.distance,
  392. probes.name,
  393. probes.switch,
  394. probes.shortloc,
  395. multicast_dst,
  396. sum(delta_skips) as skips,
  397. sum(delta_discon) as drops,
  398. count(multicast_dst) as records,
  399. UNIX_TIMESTAMP(min(record_time)) as timemin,
  400. UNIX_TIMESTAMP(max(record_time)) as timemax,
  401. TIMESTAMPDIFF(SECOND, min(record_time), max(record_time)) as period
  402. FROM log_event, probes
  403. WHERE probe_id = probes.id
  404. AND multicast_dst = '%s' ",
  405. mysql_real_escape_string($channel));
  406. $query .= helper_between_query($fromT, $toT, $interval, "record_time");
  407. #$query .= " AND prev_id > 0"; // Exclude the initial records, on daemon start
  408. #$query .= " AND event_type = 2"; // Only include drop events
  409. # $query .= " AND delta_discon > 0"; // Exclude none drop records
  410. $query .= " GROUP BY daemon_session_id, probe_id";
  411. $query .= " ORDER BY probes.distance, probe_id, timemin";
  412. //print_r($query);
  413. $db_result = mysql_query($query);
  414. if (!$db_result) {
  415. $message = 'Invalid query: ' . mysql_error() . "\n<br>";
  416. $message .= 'Whole query: ' . $query;
  417. die($message);
  418. }
  419. # DEBUG
  420. # echo __FUNCTION__ . ' QUERY: <p>' . $query . "</p><br>";
  421. $result = array();
  422. $probekey_prev = "";
  423. while ($row = mysql_fetch_assoc($db_result)) {
  424. $probekey = "probe_id" . $row['probe_id'];
  425. /* Save an info record */
  426. if ("$probekey" != "$probekey_prev" ) {
  427. $result["$probekey"]['info']['name'] = $row['name'];
  428. $result["$probekey"]['info']['switch'] = $row['switch'];
  429. $result["$probekey"]['info']['shortloc'] = $row['shortloc'];
  430. $result["$probekey"]['info']['distance'] = $row['distance'];
  431. $result["$probekey"]['info']['multicast_dst'] = $row['multicast_dst'];
  432. $result["$probekey"]['info']['timemin'] = $row['timemin'];
  433. $result["$probekey"]['info']['probe_id'] = $row['probe_id'];
  434. }
  435. $probekey_prev = $probekey;
  436. // Always save the last timemax
  437. $result["$probekey"]['info']['timemax'] = $row['timemax'];
  438. // Counting the total value
  439. $result["$probekey"]['info']['skips'] += $row['skips'];
  440. $result["$probekey"]['info']['drops'] += $row['drops'];
  441. $result["$probekey"]['info']['records'] += $row['records'];
  442. $result["$probekey"]['info']['period'] += $row['period'];
  443. // Counting the number of period recordings
  444. $result["$probekey"]['info']['count']++;
  445. // Save the entire row under the $daemon_id
  446. $daemon_id = $row['daemon_session_id'];
  447. $result["$probekey"]["$daemon_id"] = $row;
  448. //OLD: array_push($result, $row);
  449. }
  450. # print_r($result);
  451. return $result;
  452. }
  453. // The selected{F,T} option are timestamps which are used for
  454. // generating correct URLs
  455. function one_channel_info_show_table(& $result, $selectedF=NULL, $selectedT=NULL)
  456. {
  457. $selected_id = $_REQUEST['probeid'];
  458. echo "<TABLE width=100% border=\"1\">";
  459. echo "<TR>";
  460. echo "<TD>sub-periods</TD>";
  461. # Make it possible to select all probes
  462. echo "<TD "
  463. . "onClick=\"document.getElementById('probeid').value = '';"
  464. . "document.getElementById('frmChannel').submit();"
  465. . "this.style.backgroundColor='red';"
  466. . "\"\n";
  467. echo "onMouseOver=\"this.style.backgroundColor='yellow';\"\n";
  468. if (!isset($selected_id) || $selected_id == "" || $selected_id == "all") {
  469. echo " style=\"background-color:yellow;cursor:crosshair\"\n";
  470. echo " onMouseOut=\"this.style.backgroundColor='yellow';\"\n";
  471. } else {
  472. echo " style=\"cursor:pointer\"\n";
  473. echo " onMouseOut=\"this.style.backgroundColor='white';\"\n";
  474. }
  475. echo ">probe (all)</TD>";
  476. echo "<TD><b>drops</b></TD>";
  477. echo "<TD><b>average sec between drops</b></TD>";
  478. echo "<TD>measurement <b>period</b></TD>";
  479. echo "<TD>from</TD>";
  480. echo "<TD>to</TD>";
  481. echo "<TD>records</TD>";
  482. echo "</TR>\n";
  483. foreach ($result as $prikey => $probe) {
  484. $cnt = 0;
  485. foreach ($probe as $key => $row) {
  486. $probe_id = $row['probe_id'];
  487. /*
  488. if ($row['records'] < 2) {
  489. continue; //break the loop and get the next value.
  490. }
  491. */
  492. if ($key == "info") {
  493. /* Notice expandRow() is defined in function.js */
  494. echo "\n<TR ";
  495. echo ">\n";
  496. echo " <TD "
  497. . "onclick=\"javascript: expandRow('$prikey');\""
  498. . "onMouseOver=\"this.style.backgroundColor='gray';\""
  499. . "onMouseOut=\"this.style.backgroundColor='white';\"";
  500. echo ">" . "<b>" . $row['count'] . "</b>" . "</TD>\n";
  501. $theprobe = $row['name'] . "/" . $row['switch'];
  502. echo " <TD "
  503. . "onClick=\"document.getElementById('probeid').value ="
  504. . $probe_id . ";"
  505. . "document.getElementById('frmChannel').submit();"
  506. . "this.style.backgroundColor='red';"
  507. . "\"\n";
  508. echo "onMouseOver=\"this.style.backgroundColor='yellow';\"\n";
  509. if ($selected_id == $probe_id) {
  510. echo " style=\"background-color:yellow;cursor:crosshair\"\n";
  511. echo " onMouseOut=\"this.style.backgroundColor='yellow';\"\n";
  512. } else {
  513. echo " style=\"cursor:pointer\"\n";
  514. echo " onMouseOut=\"this.style.backgroundColor='white';\"\n";
  515. }
  516. echo "><b>";
  517. echo $theprobe;
  518. echo "</b></TD>\n";
  519. unset($theprobe);
  520. } else {
  521. $cnt++;
  522. $rowid = $prikey ."_" . $cnt;
  523. echo "<TR id=\"$rowid\" style=\"display:none;\">\n";
  524. echo " <TD> sub-" . $cnt . "</TD>\n";
  525. echo " <TD>" . "measurement period" . "</TD>\n";
  526. }
  527. echo ' <TD align="right">' . $row['drops'] . "</TD>\n";
  528. #echo '<TD align="right">' . $row['skips'] . "</TD>";
  529. /* This is a strange number, but that might no be correct if a
  530. * probes has been offline a long periode.
  531. *
  532. * The period (in sec) divided by number of drops, given
  533. * an average of sec between drop (if they were spaced
  534. * perfect in time)
  535. */
  536. if ($row['drops'] > 0) {
  537. $average_sec_between_drops = $row['period'] / $row['drops'];
  538. $formatted = sprintf("%.2f", $average_sec_between_drops);
  539. } else {
  540. $formatted = "none";
  541. }
  542. echo ' <TD align="right">' . $formatted . "</TD>\n";
  543. // Create a link to self which limits the timeperiod
  544. $tsF=$row['timemin'];
  545. $tsT=$row['timemax'];
  546. // Hack?
  547. $bucketsz= $_REQUEST['bucketsz'];
  548. $channel = $row['multicast_dst'];
  549. $link = '<A HREF="?channel=' . $channel;
  550. $link .= '&bucketsz=' . $bucketsz;
  551. $linkF = "tstampF=$tsF";
  552. $linkT = "tstampT=$tsT";
  553. $linkend = '">';
  554. echo ' <TD align="right">';
  555. echo "$link&$linkF&$linkT$linkend";
  556. echo timespan($row['period']);
  557. echo "</A>";
  558. echo "</TD>\n";
  559. $datemin = date('Y-m-d H:i', $row['timemin']);
  560. $datemax = date('Y-m-d H:i', $row['timemax']);
  561. echo " <TD>" . "$link&$linkF&tstampT=$selectedT$linkend"
  562. . $datemin . "</TD>";
  563. echo "<TD>" . "$link&$linkT&tstampF=$selectedF$linkend"
  564. . $datemax . "</TD>\n";
  565. // echo "<TD>" . $row['timemax'] . "</TD>";
  566. echo ' <TD align="right">' . $row['records'] . "</TD>\n";
  567. echo "</TR>\n";
  568. }
  569. # echo "</div>";
  570. }
  571. echo "</TABLE>\n\n";
  572. }
  573. #####
  574. # Simply get a list of multicast channels for choose from
  575. #
  576. function multicast_list_query()
  577. {
  578. $query = "SELECT distinct(multicast_dst) FROM log_event";
  579. $query .= " ORDER BY INET_ATON(multicast_dst)";
  580. //print_r($query);
  581. $db_result = mysql_query($query);
  582. if (!$db_result) {
  583. $message = 'Invalid query: ' . mysql_error() . "\n<br>";
  584. $message .= 'Whole query: ' . $query;
  585. die($message);
  586. }
  587. $result = array();
  588. while ($row = mysql_fetch_assoc($db_result)) {
  589. #$id = $row['datoen'];
  590. #$result[$id] = $row;
  591. array_push($result, $row);
  592. }
  593. return $result;
  594. }
  595. # Present a form for selecting/choosing a channel
  596. function multicast_list_form_select(& $channel_list_data) {
  597. # Channel list data from:
  598. # multicast_list_form_select($fromT, $toT);
  599. # Get hold of the currently selected channel
  600. $channel = $_REQUEST['channel'];
  601. $bucketsz= $_REQUEST['bucketsz'];
  602. $default_bucketsz=3600;
  603. if (!is_numeric($bucketsz)) {
  604. $bucketsz = $default_bucketsz;
  605. }
  606. echo '<form method="get" id="choose_channel" action="">';
  607. echo " <p>Choose a channel:<br />\n";
  608. echo " <select name=\"channel\">\n";
  609. foreach ($channel_list_data as $row) {
  610. $multicast_dst = $row['multicast_dst'];
  611. echo ' <option ';
  612. echo " value=\"$multicast_dst\"";
  613. if ($channel == $multicast_dst) {
  614. echo ' selected="selected"';
  615. }
  616. echo ">$multicast_dst";
  617. echo "</option>\n";
  618. unset($multicast_dst);
  619. }
  620. echo " </select>\n";
  621. echo " <input type=\"submit\" value=\"Select\" /><br>\n";
  622. # Selection of bucketsz
  623. echo '<br>Aggregation interval/period (bucket size) in sec:';
  624. echo ' <input type="text"';
  625. echo ' value="' . $bucketsz . '"';
  626. echo ' name="bucketsz" id="bucketsz"/>';
  627. echo " (" . timespan($bucketsz) . ")<br>";
  628. echo "</form>\n";
  629. }
  630. # Form element for selecting/choosing a channel
  631. function form_elem_select_multicast_list(& $channel_list_data) {
  632. // Channel list data from:
  633. // multicast_list_query($fromT, $toT);
  634. // Get hold of the currently selected channel
  635. $channel = $_REQUEST['channel'];
  636. echo " <select name=\"channel\">\n";
  637. foreach ($channel_list_data as $row) {
  638. $multicast_dst = $row['multicast_dst'];
  639. echo ' <option ';
  640. echo " value=\"$multicast_dst\"";
  641. if ($channel == $multicast_dst) {
  642. echo ' selected="selected"';
  643. }
  644. echo ">$multicast_dst";
  645. echo "</option>\n";
  646. unset($multicast_dst);
  647. }
  648. echo " </select>\n";
  649. }
  650. function form_elem_bucketsz($period, $elems=120)
  651. {
  652. // Get hold of the currently setting
  653. $bucketsz= $_REQUEST['bucketsz'];
  654. // Selection of bucketsz
  655. echo 'Aggregation interval/period (bucket size) in sec:';
  656. echo ' <input size="8" type="text"';
  657. echo ' value="';
  658. if (is_numeric($bucketsz)) {
  659. echo $bucketsz . '"';
  660. } else {
  661. $default_bucketsz= floor($period / $elems);
  662. echo $default_bucketsz . '"';
  663. echo 'style="background-color:pink" ';
  664. # Hack to avoid reloading page
  665. $_REQUEST['bucketsz'] = $default_bucketsz;
  666. $bucketsz = $default_bucketsz;
  667. }
  668. echo ' name="bucketsz" id="bucketsz"';
  669. echo '/>';
  670. echo " (" . timespan($bucketsz) . ")\n";
  671. }
  672. function form_elem_timeperiod_from()
  673. {
  674. $fromT = $_REQUEST['fromT'];
  675. # Use the get timestamp if $fromT is not set
  676. if (!isset($fromT)) {
  677. $tstampF = $_REQUEST['tstampF'];
  678. if (is_numeric($tstampF)) {
  679. $fromT = date('Y-m-d H:i:s', $tstampF);
  680. }
  681. }
  682. $timestampF;
  683. echo '<input type="text" size="19"';
  684. if (($timestampF = strtotime($fromT)) === false) {
  685. $fromT = "- 2 days";
  686. $timestampF = strtotime($fromT);
  687. // Indicate the date was not valid
  688. echo ' style="background-color:pink" ';
  689. }
  690. ###$calc_fromT = date('Y-m-d H:i:s', $timestampF);
  691. #### Overwrite input to avoid reload of page
  692. ###$_REQUEST['fromT'] = $calc_fromT;
  693. # Store the timestamp in the global request
  694. $_REQUEST['tstampF'] = $timestampF;
  695. echo 'value="' . htmlspecialchars($fromT);
  696. echo "\" name='fromT' id='fromT' />\n";
  697. return $timestampF;
  698. }
  699. function form_elem_timeperiod_to()
  700. {
  701. $toT = $_REQUEST['toT'];
  702. # Use the get timestamp if $toT is not set
  703. if (!isset($toT)) {
  704. $tstampT = $_REQUEST['tstampT'];
  705. if (is_numeric($tstampT)) {
  706. $toT = date('Y-m-d H:i:s', $tstampT);
  707. }
  708. }
  709. $timestampT;
  710. echo '<input type="text" size="19"';
  711. if (($timestampT = strtotime($toT)) === false) {
  712. $toT = "now";
  713. $timestampT = strtotime($toT);
  714. // Indicate the date was not valid
  715. echo ' style="background-color:pink" ';
  716. }
  717. ###$calc_toT = date('Y-m-d H:i:s', $timestampT);
  718. #### Overwrite input to avoid reload of page
  719. ###$_REQUEST['toT'] = $calc_toT;
  720. # Store the timestamp in the global request
  721. $_REQUEST['tstampT'] = $timestampT;
  722. echo 'value="' . htmlspecialchars($toT);
  723. echo "\" name='toT' id='toT' />\n";
  724. return $timestampT;
  725. }
  726. function form_elem_maxy()
  727. {
  728. $maxy_default = 5000;
  729. $maxy = $_REQUEST['maxy'];
  730. $maxy_fixed = $_REQUEST['maxy_fixed'];
  731. echo ' <input ';
  732. echo 'type="text" size="5"';
  733. echo 'value="';
  734. if (is_numeric($maxy)) {
  735. echo $maxy . '" ';
  736. } else {
  737. echo $maxy_default . '" ';
  738. echo 'style="background-color:pink" ';
  739. # Hack to avoid reloading page
  740. $_REQUEST['maxy'] = $maxy_default;
  741. }
  742. echo ' name="maxy" id="maxy" />';
  743. echo ' <input type="checkbox" name="maxy_fixed"';
  744. echo "value=\"fixed\"";
  745. if ($maxy_fixed == "fixed") {
  746. echo " checked ";
  747. }
  748. echo "/>";
  749. #echo "force\n";
  750. }
  751. function form_elem_probeid($options)
  752. {
  753. $probeid = $_REQUEST['probeid'];
  754. echo ' <input ';
  755. if ($options['hidden'] == TRUE) {
  756. echo 'type="hidden" ';
  757. } else {
  758. echo 'type="text" ';
  759. }
  760. echo "value=\"$probeid\" name='probeid' id='probeid' />";
  761. }
  762. function form_channel_selection(& $channels, & $probesinfo)
  763. {
  764. echo "<fieldset>\n";
  765. echo "<legend>\n";
  766. echo "Choose a channel and adjust period\n";
  767. echo "</legend>\n";
  768. echo '<form name="frmChannel" id="frmChannel"';
  769. echo ' method="get" id="choose_channel" action="">';
  770. echo " <p>Choose a channel:<br />\n";
  771. form_elem_select_multicast_list($channels);
  772. echo " <input type=\"submit\" value=\"Select\" /><br>\n";
  773. $probeid = $_REQUEST['probeid'];
  774. $probename = get_probename($probeid, $probesinfo);
  775. echo "Selected probe: <b>$probename</b><br>\n";
  776. form_elem_probeid(array('hidden' => TRUE));
  777. echo "From: ";
  778. $timestampF = form_elem_timeperiod_from();
  779. echo date('Y-m-d H:i:s', $timestampF);
  780. echo "<br>\n";
  781. echo "To:&nbsp&nbsp&nbsp&nbsp&nbsp";
  782. $timestampT = form_elem_timeperiod_to();
  783. echo date('Y-m-d H:i:s', $timestampT);
  784. echo "<br>\n";
  785. $period = ($timestampT - $timestampF) ;
  786. $readable_period = timespan($period);
  787. echo " Period: $readable_period";
  788. echo " (sec:$period)";
  789. echo "<br>\n";
  790. form_elem_bucketsz($period);
  791. echo "<br>\n";
  792. echo "Excessive level";
  793. form_elem_maxy();
  794. echo "fix graph";
  795. #echo "<br>\n";
  796. echo "</form>\n";
  797. echo "</fieldset>\n";
  798. echo "<br>\n";
  799. }
  800. #####
  801. # Info on the probes
  802. function probes_info_query()
  803. {
  804. // Query
  805. $query = sprintf(
  806. "SELECT *
  807. FROM probes
  808. WHERE probes.hidden <> 'yes'
  809. ORDER BY probes.distance");
  810. $result = mysql_query($query);
  811. if (!$result) {
  812. $message = 'Invalid query: ' . mysql_error() . "\n<br>";
  813. $message .= 'Whole query: ' . $query;
  814. die($message);
  815. }
  816. $probes = array();
  817. while ($row = mysql_fetch_assoc($result)) {
  818. $id = $row['id'];
  819. $probes[$id]=$row;
  820. // array_push($probes, $row);
  821. }
  822. // print_r($probes);
  823. return $probes;
  824. }
  825. function get_probename($probeid, & $probesinfo)
  826. {
  827. $name="Unknown";
  828. # If not probeid is selected then assume
  829. # the selects will return data for all probes
  830. #
  831. if (!isset($probeid) || $probeid == "" || $probeid == "all") {
  832. $name="ALL";
  833. return $name;
  834. }
  835. if (is_array($probesinfo)) {
  836. $probe = $probesinfo[$probeid];
  837. if (!is_array($probe)) {
  838. # No probe existed with that $probeid
  839. echo "<h3>". __FUNCTION__ ."() ERROR: Invalid probeid:$probeid</h3>";
  840. } else {
  841. // extract probename, switch and short location
  842. $probename = $probe['name'];
  843. $switch = $probe['switch'];
  844. $shortloc = $probe['shortloc'];
  845. $name = "$probename/$switch/$shortloc";
  846. #$name = "$probename/$switch";
  847. }
  848. } else {
  849. $name = "ProbeID:$probeid";
  850. }
  851. return $name;
  852. }
  853. function probes_info_show_table($probes)
  854. {
  855. echo "\n<TABLE border=\"1\">";
  856. echo "<TR>";
  857. echo "<TD>id</TD>";
  858. echo "<TD>probename</TD>";
  859. echo "<TD>distance</TD>";
  860. echo "<TD>connected to switch</TD>";
  861. echo "<TD>short location</TD>";
  862. echo "<TD>location</TD>";
  863. echo "<TD>description</TD>";
  864. echo "</TR>\n";
  865. foreach ($probes as $row) {
  866. echo "<TR>";
  867. echo "<TD>" . $row['id'] . "</TD>";
  868. echo "<TD>" . $row['name'] . "</TD>";
  869. echo "<TD>" . $row['distance'] . "</TD>";
  870. echo "<TD>" . $row['switch'] . "</TD>";
  871. echo "<TD>" . $row['shortloc'] . "</TD>";
  872. echo "<TD>" . $row['location'] . "</TD>";
  873. echo "<TD>" . $row['description'] . "</TD>";
  874. echo "</TR>\n";
  875. // print_r($probes);
  876. }
  877. echo "</TABLE>\n\n";
  878. }
  879. function probes_info_form_radio($probes, $probeid=0, $page="page01.php") {
  880. if (!is_array($probes)) {
  881. echo "\n<br>INVALID INPUT \$probes is not an array<br>\n";
  882. return;
  883. }
  884. echo "<form method=\"get\" action=\"$page\">\n";
  885. foreach ($probes as $row) {
  886. #$id = $row['id'];
  887. #$name = $row['name'];
  888. #$shortloc = $row['shortloc'];
  889. #$switch = $row['switch'];
  890. extract($row, EXTR_OVERWRITE);
  891. echo "<input name=probeid";
  892. echo " type=\"radio\" ";
  893. if ($probeid == $id) {
  894. echo "checked ";
  895. }
  896. echo " value=$id /> ";
  897. # The description
  898. echo "$name@$shortloc switch:$switch (distance:$distance)";
  899. echo "<br>\n";
  900. # print_r($row);
  901. }
  902. echo "<input type=\"submit\" value=\"Select probe\" />";
  903. # echo "</p>";
  904. echo "</form>\n";
  905. }
  906. function probes_info_form_tabel($probes, $selected_id=0, $page="#") {
  907. if (!is_array($probes)) {
  908. echo "\n<br>INVALID INPUT \$probes is not an array<br>\n";
  909. return;
  910. }
  911. # Extract stuff from the global _REQUEST array;
  912. $probeid = $_REQUEST['probeid'];
  913. $maxy = $_REQUEST['maxy'];
  914. $fromT = $_REQUEST['fromT'];
  915. $toT = $_REQUEST['toT'];
  916. $bucketsz = $_REQUEST['bucketsz'];
  917. $tstampF = $_REQUEST['tstampF'];
  918. $tstampT = $_REQUEST['tstampT'];
  919. #var_dump($_REQUEST);
  920. # Finding the probeid
  921. $selected_id=0;
  922. if (is_numeric($probeid)) {
  923. $selected_id = $probeid;
  924. }
  925. $default_bucketsz=86400;
  926. if (!is_numeric($bucketsz)) {
  927. $bucketsz = $default_bucketsz;
  928. }
  929. # Calc dates from Unix timestamps in $tstampX
  930. if (!isset($fromT) && is_numeric($tstampF)) {
  931. $fromT = date('Y-m-d H:i:s', $tstampF);
  932. }
  933. if (!isset($toT) && is_numeric($tstampT)) {
  934. $toT = date('Y-m-d H:i:s', $tstampT);
  935. }
  936. echo "<fieldset>\n";
  937. echo "<legend>\n";
  938. echo "Choose a probe and adjust period\n";
  939. echo "</legend>\n";
  940. #echo "selected_id:$selected_id\n";
  941. echo "<form name='frmProbe' id='frmProbe' method=\"get\" action=\"$page\">\n";
  942. echo "<TABLE border=\"1\">\n";
  943. echo "<TR>\n";
  944. echo " <TD>distance</TD>\n";
  945. echo " <TD>probename</TD>\n";
  946. echo " <TD>connected to switch</TD>\n";
  947. echo " <TD>short location</TD>\n";
  948. echo " <TD>location</TD>\n";
  949. echo " <TD>description</TD>\n";
  950. echo " <TD>id</TD>\n";
  951. echo "</TR>\n";
  952. foreach ($probes as $row) {
  953. extract($row, EXTR_OVERWRITE);
  954. echo "<TR id='r$id'\n"
  955. . " onClick=\"document.getElementById('probeid').value = $id;\n"
  956. . " document.getElementById('frmProbe').submit();\n"
  957. . " this.style.backgroundColor='red';\"\n"
  958. . " onMouseOver=\"this.style.backgroundColor='gray';\"\n";
  959. if ($selected_id == $id) {
  960. echo " style=\"background-color:yellow;cursor:crosshair\"\n";
  961. echo " onMouseOut=\"this.style.backgroundColor='yellow';\"\n";
  962. } else {
  963. echo " style=\"cursor:pointer\"\n";
  964. echo " onMouseOut=\"this.style.backgroundColor='white';\"\n";
  965. }
  966. echo ">\n";
  967. echo " <TD>$distance" . "</TD>\n";
  968. echo " <TD>$name" . "</TD>\n";
  969. echo " <TD>$switch" . "</TD>\n";
  970. echo " <TD>$shortloc" . "</TD>\n";
  971. echo " <TD>$location" . "</TD>\n";
  972. echo " <TD>$description </TD>\n";
  973. echo " <TD>$id" . "</TD>\n";
  974. echo "</TR>\n";
  975. }
  976. echo "</TABLE>\n";
  977. # Call form_elem_timeperiod_from()
  978. echo "From: ";
  979. $timestampF = form_elem_timeperiod_from();
  980. echo date('Y-m-d H:i:s', $timestampF);
  981. echo "<br>\n";
  982. # Call form_elem_timeperiod_to()
  983. echo "To:&nbsp&nbsp&nbsp&nbsp&nbsp";
  984. $timestampT = form_elem_timeperiod_to();
  985. echo date('Y-m-d H:i:s', $timestampT);
  986. echo "<br>\n";
  987. # Time conversion to human readable
  988. $period = ($timestampT - $timestampF);
  989. $readable_period = timespan($period);
  990. echo " Period: $readable_period\n";
  991. # echo " (sec:$period)<br>\n";
  992. echo " <input type=\"hidden\" value=\"$selected_id\" name='probeid' id='probeid' />";
  993. echo "<br>\n";
  994. # Aggregation interval
  995. # Call form_elem_bucketsz()
  996. form_elem_bucketsz($period);
  997. /*
  998. echo 'Aggregation interval/period (bucket size) in sec: <input type="text"';
  999. echo ' value="' . $bucketsz . '"';
  1000. echo ' name="bucketsz" id="bucketsz"/>';
  1001. echo " (" . timespan($bucketsz) . ")<br>";
  1002. */
  1003. echo "<br>";
  1004. echo "Excessive level";
  1005. form_elem_maxy();
  1006. echo "fix graph";
  1007. echo "<br>\n";
  1008. echo '<input type="submit" value="Submit" />';
  1009. echo "</form>\n";
  1010. echo "</fieldset>\n\n\n";
  1011. }
  1012. #####
  1013. # Functions providing facilities for removing channels from the dataset.
  1014. #
  1015. # Global var:
  1016. # $_POST['remove_channels'];
  1017. #
  1018. /* // Example code:
  1019. $remove_channels =& $_POST['remove_channels'];
  1020. foreach ($data as $row) {
  1021. $multicast_dst = $row['multicast_dst'];
  1022. if (isset($remove_channels["$multicast_dst"])) {
  1023. // Skip
  1024. $cnt_removed++;
  1025. continue;
  1026. }
  1027. }
  1028. */
  1029. # Present a form for de-selecting channels
  1030. function form_remove_channels(& $channel_list_data) {
  1031. # Channel list data from:
  1032. # multicast_probe_data_query($probeid, $fromT, $toT);
  1033. # Get hold of the currently checked elements
  1034. $checked =& $_POST['remove_channels'];
  1035. #print_r($checked);
  1036. echo '<form method="post" id="remove_channels" action="">';
  1037. echo " <p>Remove the following channels:<br />\n";
  1038. echo " <input type=\"submit\" value=\"Remove\" /><br>\n";
  1039. foreach ($channel_list_data as $row) {
  1040. $drops = $row['drops'];
  1041. $multicast_dst = $row['multicast_dst'];
  1042. $records = $row['records'];
  1043. $streamer_src = $row['ip_src'];
  1044. echo ' <input name="';
  1045. echo "remove_channels[$multicast_dst]\"";
  1046. echo ' value="remove" type="checkbox" ';
  1047. if (isset($checked["$multicast_dst"])) {
  1048. echo "checked ";
  1049. }
  1050. echo "/>$multicast_dst (drops:$drops, records:$records";
  1051. echo ", streamer:$streamer_src";
  1052. echo ")<br />\n";
  1053. unset($drops);
  1054. unset($multicast_dst);
  1055. unset($records);
  1056. unset($streamer_src);
  1057. }
  1058. echo "</form>\n";
  1059. }
  1060. function query_str_remove_channels()
  1061. {
  1062. $remove_channels =& $_POST['remove_channels'];
  1063. if (!isset($remove_channels)) {
  1064. return "";
  1065. }
  1066. $str_parts=array();
  1067. foreach ($remove_channels as $key => $value) {
  1068. $str = sprintf("'%s'", mysql_real_escape_string($key));
  1069. array_push($str_parts, $str);
  1070. }
  1071. $str_elems = implode(', ',$str_parts);
  1072. $query = " AND multicast_dst NOT IN ($str_elems)";
  1073. return $query;
  1074. }
  1075. ?>