dfshealth.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. (function () {
  19. "use strict";
  20. dust.loadSource(dust.compile($('#tmpl-dfshealth').html(), 'dfshealth'));
  21. dust.loadSource(dust.compile($('#tmpl-startup-progress').html(), 'startup-progress'));
  22. dust.loadSource(dust.compile($('#tmpl-datanode').html(), 'datanode-info'));
  23. dust.loadSource(dust.compile($('#tmpl-snapshot').html(), 'snapshot-info'));
  24. function load_overview() {
  25. var BEANS = [
  26. {"name": "nn", "url": "/jmx?qry=Hadoop:service=NameNode,name=NameNodeInfo"},
  27. {"name": "nnstat", "url": "/jmx?qry=Hadoop:service=NameNode,name=NameNodeStatus"},
  28. {"name": "fs", "url": "/jmx?qry=Hadoop:service=NameNode,name=FSNamesystemState"},
  29. {"name": "mem", "url": "/jmx?qry=java.lang:type=Memory"},
  30. ];
  31. var HELPERS = {
  32. 'helper_fs_max_objects': function (chunk, ctx, bodies, params) {
  33. var o = ctx.current();
  34. if (o.MaxObjects > 0) {
  35. chunk.write('(' + Math.round((o.FilesTotal + o.BlockTotal) / o.MaxObjects * 100) * 100 + ')%');
  36. }
  37. },
  38. 'helper_dir_status': function (chunk, ctx, bodies, params) {
  39. var j = ctx.current();
  40. for (var i in j) {
  41. chunk.write('<tr><td>' + i + '</td><td>' + j[i] + '</td><td>' + params.type + '</td></tr>');
  42. }
  43. }
  44. };
  45. var data = {};
  46. // Workarounds for the fact that JMXJsonServlet returns non-standard JSON strings
  47. function data_workaround(d) {
  48. d.nn.JournalTransactionInfo = JSON.parse(d.nn.JournalTransactionInfo);
  49. d.nn.NameJournalStatus = JSON.parse(d.nn.NameJournalStatus);
  50. d.nn.NameDirStatuses = JSON.parse(d.nn.NameDirStatuses);
  51. d.nn.NodeUsage = JSON.parse(d.nn.NodeUsage);
  52. d.nn.CorruptFiles = JSON.parse(d.nn.CorruptFiles);
  53. return d;
  54. }
  55. load_json(
  56. BEANS,
  57. function(d) {
  58. for (var k in d) {
  59. data[k] = d[k].beans[0];
  60. }
  61. data = data_workaround(data);
  62. render();
  63. },
  64. function (url, jqxhr, text, err) {
  65. show_err_msg('<p>Failed to retrieve data from ' + url + ', cause: ' + err + '</p>');
  66. });
  67. function render() {
  68. var base = dust.makeBase(HELPERS);
  69. dust.render('dfshealth', base.push(data), function(err, out) {
  70. $('#tab-overview').html(out);
  71. $('a[href="#tab-datanode"]').click(load_datanode_info);
  72. $('#ui-tabs a[href="#tab-overview"]').tab('show');
  73. });
  74. }
  75. }
  76. $('#ui-tabs a[href="#tab-overview"]').click(load_overview);
  77. function show_err_msg(msg) {
  78. $('#alert-panel-body').html(msg);
  79. $('#alert-panel').show();
  80. }
  81. function ajax_error_handler(url, jqxhr, text, err) {
  82. show_err_msg('<p>Failed to retrieve data from ' + url + ', cause: ' + err + '</p>');
  83. }
  84. function load_startup_progress() {
  85. function workaround(r) {
  86. function rename_property(o, s, d) {
  87. if (o[s] !== undefined) {
  88. o[d] = o[s];
  89. delete o[s];
  90. }
  91. }
  92. r.percentComplete *= 100;
  93. $.each(r.phases, function (idx, p) {
  94. p.percentComplete *= 100;
  95. $.each(p.steps, function (idx2, s) {
  96. s.percentComplete *= 100;
  97. // dust.js is confused by these optional keys in nested
  98. // structure, rename them
  99. rename_property(s, "desc", "stepDesc");
  100. rename_property(s, "file", "stepFile");
  101. rename_property(s, "size", "stepSize");
  102. });
  103. });
  104. return r;
  105. }
  106. $.get('/startupProgress', function (resp) {
  107. var data = workaround(resp);
  108. dust.render('startup-progress', data, function(err, out) {
  109. $('#tab-startup-progress').html(out);
  110. $('#ui-tabs a[href="#tab-startup-progress"]').tab('show');
  111. });
  112. }).error(ajax_error_handler);
  113. }
  114. $('#ui-tabs a[href="#tab-startup-progress"]').click(load_startup_progress);
  115. function load_datanode_info() {
  116. function workaround(r) {
  117. function node_map_to_array(nodes) {
  118. var res = [];
  119. for (var n in nodes) {
  120. var p = nodes[n];
  121. p.name = n;
  122. res.push(p);
  123. }
  124. return res;
  125. }
  126. r.LiveNodes = node_map_to_array(JSON.parse(r.LiveNodes));
  127. r.DeadNodes = node_map_to_array(JSON.parse(r.DeadNodes));
  128. r.DecomNodes = node_map_to_array(JSON.parse(r.DecomNodes));
  129. return r;
  130. }
  131. $.get('/jmx?qry=Hadoop:service=NameNode,name=NameNodeInfo', function (resp) {
  132. var data = workaround(resp.beans[0]);
  133. dust.render('datanode-info', data, function(err, out) {
  134. $('#tab-datanode').html(out);
  135. $('#ui-tabs a[href="#tab-datanode"]').tab('show');
  136. });
  137. }).error(ajax_error_handler);
  138. }
  139. $('a[href="#tab-datanode"]').click(load_datanode_info);
  140. function load_snapshot_info() {
  141. $.get('/jmx?qry=Hadoop:service=NameNode,name=FSNamesystemState', function (resp) {
  142. var data = JSON.parse(resp.beans[0].SnapshotStats);
  143. dust.render('snapshot-info', data, function(err, out) {
  144. $('#tab-snapshot').html(out);
  145. $('#ui-tabs a[href="#tab-snapshot"]').tab('show');
  146. });
  147. }).error(ajax_error_handler);
  148. }
  149. $('#ui-tabs a[href="#tab-snapshot"]').click(load_snapshot_info);
  150. var hash = window.location.hash;
  151. if (hash === "#tab-datanode") {
  152. load_datanode_info();
  153. } else if (hash === "#tab-snapshot") {
  154. load_snapshot_info();
  155. } else if (hash === "#tab-startup-progress") {
  156. load_startup_progress();
  157. } else {
  158. load_overview();
  159. }
  160. })();