cluster_controller.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. clusterDataLoadedPercent: 'width:0', // 0 to 1
  24. /**
  25. * Whether we need to update statuses automatically or not
  26. */
  27. isWorking: false,
  28. updateLoadStatus:function (item) {
  29. var loadList = this.get('dataLoadList');
  30. var loaded = true;
  31. var numLoaded = 0;
  32. var loadListLength = 0;
  33. loadList.set(item, true);
  34. for (var i in loadList) {
  35. if (loadList.hasOwnProperty(i)) {
  36. loadListLength++;
  37. if(!loadList[i] && loaded){
  38. loaded = false;
  39. }
  40. }
  41. // calculate the number of true
  42. if (loadList.hasOwnProperty(i) && loadList[i]){
  43. numLoaded++;
  44. }
  45. }
  46. this.set('isLoaded', loaded);
  47. this.set('clusterDataLoadedPercent', 'width:' + (Math.floor(numLoaded / loadListLength * 100)).toString() + '%');
  48. },
  49. dataLoadList:Em.Object.create({
  50. 'hosts':false,
  51. 'services':false,
  52. 'cluster':false,
  53. 'clusterStatus':false,
  54. 'racks':false,
  55. 'alerts':false,
  56. 'users':false,
  57. 'datasets':false,
  58. 'targetclusters':false
  59. }),
  60. /**
  61. * load cluster name
  62. */
  63. loadClusterName:function (reload) {
  64. if (this.get('clusterName') && !reload) {
  65. return;
  66. }
  67. App.ajax.send({
  68. name: 'cluster.load_cluster_name',
  69. sender: this,
  70. success: 'loadClusterNameSuccessCallback',
  71. error: 'loadClusterNameErrorCallback'
  72. });
  73. if(!App.get('currentStackVersion')){
  74. App.set('currentStackVersion', App.defaultStackVersion);
  75. }
  76. },
  77. loadClusterNameSuccessCallback: function (data) {
  78. this.set('cluster', data.items[0]);
  79. App.set('clusterName', data.items[0].Clusters.cluster_name);
  80. App.set('currentStackVersion', data.items[0].Clusters.version);
  81. },
  82. loadClusterNameErrorCallback: function (request, ajaxOptions, error) {
  83. console.log('failed on loading cluster name');
  84. this.set('isLoaded', true);
  85. },
  86. getUrl:function (testUrl, url) {
  87. return (App.testMode) ? testUrl : App.apiPrefix + '/clusters/' + this.get('clusterName') + url;
  88. },
  89. /**
  90. * Provides the URL to use for Ganglia server. This URL
  91. * is helpful in populating links in UI.
  92. *
  93. * If null is returned, it means GANGLIA service is not installed.
  94. */
  95. gangliaUrl: function () {
  96. if (App.testMode) {
  97. return 'http://gangliaserver/ganglia/?t=yes';
  98. } else {
  99. // We want live data here
  100. var svcs = App.Service.find();
  101. var gangliaSvc = svcs.findProperty("serviceName", "GANGLIA");
  102. if (gangliaSvc) {
  103. var svcComponents = gangliaSvc.get('hostComponents');
  104. if (svcComponents) {
  105. var gangliaSvcComponent = svcComponents.findProperty("componentName", "GANGLIA_SERVER");
  106. if (gangliaSvcComponent) {
  107. var hostName = gangliaSvcComponent.get('host.hostName');
  108. if (hostName) {
  109. var host = App.Host.find(hostName);
  110. if (host) {
  111. hostName = host.get('publicHostName');
  112. }
  113. return "http://" + (App.singleNodeInstall ? App.singleNodeAlias : hostName) + "/ganglia";
  114. }
  115. }
  116. }
  117. }
  118. return null;
  119. }
  120. }.property('App.router.updateController.isUpdated', 'dataLoadList.hosts'),
  121. /**
  122. * Provides the URL to use for NAGIOS server. This URL
  123. * is helpful in getting alerts data from server and also
  124. * in populating links in UI.
  125. *
  126. * If null is returned, it means NAGIOS service is not installed.
  127. */
  128. nagiosUrl:function () {
  129. if (App.testMode) {
  130. return 'http://nagiosserver/nagios';
  131. } else {
  132. // We want live data here
  133. var svcs = App.Service.find();
  134. var nagiosSvc = svcs.findProperty("serviceName", "NAGIOS");
  135. if (nagiosSvc) {
  136. var svcComponents = nagiosSvc.get('hostComponents');
  137. if (svcComponents) {
  138. var nagiosSvcComponent = svcComponents.findProperty("componentName", "NAGIOS_SERVER");
  139. if (nagiosSvcComponent) {
  140. var hostName = nagiosSvcComponent.get('host.hostName');
  141. if (hostName) {
  142. var host = App.Host.find(hostName);
  143. if (host) {
  144. hostName = host.get('publicHostName');
  145. }
  146. return "http://" + (App.singleNodeInstall ? App.singleNodeAlias : hostName) + "/nagios";
  147. }
  148. }
  149. }
  150. }
  151. return null;
  152. }
  153. }.property('App.router.updateController.isUpdated', 'dataLoadList.services', 'dataLoadList.hosts'),
  154. isNagiosInstalled:function () {
  155. return !!App.Service.find().findProperty('serviceName', 'NAGIOS');
  156. }.property('App.router.updateController.isUpdated', 'dataLoadList.services'),
  157. /**
  158. * Sorted list of alerts.
  159. * Changes whenever alerts are loaded.
  160. */
  161. alerts:[],
  162. updateAlerts: function(){
  163. var alerts = App.Alert.find();
  164. var alertsArray = alerts.toArray();
  165. var sortedArray = alertsArray.sort(function (left, right) {
  166. var statusDiff = right.get('status') - left.get('status');
  167. if (statusDiff == 0) { // same error severity - sort by time
  168. var rightTime = right.get('date');
  169. var leftTime = left.get('date');
  170. rightTime = rightTime ? rightTime.getTime() : 0;
  171. leftTime = leftTime ? leftTime.getTime() : 0;
  172. statusDiff = rightTime - leftTime;
  173. }
  174. return statusDiff;
  175. });
  176. this.set('alerts', sortedArray);
  177. },
  178. /**
  179. * Load alerts from server
  180. * @param callback Slave function, should be called to fire delayed update.
  181. * Look at <code>App.updater.run</code> for more information.
  182. * Also used to set <code>dataLoadList.alerts</code> status during app loading
  183. */
  184. loadAlerts:function (callback) {
  185. if (this.get('isNagiosInstalled')) {
  186. var dataUrl = this.getUrl('/data/alerts/alerts.json', '/host_components?fields=HostRoles/nagios_alerts&HostRoles/component_name=NAGIOS_SERVER');
  187. var self = this;
  188. var ajaxOptions = {
  189. dataType:"json",
  190. complete:function () {
  191. self.updateAlerts();
  192. callback();
  193. },
  194. error: function(jqXHR, testStatus, error) {
  195. console.log('Nagios $.ajax() response:', error);
  196. }
  197. };
  198. App.HttpClient.get(dataUrl, App.alertsMapper, ajaxOptions);
  199. } else {
  200. console.log("No Nagios URL provided.");
  201. callback();
  202. }
  203. },
  204. /**
  205. * Determination of Nagios presence is known only after App.Service is
  206. * loaded from server. When that is done, no one tells alerts to load,
  207. * due to which alerts are not loaded & shown till the next polling cycle.
  208. * This method immediately loads alerts once Nagios presence is known.
  209. */
  210. isNagiosInstalledListener: function () {
  211. var self = this;
  212. self.loadAlerts(function () {
  213. self.updateLoadStatus('alerts');
  214. });
  215. }.observes('isNagiosInstalled'),
  216. /**
  217. * Send request to server to load components updated statuses
  218. * @param callback Slave function, should be called to fire delayed update.
  219. * Look at <code>App.updater.run</code> for more information
  220. * @return {Boolean} Whether we have errors
  221. */
  222. loadUpdatedStatus: function(callback){
  223. if(!this.get('clusterName')){
  224. callback();
  225. return false;
  226. }
  227. var servicesUrl = this.getUrl('/data/dashboard/services.json', '/services?fields=ServiceInfo,components/host_components/HostRoles/desired_state,components/host_components/HostRoles/state');
  228. App.HttpClient.get(servicesUrl, App.statusMapper, {
  229. complete: callback
  230. });
  231. return true;
  232. },
  233. /**
  234. * Run <code>loadUpdatedStatus</code> with delay
  235. * @param delay
  236. */
  237. loadUpdatedStatusDelayed: function(delay){
  238. setTimeout(function(){
  239. App.updater.immediateRun('loadUpdatedStatus');
  240. }, delay);
  241. },
  242. /**
  243. * Start polling, when <code>isWorking</code> become true
  244. */
  245. startPolling: function(){
  246. if(!this.get('isWorking')){
  247. return false;
  248. }
  249. App.updater.run(this, 'loadUpdatedStatus', 'isWorking', App.componentsUpdateInterval); //update will not run it immediately
  250. App.updater.run(this, 'loadAlerts', 'isWorking'); //update will not run it immediately
  251. return true;
  252. }.observes('isWorking'),
  253. /**
  254. *
  255. * load all data and update load status
  256. */
  257. loadClusterData:function () {
  258. var self = this;
  259. if (!this.get('clusterName')) {
  260. return;
  261. }
  262. if(this.get('isLoaded')) { // do not load data repeatedly
  263. return;
  264. }
  265. var clusterUrl = this.getUrl('/data/clusters/cluster.json', '?fields=Clusters');
  266. 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');
  267. var usersUrl = App.testMode ? '/data/users/users.json' : App.apiPrefix + '/users/?fields=*';
  268. var racksUrl = "/data/racks/racks.json";
  269. var dataSetUrl = "/data/mirroring/all_datasets.json";
  270. var targetClusterUrl = "/data/mirroring/target_clusters.json";
  271. App.HttpClient.get(targetClusterUrl, App.targetClusterMapper, {
  272. complete: function (jqXHR, textStatus) {
  273. self.updateLoadStatus('targetclusters');
  274. }
  275. }, function (jqXHR, textStatus) {
  276. self.updateLoadStatus('targetclusters');
  277. });
  278. App.HttpClient.get(dataSetUrl, App.dataSetMapper, {
  279. complete: function (jqXHR, textStatus) {
  280. self.updateLoadStatus('datasets');
  281. }
  282. }, function (jqXHR, textStatus) {
  283. self.updateLoadStatus('datasets');
  284. });
  285. App.HttpClient.get(racksUrl, App.racksMapper, {
  286. complete:function (jqXHR, textStatus) {
  287. self.updateLoadStatus('racks');
  288. }
  289. }, function (jqXHR, textStatus) {
  290. self.updateLoadStatus('racks');
  291. });
  292. App.HttpClient.get(clusterUrl, App.clusterMapper, {
  293. complete:function (jqXHR, textStatus) {
  294. self.updateLoadStatus('cluster');
  295. }
  296. }, function (jqXHR, textStatus) {
  297. self.updateLoadStatus('cluster');
  298. });
  299. if (App.testMode) {
  300. self.updateLoadStatus('clusterStatus');
  301. } else {
  302. App.clusterStatus.updateFromServer(true).complete(function() {
  303. self.updateLoadStatus('clusterStatus');
  304. });
  305. }
  306. App.HttpClient.get(hostsUrl, App.hostsMapper, {
  307. complete:function (jqXHR, textStatus) {
  308. self.updateLoadStatus('hosts');
  309. }
  310. }, function (jqXHR, textStatus) {
  311. self.updateLoadStatus('hosts');
  312. });
  313. App.HttpClient.get(usersUrl, App.usersMapper, {
  314. complete:function (jqXHR, textStatus) {
  315. self.updateLoadStatus('users');
  316. }
  317. }, function (jqXHR, textStatus) {
  318. self.updateLoadStatus('users');
  319. });
  320. App.router.get('updateController').updateServiceMetric(function(){
  321. self.updateLoadStatus('services');
  322. }, true);
  323. this.loadAlerts(function(){
  324. self.updateLoadStatus('alerts');
  325. });
  326. },
  327. clusterName:function () {
  328. return (this.get('cluster')) ? this.get('cluster').Clusters.cluster_name : null;
  329. }.property('cluster'),
  330. updateClusterData: function () {
  331. var clusterUrl = this.getUrl('/data/clusters/cluster.json', '?fields=Clusters');
  332. App.HttpClient.get(clusterUrl, App.clusterMapper, {
  333. complete:function(){}
  334. });
  335. }
  336. });