cluster_controller.js 12 KB

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