cluster_controller.js 11 KB

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