cluster_controller.js 11 KB

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