cluster_controller.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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. updateLoadStatus:function (item) {
  24. var loadList = this.get('dataLoadList');
  25. var loaded = true;
  26. loadList.set(item, true);
  27. for (var i in loadList) {
  28. if (loadList.hasOwnProperty(i) && !loadList[i] && loaded) {
  29. loaded = false;
  30. }
  31. }
  32. this.set('isLoaded', loaded);
  33. },
  34. dataLoadList:Em.Object.create({
  35. 'hosts':false,
  36. 'services':false,
  37. 'cluster':false,
  38. 'racks':false,
  39. 'alerts':false,
  40. 'users':false
  41. }),
  42. postLoadList:{
  43. 'runs':false
  44. },
  45. /**
  46. * load cluster name
  47. */
  48. loadClusterName:function (reload) {
  49. if (this.get('clusterName') && !reload) {
  50. return;
  51. }
  52. var self = this;
  53. var url = (App.testMode) ? '/data/clusters/info.json' : App.apiPrefix + '/clusters';
  54. $.ajax({
  55. async:false,
  56. type:"GET",
  57. url:url,
  58. dataType:'json',
  59. timeout:App.timeout,
  60. success:function (data) {
  61. self.set('cluster', data.items[0]);
  62. },
  63. error:function (request, ajaxOptions, error) {
  64. console.log('failed on loading cluster name');
  65. self.set('isLoaded', true);
  66. },
  67. statusCode:require('data/statusCodes')
  68. });
  69. },
  70. getUrl:function (testUrl, url) {
  71. return (App.testMode) ? testUrl : App.apiPrefix + '/clusters/' + this.get('clusterName') + url;
  72. },
  73. /**
  74. * Provides the URL to use for Ganglia server. This URL
  75. * is helpful in populating links in UI.
  76. *
  77. * If null is returned, it means GANGLIA service is not installed.
  78. */
  79. gangliaUrl: function () {
  80. if (App.testMode) {
  81. return 'http://gangliaserver/ganglia/?t=yes';
  82. } else {
  83. // We want live data here
  84. var svcs = App.Service.find();
  85. var gangliaSvc = svcs.findProperty("serviceName", "GANGLIA");
  86. if (gangliaSvc) {
  87. var svcComponents = gangliaSvc.get('components');
  88. if (svcComponents) {
  89. var gangliaSvcComponent = svcComponents.findProperty("componentName", "GANGLIA_SERVER");
  90. if (gangliaSvcComponent) {
  91. var hostName = gangliaSvcComponent.get('host.hostName');
  92. if (hostName) {
  93. var host = App.Host.find(hostName);
  94. if (host) {
  95. hostName = host.get('publicHostName');
  96. }
  97. return "http://" + hostName + "/ganglia";
  98. }
  99. }
  100. }
  101. }
  102. return null;
  103. }
  104. }.property('App.router.updateController.isUpdated', 'dataLoadList.hosts'),
  105. /**
  106. * Provides the URL to use for NAGIOS server. This URL
  107. * is helpful in getting alerts data from server and also
  108. * in populating links in UI.
  109. *
  110. * If null is returned, it means NAGIOS service is not installed.
  111. */
  112. nagiosUrl:function () {
  113. if (App.testMode) {
  114. return 'http://nagiosserver/nagios';
  115. } else {
  116. // We want live data here
  117. var svcs = App.Service.find();
  118. var nagiosSvc = svcs.findProperty("serviceName", "NAGIOS");
  119. if (nagiosSvc) {
  120. var svcComponents = nagiosSvc.get('components');
  121. if (svcComponents) {
  122. var nagiosSvcComponent = svcComponents.findProperty("componentName", "NAGIOS_SERVER");
  123. if (nagiosSvcComponent) {
  124. var hostName = nagiosSvcComponent.get('host.hostName');
  125. if (hostName) {
  126. var host = App.Host.find(hostName);
  127. if (host) {
  128. hostName = host.get('publicHostName');
  129. }
  130. return "http://" + hostName + "/nagios";
  131. }
  132. }
  133. }
  134. }
  135. return null;
  136. }
  137. }.property('App.router.updateController.isUpdated'),
  138. isNagiosInstalled:function () {
  139. if (App.testMode) {
  140. return true;
  141. } else {
  142. var svcs = App.Service.find();
  143. var nagiosSvc = svcs.findProperty("serviceName", "NAGIOS");
  144. return nagiosSvc != null;
  145. }
  146. }.property('App.router.updateController.isUpdated'),
  147. /**
  148. * Sorted list of alerts.
  149. * Changes whenever alerts are loaded.
  150. */
  151. alerts:[],
  152. updateAlerts: function(){
  153. var alerts = App.Alert.find();
  154. var alertsArray = alerts.toArray();
  155. var sortedArray = alertsArray.sort(function (left, right) {
  156. var statusDiff = right.get('status') - left.get('status');
  157. if (statusDiff == 0) { // same error severity - sort by time
  158. var rightTime = right.get('date');
  159. var leftTime = left.get('time');
  160. rightTime = rightTime ? rightTime.getTime() : 0;
  161. leftTime = leftTime ? leftTime.getTime() : 0;
  162. statusDiff = rightTime - leftTime;
  163. }
  164. return statusDiff;
  165. });
  166. this.set('alerts', sortedArray);
  167. },
  168. loadRuns:function () {
  169. if (this.get('postLoadList.runs')) {
  170. return;
  171. }
  172. var self = this;
  173. var runsUrl = App.testMode ? "/data/apps/runs.json" : App.apiPrefix + "/jobhistory/workflow";
  174. App.HttpClient.get(runsUrl, App.runsMapper, {
  175. complete:function (jqXHR, textStatus) {
  176. self.set('postLoadList.runs', true);
  177. }
  178. }, function () {
  179. self.set('postLoadList.runs', true);
  180. });
  181. },
  182. /**
  183. * This method automatically loads alerts when Nagios URL
  184. * changes. Once done it will trigger dataLoadList.alerts
  185. * property, which will trigger the alerts property.
  186. */
  187. loadAlerts:function () {
  188. if(App.router.get('updateController.isUpdated')){
  189. return;
  190. }
  191. var nagiosUrl = this.get('nagiosUrl');
  192. if (nagiosUrl) {
  193. var lastSlash = nagiosUrl.lastIndexOf('/');
  194. if (lastSlash > -1) {
  195. nagiosUrl = nagiosUrl.substring(0, lastSlash);
  196. }
  197. var dataUrl;
  198. var ajaxOptions = {
  199. dataType:"jsonp",
  200. jsonp:"jsonp",
  201. context:this,
  202. complete:function (jqXHR, textStatus) {
  203. this.updateLoadStatus('alerts');
  204. this.updateAlerts();
  205. },
  206. error: function(jqXHR, testStatus, error) {
  207. // this.showMessage(Em.I18n.t('nagios.alerts.unavailable'));
  208. console.log('Nagios $.ajax() response:', error);
  209. }
  210. };
  211. if (App.testMode) {
  212. dataUrl = "/data/alerts/alerts.jsonp";
  213. ajaxOptions.jsonpCallback = "jQuery172040994187095202506_1352498338217";
  214. } else {
  215. dataUrl = nagiosUrl + "/hdp/nagios/nagios_alerts.php?q1=alerts&alert_type=all";
  216. }
  217. App.HttpClient.get(dataUrl, App.alertsMapper, ajaxOptions);
  218. } else {
  219. this.updateLoadStatus('alerts');
  220. console.log("No Nagios URL provided.")
  221. }
  222. }.observes('nagiosUrl'),
  223. /**
  224. * Show message in UI
  225. */
  226. showMessage: function(message){
  227. App.ModalPopup.show({
  228. header: 'Message',
  229. body: message,
  230. onPrimary: function() {
  231. this.hide();
  232. },
  233. secondary : null
  234. });
  235. },
  236. componentsUpdateInterval: App.componentsUpdateInterval,
  237. /**
  238. * Whether we need to update statuses automatically or not
  239. */
  240. updateStatus: false,
  241. statusTimeoutId: null,
  242. loadUpdatedStatusDelayed: function(delay){
  243. delay = delay || this.get('componentsUpdateInterval');
  244. var self = this;
  245. this.set('statusTimeoutId',
  246. setTimeout(function(){
  247. self.loadUpdatedStatus();
  248. }, delay)
  249. );
  250. },
  251. loadUpdatedStatus: function(){
  252. var timeoutId = this.get('statusTimeoutId');
  253. if(timeoutId){
  254. clearTimeout(timeoutId);
  255. this.set('statusTimeoutId', null);
  256. }
  257. if(!this.get('updateStatus')){
  258. return false;
  259. }
  260. if(!this.get('clusterName')){
  261. this.loadUpdatedStatusDelayed(this.get('componentsUpdateInterval')/2, 'error:clusterName');
  262. return;
  263. }
  264. var servicesUrl = this.getUrl('/data/dashboard/services.json', '/services?fields=components/ServiceComponentInfo,components/host_components,components/host_components/HostRoles');
  265. var self = this;
  266. App.HttpClient.get(servicesUrl, App.statusMapper, {
  267. complete:function (jqXHR, textStatus) {
  268. console.log('Cluster Controller: Updated components statuses successfully!!!')
  269. self.loadUpdatedStatusDelayed();
  270. }
  271. }, function(){
  272. self.loadUpdatedStatusDelayed(null, 'error:response error');
  273. });
  274. }.observes('updateStatus'),
  275. /**
  276. *
  277. * load all data and update load status
  278. */
  279. loadClusterData:function () {
  280. var self = this;
  281. if (!this.get('clusterName')) {
  282. return;
  283. }
  284. if(this.get('isLoaded')) { // do not load data repeatedly
  285. return;
  286. }
  287. var clusterUrl = this.getUrl('/data/clusters/cluster.json', '?fields=Clusters');
  288. var hostsUrl = this.getUrl('/data/hosts/hosts.json', '/hosts?fields=Hosts,host_components');
  289. var usersUrl = App.testMode ? '/data/users/users.json' : App.apiPrefix + '/users/?fields=*';
  290. var racksUrl = "/data/racks/racks.json";
  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. App.HttpClient.get(hostsUrl, App.hostsMapper, {
  306. complete:function (jqXHR, textStatus) {
  307. self.updateLoadStatus('hosts');
  308. }
  309. }, function (jqXHR, textStatus) {
  310. self.updateLoadStatus('hosts');
  311. });
  312. App.HttpClient.get(usersUrl, App.usersMapper, {
  313. complete:function (jqXHR, textStatus) {
  314. self.updateLoadStatus('users');
  315. }
  316. }, function (jqXHR, textStatus) {
  317. self.updateLoadStatus('users');
  318. });
  319. App.router.get('updateController').updateServiceMetric(function(){
  320. self.updateLoadStatus('services');
  321. }, true);
  322. setTimeout(function(){
  323. self.set('updateStatus', true);
  324. }, this.get('componentsUpdateInterval')*2);
  325. },
  326. clusterName:function () {
  327. return (this.get('cluster')) ? this.get('cluster').Clusters.cluster_name : null;
  328. }.property('cluster')
  329. })