PageRenderTime 120ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/ganglia-3.3.7/web/cluster_view.php

#
PHP | 613 lines | 479 code | 91 blank | 43 comment | 93 complexity | 54947aa07e0c593e35c73501bb7f7f05 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. $refresh = isset($_GET['refresh']);
  3. include_once "./eval_conf.php";
  4. // ATD - function.php must be included before get_context.php.
  5. // It defines some needed functions.
  6. include_once "./functions.php";
  7. include_once "./get_context.php";
  8. include_once "./ganglia.php";
  9. include_once "./get_ganglia.php";
  10. include_once "./dwoo/dwooAutoload.php";
  11. if ($refresh) {
  12. try {
  13. $dwoo = new Dwoo($conf['dwoo_compiled_dir'], $conf['dwoo_cache_dir']);
  14. } catch (Exception $e) {
  15. print "<H4>There was an error initializing the Dwoo PHP Templating Engine: " .
  16. $e->getMessage() .
  17. "<br><br>The compile directory should be owned and writable by the apache user.</H4>";
  18. exit;
  19. }
  20. }
  21. function get_load($host, $metrics) {
  22. if (isset($metrics[$host]["cpu_num"]['VAL']) and
  23. $metrics[$host]["cpu_num"]['VAL'] != 0 ) {
  24. $cpus = $metrics[$host]["cpu_num"]['VAL'];
  25. } else {
  26. $cpus = 1;
  27. }
  28. if (isset($metrics[$host]["load_one"]['VAL']) ){
  29. $load_one = $metrics[$host]["load_one"]['VAL'];
  30. } else {
  31. $load_one = 0;
  32. }
  33. $load = ((float) $load_one) / $cpus;
  34. return $load;
  35. }
  36. function get_load_pie($showhosts,
  37. $hosts_up,
  38. $hosts_down,
  39. $user,
  40. $conf,
  41. $metrics,
  42. $cluster,
  43. $name,
  44. $data) {
  45. if ($showhosts) {
  46. $percent_hosts = array();
  47. foreach ($hosts_up as $host => $val) {
  48. // If host_regex is defined
  49. if (isset($user['host_regex']) &&
  50. ! preg_match("/" .$user['host_regex'] . "/", $host))
  51. continue;
  52. $load = get_load($host, $metrics);
  53. if (isset($percent_hosts[load_color($load)])) {
  54. $percent_hosts[load_color($load)] += 1;
  55. } else {
  56. $percent_hosts[load_color($load)] = 1;
  57. }
  58. }
  59. foreach ($hosts_down as $host => $val) {
  60. $load = -1.0;
  61. if (isset($percent_hosts[load_color($load)])) {
  62. $percent_hosts[load_color($load)] += 1;
  63. } else {
  64. $percent_hosts[load_color($load)] = 1;
  65. }
  66. }
  67. // Show pie chart of loads
  68. $pie_args = "title=" . rawurlencode("Cluster Load Percentages");
  69. $pie_args .= "&amp;size=250x150";
  70. foreach ($conf['load_colors'] as $name => $color) {
  71. if (!array_key_exists($color, $percent_hosts))
  72. continue;
  73. $n = $percent_hosts[$color];
  74. $name_url = rawurlencode($name);
  75. $pie_args .= "&amp;$name_url=$n,$color";
  76. }
  77. $data->assign("pie_args", $pie_args);
  78. } else {
  79. // Show pie chart of hosts up/down
  80. $pie_args = "title=" . rawurlencode("Host Status");
  81. $pie_args .= "&amp;size=250x150";
  82. $up_color = $conf['load_colors']["25-50"];
  83. $down_color = $conf['load_colors']["down"];
  84. $pie_args .= "&amp;Up=$cluster[HOSTS_UP],$up_color";
  85. $pie_args .= "&amp;Down=$cluster[HOSTS_DOWN],$down_color";
  86. $data->assign("pie_args", $pie_args);
  87. }
  88. }
  89. function get_host_metric_graphs($showhosts,
  90. $hosts_up,
  91. $hosts_down,
  92. $user,
  93. $conf,
  94. $metrics,
  95. $metricname,
  96. $sort,
  97. $clustername,
  98. $get_metric_string,
  99. $cluster,
  100. $always_timestamp,
  101. $reports_metricname,
  102. $clustergraphsize,
  103. $range,
  104. $cs,
  105. $ce,
  106. $vlabel,
  107. $data) {
  108. $sorted_hosts = array();
  109. $down_hosts = array();
  110. if ($showhosts) {
  111. foreach ($hosts_up as $host => $val) {
  112. // If host_regex is defined
  113. if (isset($user['host_regex']) &&
  114. ! preg_match("/" .$user['host_regex'] . "/", $host))
  115. continue;
  116. $load = get_load($host, $metrics);
  117. $host_load[$host] = $load;
  118. if ($metricname == "load_one")
  119. $sorted_hosts[$host] = $load;
  120. else if (isset($metrics[$host][$metricname]))
  121. $sorted_hosts[$host] = $metrics[$host][$metricname]['VAL'];
  122. else
  123. $sorted_hosts[$host] = "";
  124. } // foreach hosts_up
  125. foreach ($hosts_down as $host => $val) {
  126. $down_hosts[$host] = -1.0;
  127. }
  128. $data->assign("node_legend", 1);
  129. }
  130. if (!is_array($hosts_up) or !$showhosts)
  131. return;
  132. switch ($sort) {
  133. case "descending":
  134. arsort($sorted_hosts);
  135. break;
  136. case "by name":
  137. uksort($sorted_hosts, "strnatcmp");
  138. break;
  139. default:
  140. case "ascending":
  141. asort($sorted_hosts);
  142. break;
  143. }
  144. $sorted_hosts = array_merge($down_hosts, $sorted_hosts);
  145. if (isset($user['max_graphs']))
  146. $max_graphs = $user['max_graphs'];
  147. else
  148. $max_graphs = $conf['max_graphs'];
  149. // First pass to find the max value in all graphs for this
  150. // metric. The $start,$end variables comes from get_context.php,
  151. // included in index.php.
  152. // Do this only if person has not selected a maximum set of graphs to display
  153. if ($max_graphs == 0)
  154. list($min, $max) = find_limits($sorted_hosts, $metricname);
  155. // Second pass to output the graphs or metrics.
  156. $i = 1;
  157. // Initialize overflow list
  158. $overflow_list = array();
  159. $overflow_counter = 1;
  160. $cluster_url = rawurlencode($clustername);
  161. $size = isset($clustergraphsize) ? $clustergraphsize : 'small';
  162. if ($conf['hostcols'] == 0) // enforce small size in multi-host report
  163. $size = 'small';
  164. // set host zoom class based on the size of the graph shown
  165. if (isset($conf['zoom_support']) && $conf['zoom_support'] === true)
  166. $additional_host_img_html_args = "class=host_${size}_zoomable";
  167. foreach ($sorted_hosts as $host => $value) {
  168. $host_url = rawurlencode($host);
  169. $host_link="\"?c=$cluster_url&amp;h=$host_url&amp;$get_metric_string\"";
  170. $textval = "";
  171. //echo "$host: $value, ";
  172. if (isset($hosts_down[$host]) and $hosts_down[$host]) {
  173. $last_heartbeat = $cluster['LOCALTIME'] - $hosts_down[$host]['REPORTED'];
  174. $age = $last_heartbeat > 3600 ?
  175. uptime($last_heartbeat) : "${last_heartbeat}s";
  176. $class = "down";
  177. $textval = "down <br>&nbsp;<font size=\"-2\">Last heartbeat $age ago</font>";
  178. } else {
  179. if (isset($metrics[$host][$metricname]))
  180. $val = $metrics[$host][$metricname];
  181. else
  182. $val = NULL;
  183. $class = "metric";
  184. if ($val['TYPE']=="timestamp" or
  185. (isset($always_timestamp[$metricname]) and
  186. $always_timestamp[$metricname])) {
  187. $textval = date("r", $val['VAL']);
  188. } elseif ($val['TYPE']=="string" or
  189. $val['SLOPE']=="zero" or
  190. (isset($always_constant[$metricname]) and
  191. $always_constant[$metricname] or
  192. ($max_graphs > 0 and $i > $max_graphs))) {
  193. if (isset($reports_metricname) and $reports_metricname)
  194. // No "current" values available for reports
  195. $textval = "N/A";
  196. else
  197. $textval = "$val[VAL]";
  198. if (isset($val['UNITS']))
  199. $textval .= " $val[UNITS]";
  200. }
  201. }
  202. $graphargs = "z=$size&amp;c=$cluster_url&amp;h=$host_url";
  203. if (isset($host_load[$host])) {
  204. $load_color = load_color($host_load[$host]);
  205. $graphargs .= "&amp;l=$load_color&amp;v=$val[VAL]";
  206. }
  207. $graphargs .= "&amp;r=$range&amp;su=1&amp;st=$cluster[LOCALTIME]";
  208. if ($cs)
  209. $graphargs .= "&amp;cs=" . rawurlencode($cs);
  210. if ($ce)
  211. $graphargs .= "&amp;ce=" . rawurlencode($ce);
  212. if ($showhosts == 1 && $max_graphs == 0 )
  213. $graphargs .= "&amp;x=$max&amp;n=$min";
  214. if (isset($vlabel))
  215. $graphargs .= "&amp;vl=" . urlencode($vlabel);
  216. if ($textval) {
  217. $cell = "<td class=$class>" .
  218. "<b><a href=$host_link>$host</a></b><br>" .
  219. "<i>$metricname:</i> <b>$textval</b></td>";
  220. } else {
  221. $cell = "<td><div><font style='font-size: 8px'>$host</font><br><a href=$host_link><img $additional_host_img_html_args src=\"./graph.php?";
  222. $cell .= (isset($reports_metricname) and
  223. $reports_metricname) ? "g=$metricname" : "m=$metricname";
  224. $cell .= "&amp;$graphargs\" title=\"$host\" border=0 style=\"padding:2px;\"></a></div></td>";
  225. }
  226. if ($conf['hostcols'] == 0) {
  227. $pre = "<td><a href=$host_link><img src=\"./graph.php?g=";
  228. $post = "&amp;$graphargs\" $additional_host_img_html_args title=\"$host\" border=0 style=\"padding:2px;\"></a></td>";
  229. $cell .= $pre . "load_report" . $post;
  230. $cell .= $pre . "mem_report" . $post;
  231. $cell .= $pre . "cpu_report" . $post;
  232. $cell .= $pre . "network_report" . $post;
  233. }
  234. // Check if max_graphs is set.
  235. // If it put cells in an overflow list since that one is hidden by default
  236. if ($max_graphs > 0 and $i > $max_graphs ) {
  237. $overflow_list[$host]["metric_image"] = $cell;
  238. if (! ($overflow_counter++ % $conf['hostcols']) ) {
  239. $overflow_list[$host]["br"] = "</tr><tr>";
  240. } else {
  241. $overflow_list[$host]["br"] = "";
  242. }
  243. } else {
  244. $sorted_list[$host]["metric_image"] = $cell;
  245. if (! ($i++ % $conf['hostcols']) ) {
  246. $sorted_list[$host]["br"] = "</tr><tr>";
  247. } else {
  248. $sorted_list[$host]["br"] = "";
  249. }
  250. } // end of if ($max_graphs > 0 and $i > $max_graphs ) {
  251. } // foreach sorted_hosts
  252. $data->assign("sorted_list", $sorted_list);
  253. // If there is an overflow list
  254. if (sizeof($overflow_list) > 0) {
  255. $data->assign("overflow_list_header", '<p><table width=80%><tr><td align=center class=metric>
  256. <a href="#" id="overflow_list_button"onclick="$(\'#overflow_list\').toggle();" class="button ui-state-default ui-corner-all" title="Toggle overflow list">Show more hosts ('
  257. . ($overflow_counter - 1) .')</a>
  258. </td></tr></table>
  259. <div style="display: none;" id="overflow_list"><table>
  260. <tr>
  261. ');
  262. $data->assign("overflow_list_footer", "</div></tr></table></div>");
  263. } else {
  264. $data->assign("overflow_list_header", "");
  265. $data->assign("overflow_list_footer", "");
  266. }
  267. $data->assign("overflow_list", $overflow_list);
  268. }
  269. function get_cluster_overview($showhosts,
  270. $metrics,
  271. $cluster,
  272. $range,
  273. $clustername,
  274. $data) {
  275. $cpu_num = !$showhosts ? $metrics["cpu_num"]['SUM'] :
  276. cluster_sum("cpu_num", $metrics);
  277. $data->assign("cpu_num", $cpu_num);
  278. if (isset($cluster['HOSTS_UP'])) {
  279. $data->assign("num_nodes", intval($cluster['HOSTS_UP']));
  280. } else {
  281. $data->assign("num_nodes", 0);
  282. }
  283. if (isset($cluster['HOSTS_DOWN'])) {
  284. $data->assign("num_dead_nodes", intval($cluster['HOSTS_DOWN']));
  285. } else {
  286. $data->assign("num_dead_nodes", 0);
  287. }
  288. $load_one_sum = !$showhosts ? $metrics["load_one"]['SUM'] :
  289. cluster_sum("load_one", $metrics);
  290. $load_five_sum = !$showhosts ? $metrics["load_five"]['SUM'] :
  291. cluster_sum("load_five", $metrics);
  292. $load_fifteen_sum = !$showhosts ? $metrics["load_fifteen"]['SUM'] :
  293. cluster_sum("load_fifteen", $metrics);
  294. if (!$cpu_num)
  295. $cpu_num = 1;
  296. $cluster_load15 = sprintf("%.0f",
  297. ((double) $load_fifteen_sum / $cpu_num) * 100);
  298. $cluster_load5 = sprintf("%.0f", ((double) $load_five_sum / $cpu_num) * 100);
  299. $cluster_load1 = sprintf("%.0f", ((double) $load_one_sum / $cpu_num) * 100);
  300. $data->assign("cluster_load",
  301. "$cluster_load15%, $cluster_load5%, $cluster_load1%");
  302. $avg_cpu_num = find_avg($clustername, "", "cpu_num");
  303. if ($avg_cpu_num == 0)
  304. $avg_cpu_num = 1;
  305. $cluster_util = sprintf("%.0f",
  306. ((double) find_avg($clustername,
  307. "",
  308. "load_one") / $avg_cpu_num ) * 100);
  309. $data->assign("cluster_util", "$cluster_util%");
  310. $data->assign("range", $range);
  311. }
  312. function get_cluster_optional_reports($conf,
  313. $clustername,
  314. $get_metric_string,
  315. $localtime,
  316. $data) {
  317. $cluster_url = rawurlencode($clustername);
  318. $graph_args = "c=$cluster_url&amp;$get_metric_string&amp;st=$localtime";
  319. $optional_reports = "";
  320. // If we want zoomable support on graphs we need to add correct zoomable
  321. // class to every image
  322. $additional_cluster_img_html_args = "";
  323. if (isset($conf['zoom_support']) && $conf['zoom_support'] === true)
  324. $additional_cluster_img_html_args = "class=cluster_zoomable";
  325. $data->assign("additional_cluster_img_html_args", $additional_cluster_img_html_args);
  326. ###############################################################################
  327. # Let's find out what optional reports are included
  328. # First we find out what the default (site-wide) reports are then look
  329. # for host specific included or excluded reports
  330. ###############################################################################
  331. $default_reports = array("included_reports" => array(), "excluded_reports" => array());
  332. if (is_file($conf['conf_dir'] . "/default.json")) {
  333. $default_reports = array_merge(
  334. $default_reports,
  335. json_decode(file_get_contents($conf['conf_dir'] . "/default.json"), TRUE));
  336. }
  337. $cluster_file = $conf['conf_dir'] .
  338. "/cluster_" .
  339. str_replace(" ", "_", $clustername) .
  340. ".json";
  341. $override_reports = array("included_reports" => array(), "excluded_reports" => array());
  342. if (is_file($cluster_file)) {
  343. $override_reports = array_merge($override_reports,
  344. json_decode(file_get_contents($cluster_file), TRUE));
  345. }
  346. # Merge arrays
  347. $reports["included_reports"] =
  348. array_merge($default_reports["included_reports"],$override_reports["included_reports"]);
  349. $reports["excluded_reports"] =
  350. array_merge($default_reports["excluded_reports"],$override_reports["excluded_reports"]);
  351. # Remove duplicates
  352. $reports["included_reports"] = array_unique($reports["included_reports"]);
  353. $reports["excluded_reports"] = array_unique($reports["excluded_reports"]);
  354. $cluster_url = rawurlencode($clustername);
  355. foreach ($reports["included_reports"] as $index => $report_name ) {
  356. if (! in_array( $report_name, $reports["excluded_reports"])) {
  357. $optional_reports .= "<A HREF=\"./graph_all_periods.php?$graph_args&amp;g=" . $report_name . "&amp;z=large&amp;c=$cluster_url\">
  358. <IMG BORDER=0 style=\"padding:2px;\" $additional_cluster_img_html_args title=\"$cluster_url\" SRC=\"./graph.php?$graph_args&amp;g=" . $report_name ."&amp;z=medium&amp;c=$cluster_url\"></A>
  359. ";
  360. }
  361. }
  362. $data->assign("optional_reports", $optional_reports);
  363. $data->assign("graph_args", $graph_args);
  364. if (!isset($conf['optional_graphs']))
  365. $conf['optional_graphs'] = array();
  366. $optional_graphs_data = array();
  367. foreach ($conf['optional_graphs'] as $g) {
  368. $optional_graphs_data[$g]['name'] = $g;
  369. $optional_graphs_data[$g]['graph_args'] = $graph_args;
  370. }
  371. $data->assign('optional_graphs_data', $optional_graphs_data);
  372. }
  373. function get_load_heatmap($hosts_up, $user, $metrics, $data) {
  374. foreach ($hosts_up as $host => $val) {
  375. // If host_regex is defined
  376. if (isset($user['host_regex']) &&
  377. ! preg_match("/" .$user['host_regex'] . "/", $host))
  378. continue;
  379. $load = get_load($host, $metrics);
  380. $host_load[$host] = $load;
  381. }
  382. $num_hosts = count($host_load);
  383. $matrix = ceil(sqrt($num_hosts));
  384. $xindex = 0;
  385. $yindex = 0;
  386. foreach ($host_load as $key => $value) {
  387. if ($xindex >= $matrix) {
  388. $string_array[] = "[" . join(",", $matrix_array[$yindex]) . "]";
  389. $yindex++;
  390. $xindex = 0;
  391. }
  392. $matrix_array[$yindex][$xindex] = $value;
  393. $xindex++;
  394. }
  395. $string_array[] = "[" . join(",", $matrix_array[$yindex]) . "]";
  396. $conf['heatmap_size'] = 200;
  397. $heatmap = join(",", $string_array);
  398. $data->assign("heatmap", $heatmap);
  399. $data->assign("heatmap_size", floor($conf['heatmap_size'] / $matrix));
  400. }
  401. $fn = "cluster_" . ($refresh ? "refresh" : "view") . ".tpl";
  402. $tpl = new Dwoo_Template_File(template($fn));
  403. $data = new Dwoo_Data();
  404. if (! $refresh) {
  405. $data->assign("php_gd",
  406. (function_exists('imagegif') or function_exists('imagepng')));
  407. $data->assign("extra", template("cluster_extra.tpl"));
  408. $data->assign("user_may_edit",
  409. checkAccess( $clustername, GangliaAcl::EDIT, $conf ) );
  410. $data->assign("graph_engine", $conf['graph_engine']);
  411. }
  412. $data->assign("cluster", $clustername);
  413. $data->assign("localtimestamp", $cluster['LOCALTIME']);
  414. $data->assign("localtime", date("Y-m-d H:i", $cluster['LOCALTIME']));
  415. get_cluster_overview($showhosts,
  416. $metrics,
  417. $cluster,
  418. $range,
  419. $clustername,
  420. $data);
  421. if (! $refresh) {
  422. get_cluster_optional_reports($conf,
  423. $clustername,
  424. $get_metric_string,
  425. $cluster[LOCALTIME],
  426. $data);
  427. ///////////////////////////////////////////////////////////////////////////////
  428. // Begin Host Display Controller
  429. ///////////////////////////////////////////////////////////////////////////////
  430. // Correct handling of *_report metrics
  431. if (!$showhosts) {
  432. if (array_key_exists($metricname, $metrics))
  433. $units = $metrics[$metricname]['UNITS'];
  434. } else {
  435. if (array_key_exists($metricname, $metrics[key($metrics)]))
  436. if (isset($metrics[key($metrics)][$metricname]['UNITS']))
  437. $units = $metrics[key($metrics)][$metricname]['UNITS'];
  438. else
  439. $units = '';
  440. }
  441. // Correctly handle *_report cases and blank (" ") units
  442. if (isset($units)) {
  443. $vlabel = $units;
  444. if ($units == " ")
  445. $units = "";
  446. else
  447. $units=$units ? "($units)" : "";
  448. } else {
  449. $units = "";
  450. }
  451. $data->assign("metric","$metricname $units");
  452. $data->assign("metric_name","$metricname");
  453. $data->assign("sort", $sort);
  454. $data->assign("range", $range);
  455. $showhosts_levels = array(1 => array('checked'=>'', 'name'=>'Auto'),
  456. 2 => array('checked'=>'', 'name'=>'Same'),
  457. 0 => array('checked'=>'', 'name'=>'None'),
  458. );
  459. $showhosts_levels[$showhosts]['checked'] = 'checked';
  460. $data->assign("showhosts_levels", $showhosts_levels);
  461. if ($showhosts) {
  462. $data->assign("columns_size_dropdown", 1);
  463. $data->assign("cols_menu", $cols_menu);
  464. $data->assign("size_menu", $size_menu);
  465. }
  466. ///////////////////////////////////////////////////////////////////////////////
  467. // End Host Display Controller
  468. ///////////////////////////////////////////////////////////////////////////////
  469. }
  470. if (!(isset($conf['heatmaps_enabled']) and $conf['heatmaps_enabled'] == 1))
  471. get_load_pie($showhosts,
  472. $hosts_up,
  473. $hosts_down,
  474. $user,
  475. $conf,
  476. $metrics,
  477. $cluster,
  478. $name,
  479. $data);
  480. get_host_metric_graphs($showhosts,
  481. $hosts_up,
  482. $hosts_down,
  483. $user,
  484. $conf,
  485. $metrics,
  486. $metricname,
  487. $sort,
  488. $clustername,
  489. $get_metric_string,
  490. $cluster,
  491. $always_timestamp,
  492. $reports[$metricname],
  493. $clustergraphsize,
  494. $range,
  495. $cs,
  496. $ce,
  497. $vlabel,
  498. $data);
  499. // No reason to go on if we have no up hosts.
  500. if (!is_array($hosts_up) or !$showhosts) {
  501. $dwoo->output($tpl, $data);
  502. return;
  503. }
  504. ///////////////////////////////////////////////////////////////////////////////
  505. // Creates a heatmap
  506. ///////////////////////////////////////////////////////////////////////////////
  507. if (isset($conf['heatmaps_enabled']) and $conf['heatmaps_enabled'] == 1)
  508. get_load_heatmap($hosts_up, $user, $metrics, $data);
  509. ///////////////////////////////////////////////////////////////////////////////
  510. // Show stacked graphs
  511. ///////////////////////////////////////////////////////////////////////////////
  512. if (isset($conf['show_stacked_graphs']) and
  513. $conf['show_stacked_graphs'] == 1 and
  514. ! preg_match("/_report$/", $metricname)) {
  515. $cluster_url = rawurlencode($clustername);
  516. $stacked_args = "m=$metricname&c=$cluster_url&r=$range&st=$cluster[LOCALTIME]";
  517. if (isset($user['host_regex']))
  518. $stacked_args .= "&host_regex=" . $user['host_regex'];
  519. $data->assign("stacked_graph_args", $stacked_args);
  520. }
  521. $dwoo->output($tpl, $data);
  522. ?>