cluster_controller.js 12 KB

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