cluster_controller.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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.router.get('updateController.isUpdated')){
  180. return;
  181. }
  182. var nagiosUrl = this.get('nagiosUrl');
  183. if (nagiosUrl) {
  184. var lastSlash = nagiosUrl.lastIndexOf('/');
  185. if (lastSlash > -1) {
  186. nagiosUrl = nagiosUrl.substring(0, lastSlash);
  187. }
  188. var dataUrl = this.getUrl('/data/alerts/alerts.json', '/host_components?fields=HostRoles/nagios_alerts&HostRoles/component_name=NAGIOS_SERVER');
  189. var ajaxOptions = {
  190. dataType:"json",
  191. context:this,
  192. complete:function (jqXHR, textStatus) {
  193. this.updateLoadStatus('alerts');
  194. this.updateAlerts();
  195. },
  196. error: function(jqXHR, testStatus, error) {
  197. // this.showMessage(Em.I18n.t('nagios.alerts.unavailable'));
  198. console.log('Nagios $.ajax() response:', error);
  199. }
  200. };
  201. App.HttpClient.get(dataUrl, App.alertsMapper, ajaxOptions);
  202. } else {
  203. this.updateLoadStatus('alerts');
  204. console.log("No Nagios URL provided.")
  205. }
  206. }.observes('nagiosUrl'),
  207. /**
  208. * Show message in UI
  209. */
  210. showMessage: function(message){
  211. App.ModalPopup.show({
  212. header: 'Message',
  213. body: message,
  214. onPrimary: function() {
  215. this.hide();
  216. },
  217. secondary : null
  218. });
  219. },
  220. statusTimeoutId: null,
  221. loadUpdatedStatusDelayed: function(delay){
  222. delay = delay || App.componentsUpdateInterval;
  223. var self = this;
  224. this.set('statusTimeoutId',
  225. setTimeout(function(){
  226. self.loadUpdatedStatus();
  227. }, delay)
  228. );
  229. },
  230. loadUpdatedStatus: function(){
  231. var timeoutId = this.get('statusTimeoutId');
  232. if(timeoutId){
  233. clearTimeout(timeoutId);
  234. this.set('statusTimeoutId', null);
  235. }
  236. if(!this.get('isWorking')){
  237. return false;
  238. }
  239. if(!this.get('clusterName')){
  240. this.loadUpdatedStatusDelayed(this.get('componentsUpdateInterval')/2, 'error:clusterName');
  241. return;
  242. }
  243. var servicesUrl = this.getUrl('/data/dashboard/services.json', '/services?fields=components/ServiceComponentInfo,components/host_components,components/host_components/HostRoles');
  244. var self = this;
  245. App.HttpClient.get(servicesUrl, App.statusMapper, {
  246. complete:function (jqXHR, textStatus) {
  247. //console.log('Cluster Controller: Updated components statuses successfully!!!')
  248. self.loadUpdatedStatusDelayed();
  249. }
  250. }, function(){
  251. self.loadUpdatedStatusDelayed(null, 'error:response error');
  252. });
  253. },
  254. startLoadUpdatedStatus: function(){
  255. var self = this;
  256. this.set('isWorking', true);
  257. setTimeout(function(){
  258. self.loadUpdatedStatus();
  259. }, App.componentsUpdateInterval*2);
  260. },
  261. /**
  262. *
  263. * load all data and update load status
  264. */
  265. loadClusterData:function () {
  266. var self = this;
  267. if (!this.get('clusterName')) {
  268. return;
  269. }
  270. if(this.get('isLoaded')) { // do not load data repeatedly
  271. return;
  272. }
  273. var clusterUrl = this.getUrl('/data/clusters/cluster.json', '?fields=Clusters');
  274. var hostsUrl = this.getUrl('/data/hosts/hosts.json', '/hosts?fields=Hosts/host_name,Hosts/public_host_name,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/cpu,metrics/load,metrics/memory');
  275. var usersUrl = App.testMode ? '/data/users/users.json' : App.apiPrefix + '/users/?fields=*';
  276. var racksUrl = "/data/racks/racks.json";
  277. App.HttpClient.get(racksUrl, App.racksMapper, {
  278. complete:function (jqXHR, textStatus) {
  279. self.updateLoadStatus('racks');
  280. }
  281. }, function (jqXHR, textStatus) {
  282. self.updateLoadStatus('racks');
  283. });
  284. App.HttpClient.get(clusterUrl, App.clusterMapper, {
  285. complete:function (jqXHR, textStatus) {
  286. self.updateLoadStatus('cluster');
  287. }
  288. }, function (jqXHR, textStatus) {
  289. self.updateLoadStatus('cluster');
  290. });
  291. App.HttpClient.get(hostsUrl, App.hostsMapper, {
  292. complete:function (jqXHR, textStatus) {
  293. self.updateLoadStatus('hosts');
  294. }
  295. }, function (jqXHR, textStatus) {
  296. self.updateLoadStatus('hosts');
  297. });
  298. App.HttpClient.get(usersUrl, App.usersMapper, {
  299. complete:function (jqXHR, textStatus) {
  300. self.updateLoadStatus('users');
  301. }
  302. }, function (jqXHR, textStatus) {
  303. self.updateLoadStatus('users');
  304. });
  305. App.router.get('updateController').updateServiceMetric(function(){
  306. self.updateLoadStatus('services');
  307. }, true);
  308. },
  309. clusterName:function () {
  310. return (this.get('cluster')) ? this.get('cluster').Clusters.cluster_name : null;
  311. }.property('cluster')
  312. })