federationhealth.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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-federationhealth').html(), 'federationhealth'));
  21. dust.loadSource(dust.compile($('#tmpl-namenode').html(), 'namenode-info'));
  22. dust.loadSource(dust.compile($('#tmpl-router').html(), 'router-info'));
  23. dust.loadSource(dust.compile($('#tmpl-datanode').html(), 'datanode-info'));
  24. dust.loadSource(dust.compile($('#tmpl-mounttable').html(), 'mounttable'));
  25. $.fn.dataTable.ext.order['ng-value'] = function (settings, col)
  26. {
  27. return this.api().column(col, {order:'index'} ).nodes().map(function (td, i) {
  28. return $(td).attr('ng-value');
  29. });
  30. };
  31. function load_overview() {
  32. var BEANS = [
  33. {"name": "federation", "url": "/jmx?qry=Hadoop:service=Router,name=FederationState"}
  34. ];
  35. var HELPERS = {
  36. 'helper_fs_max_objects': function (chunk, ctx, bodies, params) {
  37. var o = ctx.current();
  38. if (o.MaxObjects > 0) {
  39. chunk.write('(' + Math.round((o.FilesTotal + o.BlockTotal) / o.MaxObjects * 100) * 100 + ')%');
  40. }
  41. },
  42. 'helper_dir_status': function (chunk, ctx, bodies, params) {
  43. var j = ctx.current();
  44. for (var i in j) {
  45. chunk.write('<tr><td>' + i + '</td><td>' + j[i] + '</td><td>' + params.type + '</td></tr>');
  46. }
  47. },
  48. 'helper_date_tostring' : function (chunk, ctx, bodies, params) {
  49. var value = dust.helpers.tap(params.value, chunk, ctx);
  50. return chunk.write('' + new Date(Number(value)).toLocaleString());
  51. }
  52. };
  53. var data = {};
  54. // Workarounds for the fact that JMXJsonServlet returns non-standard JSON strings
  55. function workaround(nn) {
  56. nn.NodeUsage = JSON.parse(nn.NodeUsage);
  57. return nn;
  58. }
  59. load_json(
  60. BEANS,
  61. guard_with_startup_progress(function(d) {
  62. for (var k in d) {
  63. data[k] = k === 'federation' ? workaround(d[k].beans[0]) : d[k].beans[0];
  64. }
  65. render();
  66. }),
  67. function (url, jqxhr, text, err) {
  68. show_err_msg('<p>Failed to retrieve data from ' + url + ', cause: ' + err + '</p>');
  69. });
  70. function render() {
  71. var base = dust.makeBase(HELPERS);
  72. dust.render('federationhealth', base.push(data), function(err, out) {
  73. $('#tab-overview').html(out);
  74. $('#ui-tabs a[href="#tab-overview"]').tab('show');
  75. });
  76. }
  77. }
  78. function load_namenode_info() {
  79. var HELPERS = {
  80. 'helper_lastcontact_tostring' : function (chunk, ctx, bodies, params) {
  81. var value = dust.helpers.tap(params.value, chunk, ctx);
  82. return chunk.write('' + new Date(Date.now()-1000*Number(value)));
  83. }
  84. };
  85. function workaround(r) {
  86. function node_map_to_array(nodes) {
  87. var res = [];
  88. for (var n in nodes) {
  89. var p = nodes[n];
  90. p.name = n;
  91. res.push(p);
  92. }
  93. return res;
  94. }
  95. function capitalise(string) {
  96. return string.charAt(0).toUpperCase() + string.slice(1).toLowerCase();
  97. }
  98. function augment_namenodes(nodes) {
  99. for (var i = 0, e = nodes.length; i < e; ++i) {
  100. var n = nodes[i];
  101. n.usedPercentage = Math.round(n.used * 1.0 / n.totalSpace * 100);
  102. n.title = "Unavailable";
  103. n.iconState = "unavailable";
  104. if (n.isSafeMode === true) {
  105. n.title = capitalise(n.state) + " (safe mode)"
  106. n.iconState = "safemode";
  107. } else if (n.state === "ACTIVE") {
  108. n.title = capitalise(n.state);
  109. n.iconState = "active";
  110. } else if (nodes[i].state === "STANDBY") {
  111. n.title = capitalise(n.state);
  112. n.iconState = "standby";
  113. } else if (nodes[i].state === "UNAVAILABLE") {
  114. n.title = capitalise(n.state);
  115. n.iconState = "unavailable";
  116. }
  117. if (n.namenodeId === "null") {
  118. n.namenodeId = "";
  119. }
  120. }
  121. }
  122. r.Nameservices = node_map_to_array(JSON.parse(r.Nameservices));
  123. augment_namenodes(r.Nameservices);
  124. r.Namenodes = node_map_to_array(JSON.parse(r.Namenodes));
  125. augment_namenodes(r.Namenodes);
  126. return r;
  127. }
  128. $.get(
  129. '/jmx?qry=Hadoop:service=Router,name=FederationState',
  130. guard_with_startup_progress(function (resp) {
  131. var data = workaround(resp.beans[0]);
  132. var base = dust.makeBase(HELPERS);
  133. dust.render('namenode-info', base.push(data), function(err, out) {
  134. $('#tab-namenode').html(out);
  135. $('#ui-tabs a[href="#tab-namenode"]').tab('show');
  136. });
  137. })).error(ajax_error_handler);
  138. }
  139. function load_router_info() {
  140. var HELPERS = {
  141. 'helper_lastcontact_tostring' : function (chunk, ctx, bodies, params) {
  142. var value = dust.helpers.tap(params.value, chunk, ctx);
  143. return chunk.write('' + new Date(Date.now()-1000*Number(value)));
  144. }
  145. };
  146. function workaround(r) {
  147. function node_map_to_array(nodes) {
  148. var res = [];
  149. for (var n in nodes) {
  150. var p = nodes[n];
  151. p.name = n;
  152. res.push(p);
  153. }
  154. return res;
  155. }
  156. function capitalise(string) {
  157. return string.charAt(0).toUpperCase() + string.slice(1).toLowerCase();
  158. }
  159. function augment_routers(nodes) {
  160. for (var i = 0, e = nodes.length; i < e; ++i) {
  161. var n = nodes[i];
  162. n.title = "Unavailable"
  163. n.iconState = "unavailable";
  164. if (n.status === "INITIALIZING") {
  165. n.title = capitalise(n.status);
  166. n.iconState = "active";
  167. } else if (n.status === "RUNNING") {
  168. n.title = capitalise(n.status);
  169. n.iconState = "active";
  170. } else if (n.status === "SAFEMODE") {
  171. n.title = capitalise(n.status);
  172. n.iconState = "safemode";
  173. } else if (n.status === "STOPPING") {
  174. n.title = capitalise(n.status);
  175. n.iconState = "unavailable";
  176. } else if (n.status === "SHUTDOWN") {
  177. n.title = capitalise(n.status);
  178. n.iconState = "unavailable";
  179. }
  180. }
  181. }
  182. r.Routers = node_map_to_array(JSON.parse(r.Routers));
  183. augment_routers(r.Routers);
  184. return r;
  185. }
  186. $.get(
  187. '/jmx?qry=Hadoop:service=Router,name=FederationState',
  188. guard_with_startup_progress(function (resp) {
  189. var data = workaround(resp.beans[0]);
  190. var base = dust.makeBase(HELPERS);
  191. dust.render('router-info', base.push(data), function(err, out) {
  192. $('#tab-router').html(out);
  193. $('#ui-tabs a[href="#tab-router"]').tab('show');
  194. });
  195. })).error(ajax_error_handler);
  196. }
  197. // TODO Copied directly from dfshealth.js; is there a way to import this function?
  198. function load_datanode_info() {
  199. var HELPERS = {
  200. 'helper_relative_time' : function (chunk, ctx, bodies, params) {
  201. var value = dust.helpers.tap(params.value, chunk, ctx);
  202. return chunk.write(moment().subtract(Number(value), 'seconds').format('YYYY-MM-DD HH:mm:ss'));
  203. },
  204. 'helper_usage_bar' : function (chunk, ctx, bodies, params) {
  205. var value = dust.helpers.tap(params.value, chunk, ctx);
  206. var v = Number(value);
  207. var r = null;
  208. if (v < 70) {
  209. r = 'progress-bar-success';
  210. } else if (v < 85) {
  211. r = 'progress-bar-warning';
  212. } else {
  213. r = "progress-bar-danger";
  214. }
  215. return chunk.write(r);
  216. },
  217. };
  218. function workaround(r) {
  219. function node_map_to_array(nodes) {
  220. var res = [];
  221. for (var n in nodes) {
  222. var p = nodes[n];
  223. p.name = n;
  224. res.push(p);
  225. }
  226. return res;
  227. }
  228. function augment_live_nodes(nodes) {
  229. for (var i = 0, e = nodes.length; i < e; ++i) {
  230. var n = nodes[i];
  231. n.usedPercentage = Math.round((n.used + n.nonDfsUsedSpace) * 1.0 / n.capacity * 100);
  232. if (n.adminState === "In Service") {
  233. n.state = "alive";
  234. } else if (nodes[i].adminState === "Decommission In Progress") {
  235. n.state = "decommissioning";
  236. } else if (nodes[i].adminState === "Decommissioned") {
  237. n.state = "decommissioned";
  238. }
  239. }
  240. }
  241. function augment_dead_nodes(nodes) {
  242. for (var i = 0, e = nodes.length; i < e; ++i) {
  243. if (nodes[i].decommissioned) {
  244. nodes[i].state = "down-decommissioned";
  245. } else {
  246. nodes[i].state = "down";
  247. }
  248. }
  249. }
  250. r.LiveNodes = node_map_to_array(JSON.parse(r.LiveNodes));
  251. augment_live_nodes(r.LiveNodes);
  252. r.DeadNodes = node_map_to_array(JSON.parse(r.DeadNodes));
  253. augment_dead_nodes(r.DeadNodes);
  254. r.DecomNodes = node_map_to_array(JSON.parse(r.DecomNodes));
  255. return r;
  256. }
  257. $.get(
  258. '/jmx?qry=Hadoop:service=NameNode,name=NameNodeInfo',
  259. guard_with_startup_progress(function (resp) {
  260. var data = workaround(resp.beans[0]);
  261. var base = dust.makeBase(HELPERS);
  262. dust.render('datanode-info', base.push(data), function(err, out) {
  263. $('#tab-datanode').html(out);
  264. $('#table-datanodes').dataTable( {
  265. 'lengthMenu': [ [25, 50, 100, -1], [25, 50, 100, "All"] ],
  266. 'columns': [
  267. { 'orderDataType': 'ng-value', 'searchable': true },
  268. { 'orderDataType': 'ng-value', 'type': 'numeric' },
  269. { 'orderDataType': 'ng-value', 'type': 'numeric' },
  270. { 'orderDataType': 'ng-value', 'type': 'numeric'}
  271. ]});
  272. $('#ui-tabs a[href="#tab-datanode"]').tab('show');
  273. });
  274. })).error(ajax_error_handler);
  275. }
  276. function load_mount_table() {
  277. var HELPERS = {}
  278. function workaround(resource) {
  279. function augment_read_only(mountTable) {
  280. for (var i = 0, e = mountTable.length; i < e; ++i) {
  281. if (mountTable[i].readonly == true) {
  282. mountTable[i].readonly = "true"
  283. } else {
  284. mountTable[i].readonly = "false"
  285. }
  286. }
  287. }
  288. resource.MountTable = JSON.parse(resource.MountTable)
  289. augment_read_only(resource.MountTable)
  290. return resource;
  291. }
  292. $.get(
  293. '/jmx?qry=Hadoop:service=Router,name=FederationState',
  294. guard_with_startup_progress(function (resp) {
  295. var data = workaround(resp.beans[0]);
  296. var base = dust.makeBase(HELPERS);
  297. dust.render('mounttable', base.push(data), function(err, out) {
  298. $('#tab-mounttable').html(out);
  299. $('#ui-tabs a[href="#tab-mounttable"]').tab('show');
  300. });
  301. })).error(ajax_error_handler);
  302. }
  303. function toTitleCase(str) {
  304. return str.replace(/\w\S*/g, function(txt){
  305. return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
  306. });
  307. }
  308. function show_err_msg(msg) {
  309. $('#alert-panel-body').html(msg);
  310. $('#alert-panel').show();
  311. }
  312. function ajax_error_handler(url, jqxhr, text, err) {
  313. show_err_msg('<p>Failed to retrieve data from ' + url + ', cause: ' + err + '</p>');
  314. }
  315. function guard_with_startup_progress(fn) {
  316. return function() {
  317. try {
  318. fn.apply(this, arguments);
  319. } catch (err) {
  320. if (err instanceof TypeError) {
  321. show_err_msg('Router error: ' + err);
  322. }
  323. }
  324. };
  325. }
  326. function load_page() {
  327. var hash = window.location.hash;
  328. switch(hash) {
  329. case "#tab-overview":
  330. load_overview();
  331. break;
  332. case "#tab-namenode":
  333. load_namenode_info();
  334. break;
  335. case "#tab-router":
  336. load_router_info();
  337. break;
  338. case "#tab-datanode":
  339. load_datanode_info();
  340. break;
  341. case "#tab-mounttable":
  342. load_mount_table();
  343. break;
  344. default:
  345. window.location.hash = "tab-overview";
  346. break;
  347. }
  348. }
  349. load_page();
  350. $(window).bind('hashchange', function () {
  351. load_page();
  352. });
  353. })();