cluster_controller.js 10 KB

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