cluster_controller.js 14 KB

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