cluster_controller.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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': !App.supports.hostOverrides
  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.services', '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.services'),
  177. isGangliaInstalled:function () {
  178. return !!App.Service.find().findProperty('serviceName', 'GANGLIA');
  179. }.property('App.router.updateController.isUpdated', 'dataLoadList.services'),
  180. /**
  181. * Sorted list of alerts.
  182. * Changes whenever alerts are loaded.
  183. */
  184. alerts:[],
  185. updateAlerts: function(){
  186. var alerts = App.Alert.find();
  187. var alertsArray = alerts.toArray();
  188. var sortedArray = alertsArray.sort(function (left, right) {
  189. var statusDiff = right.get('status') - left.get('status');
  190. if (statusDiff == 0) { // same error severity - sort by time
  191. var rightTime = right.get('date');
  192. var leftTime = left.get('date');
  193. rightTime = rightTime ? rightTime.getTime() : 0;
  194. leftTime = leftTime ? leftTime.getTime() : 0;
  195. statusDiff = rightTime - leftTime;
  196. }
  197. return statusDiff;
  198. });
  199. this.set('alerts', sortedArray);
  200. },
  201. /**
  202. * Load alerts from server
  203. * @param callback Slave function, should be called to fire delayed update.
  204. * Look at <code>App.updater.run</code> for more information.
  205. * Also used to set <code>dataLoadList.alerts</code> status during app loading
  206. */
  207. loadAlerts:function (callback) {
  208. if (this.get('isNagiosInstalled')) {
  209. var testUrl = App.get('isHadoop2Stack') ? '/data/alerts/HDP2/alerts.json':'/data/alerts/alerts.json';
  210. var dataUrl = this.getUrl(testUrl, '/host_components?fields=HostRoles/nagios_alerts&HostRoles/component_name=NAGIOS_SERVER');
  211. var self = this;
  212. var ajaxOptions = {
  213. dataType:"json",
  214. complete:function () {
  215. self.updateAlerts();
  216. callback();
  217. },
  218. error: function(jqXHR, testStatus, error) {
  219. console.log('Nagios $.ajax() response:', error);
  220. }
  221. };
  222. App.HttpClient.get(dataUrl, App.alertsMapper, ajaxOptions);
  223. } else {
  224. console.log("No Nagios URL provided.");
  225. callback();
  226. }
  227. },
  228. /**
  229. * Determination of Nagios presence is known only after App.Service is
  230. * loaded from server. When that is done, no one tells alerts to load,
  231. * due to which alerts are not loaded & shown till the next polling cycle.
  232. * This method immediately loads alerts once Nagios presence is known.
  233. */
  234. isNagiosInstalledListener: function () {
  235. var self = this;
  236. self.loadAlerts(function () {
  237. self.updateLoadStatus('alerts');
  238. });
  239. }.observes('isNagiosInstalled'),
  240. /**
  241. * Send request to server to load components updated statuses
  242. * @param callback Slave function, should be called to fire delayed update.
  243. * Look at <code>App.updater.run</code> for more information
  244. * @return {Boolean} Whether we have errors
  245. */
  246. loadUpdatedStatus: function(callback){
  247. if(!this.get('clusterName')){
  248. callback();
  249. return false;
  250. }
  251. var testUrl = App.get('isHadoop2Stack') ? '/data/hosts/HDP2/hc_host_status.json':'/data/dashboard/services.json';
  252. var statusUrl = '/hosts?fields=Hosts/host_status,host_components/HostRoles/state&minimal_response=true';
  253. //desired_state property is eliminated since calculateState function is commented out, it become useless
  254. statusUrl = this.getUrl(testUrl, statusUrl);
  255. App.HttpClient.get(statusUrl, App.statusMapper, {
  256. complete: callback
  257. });
  258. return true;
  259. },
  260. /**
  261. * Run <code>loadUpdatedStatus</code> with delay
  262. * @param delay
  263. */
  264. loadUpdatedStatusDelayed: function(delay){
  265. setTimeout(function(){
  266. App.updater.immediateRun('loadUpdatedStatus');
  267. }, delay);
  268. },
  269. /**
  270. * Start polling, when <code>isWorking</code> become true
  271. */
  272. startPolling: function(){
  273. if(!this.get('isWorking')){
  274. return false;
  275. }
  276. App.updater.run(this, 'loadUpdatedStatus', 'isWorking', App.componentsUpdateInterval); //update will not run it immediately
  277. App.updater.run(this, 'loadAlerts', 'isWorking'); //update will not run it immediately
  278. return true;
  279. }.observes('isWorking'),
  280. /**
  281. *
  282. * load all data and update load status
  283. */
  284. loadClusterData:function () {
  285. var self = this;
  286. this.loadAmbariProperties();
  287. if (!this.get('clusterName')) {
  288. return;
  289. }
  290. if(this.get('isLoaded')) { // do not load data repeatedly
  291. return;
  292. }
  293. var clusterUrl = this.getUrl('/data/clusters/cluster.json', '?fields=Clusters');
  294. var hostsRealUrl = '/hosts?fields=Hosts/host_name,Hosts/public_host_name,Hosts/cpu_count,Hosts/total_mem,' +
  295. 'Hosts/host_status,Hosts/last_heartbeat_time,Hosts/os_arch,Hosts/os_type,Hosts/ip,host_components,Hosts/disk_info,' +
  296. 'metrics/disk,metrics/load/load_one,metrics/cpu/cpu_system,metrics/cpu/cpu_user,metrics/memory/mem_total,metrics/memory/mem_free'+
  297. '&minimal_response=true';
  298. var usersUrl = App.testMode ? '/data/users/users.json' : App.apiPrefix + '/users/?fields=*';
  299. var racksUrl = "/data/racks/racks.json";
  300. var dataSetUrl = "/data/mirroring/all_datasets.json";
  301. var targetClusterUrl = "/data/mirroring/target_clusters.json";
  302. App.HttpClient.get(targetClusterUrl, App.targetClusterMapper, {
  303. complete: function (jqXHR, textStatus) {
  304. self.updateLoadStatus('targetclusters');
  305. }
  306. }, function (jqXHR, textStatus) {
  307. self.updateLoadStatus('targetclusters');
  308. });
  309. App.HttpClient.get(dataSetUrl, App.dataSetMapper, {
  310. complete: function (jqXHR, textStatus) {
  311. self.updateLoadStatus('datasets');
  312. }
  313. }, function (jqXHR, textStatus) {
  314. self.updateLoadStatus('datasets');
  315. });
  316. App.HttpClient.get(racksUrl, App.racksMapper, {
  317. complete:function (jqXHR, textStatus) {
  318. self.updateLoadStatus('racks');
  319. }
  320. }, function (jqXHR, textStatus) {
  321. self.updateLoadStatus('racks');
  322. });
  323. App.HttpClient.get(clusterUrl, App.clusterMapper, {
  324. complete:function (jqXHR, textStatus) {
  325. self.updateLoadStatus('cluster');
  326. }
  327. }, function (jqXHR, textStatus) {
  328. self.updateLoadStatus('cluster');
  329. });
  330. if (App.testMode) {
  331. self.updateLoadStatus('clusterStatus');
  332. } else {
  333. App.clusterStatus.updateFromServer(true).complete(function() {
  334. self.updateLoadStatus('clusterStatus');
  335. });
  336. }
  337. this.requestHosts(hostsRealUrl, function (jqXHR, textStatus) {
  338. self.updateLoadStatus('hosts');
  339. });
  340. App.HttpClient.get(usersUrl, App.usersMapper, {
  341. complete:function (jqXHR, textStatus) {
  342. self.updateLoadStatus('users');
  343. }
  344. }, function (jqXHR, textStatus) {
  345. self.updateLoadStatus('users');
  346. });
  347. App.router.get('updateController').updateServices(function () {
  348. self.updateLoadStatus('services');
  349. self.loadUpdatedStatus(function () {
  350. self.updateLoadStatus('status');
  351. App.router.get('updateController').updateServiceMetric(function () {
  352. if (App.supports.hostOverrides) {
  353. App.router.get('updateController').updateComponentConfig(function () {
  354. self.updateLoadStatus('componentConfigs');
  355. });
  356. }
  357. self.updateLoadStatus('serviceMetrics');
  358. }, true);
  359. });
  360. });
  361. this.loadAlerts(function(){
  362. self.updateLoadStatus('alerts');
  363. });
  364. },
  365. requestHosts: function(realUrl, callback){
  366. var testHostUrl = App.get('isHadoop2Stack') ? '/data/hosts/HDP2/hosts.json':'/data/hosts/hosts.json';
  367. var url = this.getUrl(testHostUrl, realUrl);
  368. App.HttpClient.get(url, App.hostsMapper, {
  369. complete: callback
  370. }, callback)
  371. },
  372. loadAmbariProperties: function() {
  373. App.ajax.send({
  374. name: 'ambari.service',
  375. sender: this,
  376. success: 'loadAmbariPropertiesSuccess',
  377. error: 'loadAmbariPropertiesError'
  378. });
  379. return this.get('ambariProperties');
  380. },
  381. loadAmbariPropertiesSuccess: function(data) {
  382. console.log('loading ambari properties');
  383. this.set('ambariProperties', data.RootServiceComponents.properties);
  384. },
  385. loadAmbariPropertiesError: function() {
  386. console.warn('can\'t get ambari properties');
  387. },
  388. clusterName:function () {
  389. return (this.get('cluster')) ? this.get('cluster').Clusters.cluster_name : null;
  390. }.property('cluster'),
  391. updateClusterData: function () {
  392. var testUrl = App.get('isHadoop2Stack') ? '/data/clusters/HDP2/cluster.json':'/data/clusters/cluster.json';
  393. var clusterUrl = this.getUrl(testUrl, '?fields=Clusters');
  394. App.HttpClient.get(clusterUrl, App.clusterMapper, {
  395. complete:function(){}
  396. });
  397. }
  398. });