cluster_controller.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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. ambariProperties: null,
  24. clusterDataLoadedPercent: 'width:0', // 0 to 1
  25. /**
  26. * Whether we need to update statuses automatically or not
  27. */
  28. isWorking: false,
  29. updateLoadStatus:function (item) {
  30. var loadList = this.get('dataLoadList');
  31. var loaded = true;
  32. var numLoaded = 0;
  33. var loadListLength = 0;
  34. loadList.set(item, true);
  35. for (var i in loadList) {
  36. if (loadList.hasOwnProperty(i)) {
  37. loadListLength++;
  38. if(!loadList[i] && loaded){
  39. loaded = false;
  40. }
  41. }
  42. // calculate the number of true
  43. if (loadList.hasOwnProperty(i) && loadList[i]){
  44. numLoaded++;
  45. }
  46. }
  47. this.set('isLoaded', loaded);
  48. this.set('clusterDataLoadedPercent', 'width:' + (Math.floor(numLoaded / loadListLength * 100)).toString() + '%');
  49. },
  50. dataLoadList:Em.Object.create({
  51. 'hosts':false,
  52. 'serviceMetrics':false,
  53. 'services': false,
  54. 'cluster':false,
  55. 'clusterStatus':false,
  56. 'racks':false,
  57. 'alerts':false,
  58. 'users':false,
  59. 'datasets':false,
  60. 'targetclusters':false,
  61. 'status': false,
  62. 'componentConfigs': false
  63. }),
  64. /**
  65. * load cluster name
  66. */
  67. loadClusterName:function (reload) {
  68. if (this.get('clusterName') && !reload) {
  69. return;
  70. }
  71. App.ajax.send({
  72. name: 'cluster.load_cluster_name',
  73. sender: this,
  74. success: 'loadClusterNameSuccessCallback',
  75. error: 'loadClusterNameErrorCallback'
  76. });
  77. if(!App.get('currentStackVersion')){
  78. App.set('currentStackVersion', App.defaultStackVersion);
  79. }
  80. },
  81. loadClusterNameSuccessCallback: function (data) {
  82. this.set('cluster', data.items[0]);
  83. App.set('clusterName', data.items[0].Clusters.cluster_name);
  84. App.set('currentStackVersion', data.items[0].Clusters.version);
  85. },
  86. loadClusterNameErrorCallback: function (request, ajaxOptions, error) {
  87. console.log('failed on loading cluster name');
  88. this.set('isLoaded', true);
  89. },
  90. getUrl:function (testUrl, url) {
  91. return (App.testMode) ? testUrl : App.apiPrefix + '/clusters/' + this.get('clusterName') + url;
  92. },
  93. /**
  94. * Provides the URL to use for Ganglia server. This URL
  95. * is helpful in populating links in UI.
  96. *
  97. * If null is returned, it means GANGLIA service is not installed.
  98. */
  99. gangliaUrl: function () {
  100. if (App.testMode) {
  101. return 'http://gangliaserver/ganglia/?t=yes';
  102. } else {
  103. // We want live data here
  104. var svcs = App.Service.find();
  105. var gangliaSvc = svcs.findProperty("serviceName", "GANGLIA");
  106. if (gangliaSvc) {
  107. var svcComponents = gangliaSvc.get('hostComponents');
  108. if (svcComponents) {
  109. var gangliaSvcComponent = svcComponents.findProperty("componentName", "GANGLIA_SERVER");
  110. if (gangliaSvcComponent) {
  111. var hostName = gangliaSvcComponent.get('host.hostName');
  112. if (hostName) {
  113. var host = App.Host.find(hostName);
  114. if (host) {
  115. hostName = host.get('publicHostName');
  116. }
  117. return this.get('gangliaWebProtocol') + "://" + (App.singleNodeInstall ? App.singleNodeAlias + ":42080" : hostName) + "/ganglia";
  118. }
  119. }
  120. }
  121. }
  122. return null;
  123. }
  124. }.property('App.router.updateController.isUpdated', 'dataLoadList.hosts','gangliaWebProtocol'),
  125. /**
  126. * Provides the URL to use for NAGIOS server. This URL
  127. * is helpful in getting alerts data from server and also
  128. * in populating links in UI.
  129. *
  130. * If null is returned, it means NAGIOS service is not installed.
  131. */
  132. nagiosUrl:function () {
  133. if (App.testMode) {
  134. return 'http://nagiosserver/nagios';
  135. } else {
  136. // We want live data here
  137. var svcs = App.Service.find();
  138. var nagiosSvc = svcs.findProperty("serviceName", "NAGIOS");
  139. if (nagiosSvc) {
  140. var svcComponents = nagiosSvc.get('hostComponents');
  141. if (svcComponents) {
  142. var nagiosSvcComponent = svcComponents.findProperty("componentName", "NAGIOS_SERVER");
  143. if (nagiosSvcComponent) {
  144. var hostName = nagiosSvcComponent.get('host.hostName');
  145. if (hostName) {
  146. var host = App.Host.find(hostName);
  147. if (host) {
  148. hostName = host.get('publicHostName');
  149. }
  150. return this.get('nagiosWebProtocol') + "://" + (App.singleNodeInstall ? App.singleNodeAlias + ":42080" : hostName) + "/nagios";
  151. }
  152. }
  153. }
  154. }
  155. return null;
  156. }
  157. }.property('App.router.updateController.isUpdated', 'dataLoadList.serviceMetrics', 'dataLoadList.hosts','nagiosWebProtocol'),
  158. nagiosWebProtocol: function () {
  159. var properties = this.get('ambariProperties');
  160. if (properties && properties.hasOwnProperty('nagios.https') && properties['nagios.https']) {
  161. return "https";
  162. } else {
  163. return "http";
  164. }
  165. }.property('ambariProperties'),
  166. gangliaWebProtocol: function () {
  167. var properties = this.get('ambariProperties');
  168. if (properties && properties.hasOwnProperty('ganglia.https') && properties['ganglia.https']) {
  169. return "https";
  170. } else {
  171. return "http";
  172. }
  173. }.property('ambariProperties'),
  174. isNagiosInstalled:function () {
  175. return !!App.Service.find().findProperty('serviceName', 'NAGIOS');
  176. }.property('App.router.updateController.isUpdated', 'dataLoadList.serviceMetrics'),
  177. isGangliaInstalled:function () {
  178. return !!App.Service.find().findProperty('serviceName', 'GANGLIA');
  179. }.property('App.router.updateController.isUpdated', 'dataLoadList.serviceMetrics'),
  180. /**
  181. * Sorted list of alerts.
  182. * Changes whenever alerts are loaded.
  183. */
  184. alerts:[],
  185. alertsHostMap: {},
  186. alertsServiceMap: {},
  187. updateAlerts: function(){
  188. var alerts = App.Alert.find().toArray();
  189. var alertsHostMap = {};
  190. var alertsServiceMap = {};
  191. alerts.forEach(function (alert) {
  192. if (!alert.get('isOk')) {
  193. if (!alert.get('ignoredForHosts')) {
  194. if (alertsHostMap[alert.get('hostName')]) {
  195. alertsHostMap[alert.get('hostName')]++;
  196. } else {
  197. alertsHostMap[alert.get('hostName')] = 1;
  198. }
  199. }
  200. if (!alert.get('ignoredForServices')) {
  201. if (alertsServiceMap[alert.get('serviceType')]) {
  202. alertsServiceMap[alert.get('serviceType')]++;
  203. } else {
  204. alertsServiceMap[alert.get('serviceType')] = 1;
  205. }
  206. }
  207. }
  208. }, this);
  209. this.set('alertsHostMap', alertsHostMap);
  210. this.set('alertsServiceMap', alertsServiceMap);
  211. this.set('alerts', alerts);
  212. },
  213. /**
  214. * Load alerts from server
  215. * @param callback Slave function, should be called to fire delayed update.
  216. * Look at <code>App.updater.run</code> for more information.
  217. * Also used to set <code>dataLoadList.alerts</code> status during app loading
  218. */
  219. loadAlerts:function (callback) {
  220. if (this.get('isNagiosInstalled')) {
  221. var testUrl = App.get('isHadoop2Stack') ? '/data/alerts/HDP2/alerts.json':'/data/alerts/alerts.json';
  222. var dataUrl = this.getUrl(testUrl, '/host_components?fields=HostRoles/nagios_alerts&HostRoles/component_name=NAGIOS_SERVER');
  223. var self = this;
  224. var ajaxOptions = {
  225. dataType:"json",
  226. complete:function () {
  227. self.updateAlerts();
  228. callback();
  229. },
  230. error: function(jqXHR, testStatus, error) {
  231. console.log('Nagios $.ajax() response:', error);
  232. }
  233. };
  234. App.HttpClient.get(dataUrl, App.alertsMapper, ajaxOptions);
  235. } else {
  236. console.log("No Nagios URL provided.");
  237. callback();
  238. }
  239. },
  240. /**
  241. * Determination of Nagios presence is known only after App.Service is
  242. * loaded from server. When that is done, no one tells alerts to load,
  243. * due to which alerts are not loaded & shown till the next polling cycle.
  244. * This method immediately loads alerts once Nagios presence is known.
  245. */
  246. isNagiosInstalledListener: function () {
  247. var self = this;
  248. self.loadAlerts(function () {
  249. self.updateLoadStatus('alerts');
  250. });
  251. }.observes('isNagiosInstalled'),
  252. /**
  253. * Send request to server to load components updated statuses
  254. * @param callback Slave function, should be called to fire delayed update.
  255. * Look at <code>App.updater.run</code> for more information
  256. * @return {Boolean} Whether we have errors
  257. */
  258. loadUpdatedStatus: function(callback){
  259. if(!this.get('clusterName')){
  260. callback();
  261. return false;
  262. }
  263. var testUrl = App.get('isHadoop2Stack') ? '/data/hosts/HDP2/hc_host_status.json':'/data/dashboard/services.json';
  264. var statusUrl = '/hosts?fields=Hosts/host_status,host_components/HostRoles/state&minimal_response=true';
  265. //desired_state property is eliminated since calculateState function is commented out, it become useless
  266. statusUrl = this.getUrl(testUrl, statusUrl);
  267. App.HttpClient.get(statusUrl, App.statusMapper, {
  268. complete: callback
  269. });
  270. return true;
  271. },
  272. /**
  273. * Run <code>loadUpdatedStatus</code> with delay
  274. * @param delay
  275. */
  276. loadUpdatedStatusDelayed: function(delay){
  277. setTimeout(function(){
  278. App.updater.immediateRun('loadUpdatedStatus');
  279. }, delay);
  280. },
  281. /**
  282. * Start polling, when <code>isWorking</code> become true
  283. */
  284. startPolling: function(){
  285. if(!this.get('isWorking')){
  286. return false;
  287. }
  288. App.updater.run(this, 'loadUpdatedStatus', 'isWorking', App.componentsUpdateInterval); //update will not run it immediately
  289. App.updater.run(this, 'loadAlerts', 'isWorking'); //update will not run it immediately
  290. return true;
  291. }.observes('isWorking'),
  292. /**
  293. *
  294. * load all data and update load status
  295. */
  296. loadClusterData:function () {
  297. var self = this;
  298. this.loadAmbariProperties();
  299. if (!this.get('clusterName')) {
  300. return;
  301. }
  302. if(this.get('isLoaded')) { // do not load data repeatedly
  303. return;
  304. }
  305. var clusterUrl = this.getUrl('/data/clusters/cluster.json', '?fields=Clusters');
  306. var hostsRealUrl = '/hosts?fields=Hosts/host_name,Hosts/public_host_name,Hosts/cpu_count,Hosts/ph_cpu_count,Hosts/total_mem,' +
  307. 'Hosts/host_status,Hosts/last_heartbeat_time,Hosts/os_arch,Hosts/os_type,Hosts/ip,host_components,Hosts/disk_info,' +
  308. 'metrics/disk,metrics/load/load_one,metrics/cpu/cpu_system,metrics/cpu/cpu_user,metrics/memory/mem_total,metrics/memory/mem_free'+
  309. '&minimal_response=true';
  310. var usersUrl = App.testMode ? '/data/users/users.json' : App.apiPrefix + '/users/?fields=*';
  311. var racksUrl = "/data/racks/racks.json";
  312. var dataSetUrl = "/data/mirroring/all_datasets.json";
  313. var targetClusterUrl = "/data/mirroring/target_clusters.json";
  314. if (App.supports.mirroring) {
  315. App.HttpClient.get(targetClusterUrl, App.targetClusterMapper, {
  316. complete: function (jqXHR, textStatus) {
  317. self.updateLoadStatus('targetclusters');
  318. }
  319. }, function (jqXHR, textStatus) {
  320. self.updateLoadStatus('targetclusters');
  321. });
  322. App.HttpClient.get(dataSetUrl, App.dataSetMapper, {
  323. complete: function (jqXHR, textStatus) {
  324. self.updateLoadStatus('datasets');
  325. }
  326. }, function (jqXHR, textStatus) {
  327. self.updateLoadStatus('datasets');
  328. });
  329. } else {
  330. self.updateLoadStatus('targetclusters');
  331. self.updateLoadStatus('datasets');
  332. }
  333. App.HttpClient.get(racksUrl, App.racksMapper, {
  334. complete:function (jqXHR, textStatus) {
  335. self.updateLoadStatus('racks');
  336. }
  337. }, function (jqXHR, textStatus) {
  338. self.updateLoadStatus('racks');
  339. });
  340. App.HttpClient.get(clusterUrl, App.clusterMapper, {
  341. complete:function (jqXHR, textStatus) {
  342. self.updateLoadStatus('cluster');
  343. }
  344. }, function (jqXHR, textStatus) {
  345. self.updateLoadStatus('cluster');
  346. });
  347. if (App.testMode) {
  348. self.updateLoadStatus('clusterStatus');
  349. } else {
  350. App.clusterStatus.updateFromServer(true).complete(function() {
  351. self.updateLoadStatus('clusterStatus');
  352. });
  353. }
  354. App.HttpClient.get(usersUrl, App.usersMapper, {
  355. complete:function (jqXHR, textStatus) {
  356. self.updateLoadStatus('users');
  357. }
  358. }, function (jqXHR, textStatus) {
  359. self.updateLoadStatus('users');
  360. });
  361. /**
  362. * Order of loading:
  363. * 1. put services in cache
  364. * 2. load host-components to model
  365. * 3. load services from cache with metrics to model
  366. * 4. update stale_configs of host-components (depends on App.supports.hostOverrides)
  367. * 5. load hosts to model
  368. */
  369. App.router.get('updateController').updateServices(function () {
  370. self.updateLoadStatus('services');
  371. self.loadUpdatedStatus(function () {
  372. self.updateLoadStatus('status');
  373. App.router.get('updateController').updateServiceMetric(function () {
  374. if (App.supports.hostOverrides) {
  375. App.router.get('updateController').updateComponentConfig(function () {
  376. self.updateLoadStatus('componentConfigs');
  377. });
  378. } else {
  379. self.updateLoadStatus('componentConfigs');
  380. }
  381. self.updateLoadStatus('serviceMetrics');
  382. self.requestHosts(hostsRealUrl, function (jqXHR, textStatus) {
  383. self.updateLoadStatus('hosts');
  384. });
  385. }, true);
  386. });
  387. });
  388. this.loadAlerts(function(){
  389. self.updateLoadStatus('alerts');
  390. });
  391. },
  392. requestHosts: function(realUrl, callback){
  393. var testHostUrl = App.get('isHadoop2Stack') ? '/data/hosts/HDP2/hosts.json':'/data/hosts/hosts.json';
  394. var url = this.getUrl(testHostUrl, realUrl);
  395. App.HttpClient.get(url, App.hostsMapper, {
  396. complete: callback
  397. }, callback)
  398. },
  399. loadAmbariProperties: function() {
  400. App.ajax.send({
  401. name: 'ambari.service',
  402. sender: this,
  403. success: 'loadAmbariPropertiesSuccess',
  404. error: 'loadAmbariPropertiesError'
  405. });
  406. return this.get('ambariProperties');
  407. },
  408. loadAmbariPropertiesSuccess: function(data) {
  409. console.log('loading ambari properties');
  410. this.set('ambariProperties', data.RootServiceComponents.properties);
  411. },
  412. loadAmbariPropertiesError: function() {
  413. console.warn('can\'t get ambari properties');
  414. },
  415. clusterName:function () {
  416. return (this.get('cluster')) ? this.get('cluster').Clusters.cluster_name : null;
  417. }.property('cluster'),
  418. updateClusterData: function () {
  419. var testUrl = App.get('isHadoop2Stack') ? '/data/clusters/HDP2/cluster.json':'/data/clusters/cluster.json';
  420. var clusterUrl = this.getUrl(testUrl, '?fields=Clusters');
  421. App.HttpClient.get(clusterUrl, App.clusterMapper, {
  422. complete:function(){}
  423. });
  424. }
  425. });