cluster_controller.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. var App = require('app');
  19. App.ClusterController = Em.Controller.extend({
  20. name:'clusterController',
  21. cluster:null,
  22. isLoaded:false,
  23. updateLoadStatus:function (item) {
  24. var loadList = this.get('dataLoadList');
  25. var loaded = true;
  26. loadList.set(item, true);
  27. for (var i in loadList) {
  28. if (loadList.hasOwnProperty(i) && !loadList[i] && loaded) {
  29. loaded = false;
  30. }
  31. }
  32. this.set('isLoaded', loaded);
  33. },
  34. dataLoadList:Em.Object.create({
  35. 'hosts':false,
  36. 'services':false,
  37. 'cluster':false,
  38. 'racks':false,
  39. 'alerts':false,
  40. 'users':false
  41. }),
  42. postLoadList:{
  43. 'runs':false
  44. },
  45. /**
  46. * load cluster name
  47. */
  48. loadClusterName:function (reload) {
  49. if (this.get('clusterName') && !reload) {
  50. return;
  51. }
  52. var self = this;
  53. var url = (App.testMode) ? '/data/clusters/info.json' : App.apiPrefix + '/clusters';
  54. $.ajax({
  55. async:false,
  56. type:"GET",
  57. url:url,
  58. dataType:'json',
  59. timeout:App.timeout,
  60. success:function (data) {
  61. self.set('cluster', data.items[0]);
  62. },
  63. error:function (request, ajaxOptions, error) {
  64. console.log('failed on loading cluster name');
  65. self.set('isLoaded', true);
  66. },
  67. statusCode:require('data/statusCodes')
  68. });
  69. },
  70. getUrl:function (testUrl, url) {
  71. return (App.testMode) ? testUrl : App.apiPrefix + '/clusters/' + this.get('clusterName') + url;
  72. },
  73. /**
  74. * Provides the URL to use for Ganglia server. This URL
  75. * is helpful in populating links in UI.
  76. *
  77. * If null is returned, it means GANGLIA service is not installed.
  78. */
  79. gangliaUrl: function () {
  80. if (App.testMode) {
  81. return 'http://gangliaserver/ganglia/?t=yes';
  82. } else {
  83. // We want live data here
  84. var svcs = App.Service.find();
  85. var gangliaSvc = svcs.findProperty("serviceName", "GANGLIA");
  86. if (gangliaSvc) {
  87. var svcComponents = gangliaSvc.get('components');
  88. if (svcComponents) {
  89. var gangliaSvcComponent = svcComponents.findProperty("componentName", "GANGLIA_SERVER");
  90. if (gangliaSvcComponent) {
  91. var hostName = gangliaSvcComponent.get('host.hostName');
  92. if (hostName) {
  93. return "http://" + hostName + "/ganglia";
  94. }
  95. }
  96. }
  97. }
  98. return null;
  99. }
  100. }.property('dataLoadList.services'),
  101. /**
  102. * Provides the URL to use for NAGIOS server. This URL
  103. * is helpful in getting alerts data from server and also
  104. * in populating links in UI.
  105. *
  106. * If null is returned, it means NAGIOS service is not installed.
  107. */
  108. nagiosUrl:function () {
  109. if (App.testMode) {
  110. return 'http://nagiosserver/nagios';
  111. } else {
  112. // We want live data here
  113. var svcs = App.Service.find();
  114. var nagiosSvc = svcs.findProperty("serviceName", "NAGIOS");
  115. if (nagiosSvc) {
  116. var svcComponents = nagiosSvc.get('components');
  117. if (svcComponents) {
  118. var nagiosSvcComponent = svcComponents.findProperty("componentName", "NAGIOS_SERVER");
  119. if (nagiosSvcComponent) {
  120. var hostName = nagiosSvcComponent.get('host.hostName');
  121. if (hostName) {
  122. return "http://"+hostName+"/nagios";
  123. }
  124. }
  125. }
  126. }
  127. return null;
  128. }
  129. }.property('dataLoadList.services'),
  130. isNagiosInstalled:function () {
  131. if (App.testMode) {
  132. return true;
  133. } else {
  134. var svcs = App.Service.find();
  135. var nagiosSvc = svcs.findProperty("serviceName", "NAGIOS");
  136. return nagiosSvc != null;
  137. }
  138. }.property('dataLoadList.services'),
  139. /**
  140. * Sorted list of alerts.
  141. * Changes whenever alerts are loaded.
  142. */
  143. alerts:function () {
  144. var alerts = App.Alert.find();
  145. var alertsArray = alerts.toArray();
  146. var sortedArray = alertsArray.sort(function (left, right) {
  147. var statusDiff = right.get('status') - left.get('status');
  148. if (statusDiff == 0) { // same error severity - sort by time
  149. var rightTime = right.get('date');
  150. var leftTime = left.get('time');
  151. rightTime = rightTime ? rightTime.getTime() : 0;
  152. leftTime = leftTime ? leftTime.getTime() : 0;
  153. statusDiff = rightTime - leftTime;
  154. }
  155. return statusDiff;
  156. });
  157. return sortedArray;
  158. }.property('dataLoadList.alerts'),
  159. loadRuns: function(){
  160. if(this.get('postLoadList.runs')){
  161. return;
  162. }
  163. var self= this;
  164. var runsUrl = App.testMode ? "/data/apps/runs.json" : App.apiPrefix + "/jobhistory/workflow";
  165. App.HttpClient.get(runsUrl, App.runsMapper, {
  166. complete:function (jqXHR, textStatus) {
  167. self.set('postLoadList.runs', true);
  168. }
  169. }, function(){
  170. self.set('postLoadList.runs', true);
  171. });
  172. },
  173. /**
  174. * This method automatically loads alerts when Nagios URL
  175. * changes. Once done it will trigger dataLoadList.alerts
  176. * property, which will trigger the alerts property.
  177. */
  178. loadAlerts:function () {
  179. var nagiosUrl = this.get('nagiosUrl');
  180. if (nagiosUrl) {
  181. var lastSlash = nagiosUrl.lastIndexOf('/');
  182. if (lastSlash > -1) {
  183. nagiosUrl = nagiosUrl.substring(0, lastSlash);
  184. }
  185. var dataUrl;
  186. var ajaxOptions = {
  187. dataType:"jsonp",
  188. jsonp:"jsonp",
  189. context:this,
  190. complete:function (jqXHR, textStatus) {
  191. this.updateLoadStatus('alerts')
  192. }
  193. };
  194. if (App.testMode) {
  195. dataUrl = "/data/alerts/alerts.jsonp";
  196. ajaxOptions.jsonpCallback = "jQuery172040994187095202506_1352498338217";
  197. } else {
  198. dataUrl = nagiosUrl + "/hdp/nagios/nagios_alerts.php?q1=alerts&alert_type=all";
  199. }
  200. App.HttpClient.get(dataUrl, App.alertsMapper, ajaxOptions);
  201. } else {
  202. this.updateLoadStatus('alerts');
  203. console.log("No Nagios URL provided.")
  204. }
  205. }.observes('nagiosUrl'),
  206. updateStatus: function(){
  207. var servicesUrl1 = this.getUrl('/data/dashboard/services.json', '/services?ServiceInfo/service_name!=MISCELLANEOUS&ServiceInfo/service_name!=DASHBOARD&fields=*,components/host_components/*');
  208. var self = this;
  209. App.HttpClient.get(servicesUrl1, App.statusMapper, {
  210. complete:function (jqXHR, textStatus) {
  211. console.log('update finished')
  212. setTimeout(function(){
  213. self.updateStatus();
  214. }, 3000);
  215. }
  216. }, null);
  217. },
  218. /**
  219. *
  220. * load all data and update load status
  221. */
  222. loadClusterData:function () {
  223. var self = this;
  224. if (!this.get('clusterName')) {
  225. return;
  226. }
  227. var clusterUrl = this.getUrl('/data/clusters/cluster.json', '?fields=Clusters');
  228. var hostsUrl = this.getUrl('/data/hosts/hosts.json', '/hosts?fields=*');
  229. var servicesUrl1 = this.getUrl('/data/dashboard/services.json', '/services?ServiceInfo/service_name!=MISCELLANEOUS&ServiceInfo/service_name!=DASHBOARD&fields=*,components/host_components/*');
  230. var servicesUrl2 = this.getUrl('/data/dashboard/serviceComponents.json', '/services?ServiceInfo/service_name!=MISCELLANEOUS&ServiceInfo/service_name!=DASHBOARD&fields=components/ServiceComponentInfo');
  231. var usersUrl = App.testMode ? '/data/users/users.json' : App.apiPrefix + '/users/?fields=*';
  232. var racksUrl = "/data/racks/racks.json";
  233. App.HttpClient.get(racksUrl, App.racksMapper, {
  234. complete:function (jqXHR, textStatus) {
  235. self.updateLoadStatus('racks');
  236. }
  237. }, function (jqXHR, textStatus) {
  238. self.updateLoadStatus('racks');
  239. });
  240. App.HttpClient.get(clusterUrl, App.clusterMapper, {
  241. complete:function (jqXHR, textStatus) {
  242. self.updateLoadStatus('cluster');
  243. }
  244. }, function (jqXHR, textStatus) {
  245. self.updateLoadStatus('cluster');
  246. });
  247. App.HttpClient.get(hostsUrl, App.hostsMapper, {
  248. complete:function (jqXHR, textStatus) {
  249. self.updateLoadStatus('hosts');
  250. }
  251. }, function (jqXHR, textStatus) {
  252. self.updateLoadStatus('hosts');
  253. });
  254. App.HttpClient.get(usersUrl, App.usersMapper, {
  255. complete:function (jqXHR, textStatus) {
  256. self.updateLoadStatus('users');
  257. }
  258. }, function (jqXHR, textStatus) {
  259. self.updateLoadStatus('users');
  260. });
  261. //////////////////////////////
  262. // Hack for services START //
  263. //////////////////////////////
  264. var metricsJson = null;
  265. var serviceComponentJson = null;
  266. var metricsMapper = {
  267. map:function (data) {
  268. metricsJson = data;
  269. }
  270. };
  271. var serviceComponentMapper = {
  272. map:function (data) {
  273. serviceComponentJson = data;
  274. if (metricsJson != null && serviceComponentJson != null) {
  275. var hdfsSvc1 = null;
  276. var hdfsSvc2 = null;
  277. var mrSvc1 = null;
  278. var mrSvc2 = null;
  279. var hbaseSvc1 = null;
  280. var hbaseSvc2 = null;
  281. metricsJson.items.forEach(function (svc) {
  282. if (svc.ServiceInfo.service_name == "HDFS") {
  283. hdfsSvc1 = svc;
  284. }
  285. if (svc.ServiceInfo.service_name == "MAPREDUCE") {
  286. mrSvc1 = svc;
  287. }
  288. if (svc.ServiceInfo.service_name == "HBASE") {
  289. hbaseSvc1 = svc;
  290. }
  291. });
  292. serviceComponentJson.items.forEach(function (svc) {
  293. if (svc.ServiceInfo.service_name == "HDFS") {
  294. hdfsSvc2 = svc;
  295. }
  296. if (svc.ServiceInfo.service_name == "MAPREDUCE") {
  297. mrSvc2 = svc;
  298. }
  299. if (svc.ServiceInfo.service_name == "HBASE") {
  300. hbaseSvc2 = svc;
  301. }
  302. });
  303. var nnC1 = null;
  304. var nnC2 = null;
  305. var jtC1 = null;
  306. var jtC2 = null;
  307. var hbm1 = null;
  308. var hbm2 = null;
  309. if (hdfsSvc1) {
  310. hdfsSvc1.components.forEach(function (c) {
  311. if (c.ServiceComponentInfo.component_name == "NAMENODE") {
  312. nnC1 = c;
  313. }
  314. });
  315. }
  316. if (hdfsSvc2) {
  317. hdfsSvc2.components.forEach(function (c) {
  318. if (c.ServiceComponentInfo.component_name == "NAMENODE") {
  319. nnC2 = c;
  320. }
  321. });
  322. }
  323. if (mrSvc1) {
  324. mrSvc1.components.forEach(function (c) {
  325. if (c.ServiceComponentInfo.component_name == "JOBTRACKER") {
  326. jtC1 = c;
  327. }
  328. });
  329. }
  330. if (mrSvc2) {
  331. mrSvc2.components.forEach(function (c) {
  332. if (c.ServiceComponentInfo.component_name == "JOBTRACKER") {
  333. jtC2 = c;
  334. }
  335. });
  336. }
  337. if (hbaseSvc1) {
  338. hbaseSvc1.components.forEach(function (c) {
  339. if (c.ServiceComponentInfo.component_name == "HBASE_MASTER") {
  340. hbm1 = c;
  341. }
  342. });
  343. }
  344. if (hbaseSvc2) {
  345. hbaseSvc2.components.forEach(function (c) {
  346. if (c.ServiceComponentInfo.component_name == "HBASE_MASTER") {
  347. hbm2 = c;
  348. }
  349. });
  350. }
  351. if (nnC1 && nnC2) {
  352. nnC1.ServiceComponentInfo = nnC2.ServiceComponentInfo;
  353. }
  354. if (jtC1 && jtC2) {
  355. jtC1.ServiceComponentInfo = jtC2.ServiceComponentInfo;
  356. }
  357. if (hbm1 && hbm2) {
  358. hbm1.ServiceComponentInfo = hbm2.ServiceComponentInfo;
  359. }
  360. App.servicesMapper.map(metricsJson);
  361. self.updateLoadStatus('services');
  362. }
  363. }
  364. }
  365. App.HttpClient.get(servicesUrl1, metricsMapper, {
  366. complete:function (jqXHR, textStatus) {
  367. App.HttpClient.get(servicesUrl2, serviceComponentMapper, {
  368. complete:function (jqXHR, textStatus) {
  369. }
  370. });
  371. }
  372. });
  373. /////////////////////////////
  374. // Hack for services END //
  375. /////////////////////////////
  376. setTimeout(function(){
  377. self.updateStatus();
  378. }, 8000);
  379. },
  380. clusterName:function () {
  381. return (this.get('cluster')) ? this.get('cluster').Clusters.cluster_name : null;
  382. }.property('cluster')
  383. })