cluster_controller.js 14 KB

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