cluster_controller.js 11 KB

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