federationhealth.js 13 KB

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