cluster_controller.js 9.9 KB

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