cluster_controller.js 11 KB

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