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