cluster_controller.js 10 KB

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