cluster_controller.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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', 'dataLoadList.hosts'),
  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:function () {
  152. var alerts = App.Alert.find();
  153. var alertsArray = alerts.toArray();
  154. var sortedArray = alertsArray.sort(function (left, right) {
  155. var statusDiff = right.get('status') - left.get('status');
  156. if (statusDiff == 0) { // same error severity - sort by time
  157. var rightTime = right.get('date');
  158. var leftTime = left.get('time');
  159. rightTime = rightTime ? rightTime.getTime() : 0;
  160. leftTime = leftTime ? leftTime.getTime() : 0;
  161. statusDiff = rightTime - leftTime;
  162. }
  163. return statusDiff;
  164. });
  165. return sortedArray;
  166. }.property('dataLoadList.alerts'),
  167. loadRuns:function () {
  168. if (this.get('postLoadList.runs')) {
  169. return;
  170. }
  171. var self = this;
  172. var runsUrl = App.testMode ? "/data/apps/runs.json" : App.apiPrefix + "/jobhistory/workflow";
  173. App.HttpClient.get(runsUrl, App.runsMapper, {
  174. complete:function (jqXHR, textStatus) {
  175. self.set('postLoadList.runs', true);
  176. }
  177. }, function () {
  178. self.set('postLoadList.runs', true);
  179. });
  180. },
  181. /**
  182. * This method automatically loads alerts when Nagios URL
  183. * changes. Once done it will trigger dataLoadList.alerts
  184. * property, which will trigger the alerts property.
  185. */
  186. loadAlerts:function () {
  187. var nagiosUrl = this.get('nagiosUrl');
  188. if (nagiosUrl) {
  189. var lastSlash = nagiosUrl.lastIndexOf('/');
  190. if (lastSlash > -1) {
  191. nagiosUrl = nagiosUrl.substring(0, lastSlash);
  192. }
  193. var dataUrl;
  194. var ajaxOptions = {
  195. dataType:"jsonp",
  196. jsonp:"jsonp",
  197. context:this,
  198. complete:function (jqXHR, textStatus) {
  199. this.updateLoadStatus('alerts')
  200. },
  201. error: function(jqXHR, testStatus, error) {
  202. this.showMessage(Em.I18n.t('nagios.alerts.unavailable'));
  203. console.log('Nagios $.ajax() response:', error);
  204. }
  205. };
  206. if (App.testMode) {
  207. dataUrl = "/data/alerts/alerts.jsonp";
  208. ajaxOptions.jsonpCallback = "jQuery172040994187095202506_1352498338217";
  209. } else {
  210. dataUrl = nagiosUrl + "/hdp/nagios/nagios_alerts.php?q1=alerts&alert_type=all";
  211. }
  212. App.HttpClient.get(dataUrl, App.alertsMapper, ajaxOptions);
  213. } else {
  214. this.updateLoadStatus('alerts');
  215. console.log("No Nagios URL provided.")
  216. }
  217. }.observes('nagiosUrl'),
  218. /**
  219. * Show message in UI
  220. */
  221. showMessage: function(message){
  222. App.ModalPopup.show({
  223. header: 'Message',
  224. body: message,
  225. onPrimary: function() {
  226. this.hide();
  227. },
  228. secondary : null
  229. });
  230. },
  231. componentsUpdateInterval: App.componentsUpdateInterval,
  232. /**
  233. * Whether we need to update statuses automatically or not
  234. */
  235. updateStatus: false,
  236. statusTimeoutId: null,
  237. loadUpdatedStatusDelayed: function(delay){
  238. delay = delay || this.get('componentsUpdateInterval');
  239. var self = this;
  240. this.set('statusTimeoutId',
  241. setTimeout(function(){
  242. self.loadUpdatedStatus();
  243. }, delay)
  244. );
  245. },
  246. loadUpdatedStatus: function(){
  247. var timeoutId = this.get('statusTimeoutId');
  248. if(timeoutId){
  249. clearTimeout(timeoutId);
  250. this.set('statusTimeoutId', null);
  251. }
  252. if(!this.get('updateStatus')){
  253. return false;
  254. }
  255. if(!this.get('clusterName')){
  256. this.loadUpdatedStatusDelayed(this.get('componentsUpdateInterval')/2, 'error:clusterName');
  257. return;
  258. }
  259. var servicesUrl1 = this.getUrl('/data/dashboard/services.json', '/services?ServiceInfo/service_name!=MISCELLANEOUS&ServiceInfo/service_name!=DASHBOARD&fields=*,components/host_components/*');
  260. var self = this;
  261. App.HttpClient.get(servicesUrl1, App.statusMapper, {
  262. complete:function (jqXHR, textStatus) {
  263. console.log('Cluster Controller: Updated components statuses successfully!!!')
  264. self.loadUpdatedStatusDelayed();
  265. }
  266. }, function(){
  267. self.loadUpdatedStatusDelayed(null, 'error:response error');
  268. });
  269. }.observes('updateStatus'),
  270. /**
  271. *
  272. * load all data and update load status
  273. */
  274. loadClusterData:function () {
  275. var self = this;
  276. if (!this.get('clusterName')) {
  277. return;
  278. }
  279. if(this.get('isLoaded')) { // do not load data repeatedly
  280. return;
  281. }
  282. var clusterUrl = this.getUrl('/data/clusters/cluster.json', '?fields=Clusters');
  283. var hostsUrl = this.getUrl('/data/hosts/hosts.json', '/hosts?fields=*');
  284. var usersUrl = App.testMode ? '/data/users/users.json' : App.apiPrefix + '/users/?fields=*';
  285. var racksUrl = "/data/racks/racks.json";
  286. App.HttpClient.get(racksUrl, App.racksMapper, {
  287. complete:function (jqXHR, textStatus) {
  288. self.updateLoadStatus('racks');
  289. }
  290. }, function (jqXHR, textStatus) {
  291. self.updateLoadStatus('racks');
  292. });
  293. App.HttpClient.get(clusterUrl, App.clusterMapper, {
  294. complete:function (jqXHR, textStatus) {
  295. self.updateLoadStatus('cluster');
  296. }
  297. }, function (jqXHR, textStatus) {
  298. self.updateLoadStatus('cluster');
  299. });
  300. App.HttpClient.get(hostsUrl, App.hostsMapper, {
  301. complete:function (jqXHR, textStatus) {
  302. self.updateLoadStatus('hosts');
  303. }
  304. }, function (jqXHR, textStatus) {
  305. self.updateLoadStatus('hosts');
  306. });
  307. App.HttpClient.get(usersUrl, App.usersMapper, {
  308. complete:function (jqXHR, textStatus) {
  309. self.updateLoadStatus('users');
  310. }
  311. }, function (jqXHR, textStatus) {
  312. self.updateLoadStatus('users');
  313. });
  314. App.router.get('updateController').updateServiceMetric(function(){
  315. self.updateLoadStatus('services');
  316. });
  317. setTimeout(function(){
  318. self.set('updateStatus', true);
  319. }, this.get('componentsUpdateInterval')*2);
  320. },
  321. clusterName:function () {
  322. return (this.get('cluster')) ? this.get('cluster').Clusters.cluster_name : null;
  323. }.property('cluster')
  324. })