cluster_controller.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. updateLoadStatus: function(item){
  24. var loadList = this.get('dataLoadList');
  25. var loaded = true;
  26. loadList.set(item, true);
  27. for(var i in loadList){
  28. if(loadList.hasOwnProperty(i) && !loadList[i] && loaded){
  29. loaded = false;
  30. }
  31. }
  32. this.set('isLoaded', loaded);
  33. },
  34. dataLoadList: Em.Object.create({
  35. 'hosts': false,
  36. 'runs': false,
  37. 'services': false,
  38. 'cluster' : false,
  39. 'racks' : false,
  40. 'alerts' : false,
  41. 'users' : false
  42. }),
  43. /**
  44. * load cluster name
  45. */
  46. loadClusterName: function(reload){
  47. if(this.get('clusterName') && !reload){
  48. return;
  49. }
  50. var self = this;
  51. var url = (App.testMode) ? '/data/clusters/info.json' : '/api/clusters';
  52. $.ajax({
  53. async: false,
  54. type: "GET",
  55. url: url,
  56. dataType: 'json',
  57. timeout: 5000,
  58. success: function (data) {
  59. self.set('cluster', data.items[0]);
  60. },
  61. error: function (request, ajaxOptions, error) {
  62. console.log('failed on loading cluster name');
  63. self.set('isLoaded', true);
  64. },
  65. statusCode: require('data/statusCodes')
  66. });
  67. },
  68. getUrl: function(testUrl, url){
  69. return (App.testMode) ? testUrl: '/api/clusters/' + this.get('clusterName') + url;
  70. },
  71. /**
  72. * Provides the URL to use for NAGIOS server. This URL
  73. * is helpful in getting alerts data from server and also
  74. * in populating links in UI.
  75. *
  76. * If null is returned, it means NAGIOS service is not installed.
  77. */
  78. nagiosUrl: function () {
  79. if (App.testMode) {
  80. return 'http://nagiosserver/nagios';
  81. } else {
  82. // We want live data here
  83. var svcs = App.Service.find();
  84. var nagiosSvc = svcs.findProperty("serviceName", "NAGIOS");
  85. if (nagiosSvc) {
  86. var svcComponents = nagiosSvc.get('components');
  87. if (svcComponents) {
  88. var nagiosSvcComponent = svcComponents.findProperty("componentName", "NAGIOS_SERVER");
  89. if(nagiosSvcComponent){
  90. var hostName = nagiosSvcComponent.get('host.hostName');
  91. if(hostName){
  92. return "http://"+hostName+"/nagios";
  93. }
  94. }
  95. }
  96. }
  97. return null;
  98. }
  99. }.property('dataLoadList.services'),
  100. isNagiosInstalled: function(){
  101. if(App.testMode){
  102. return true;
  103. }else{
  104. var svcs = App.Service.find();
  105. var nagiosSvc = svcs.findProperty("serviceName", "NAGIOS");
  106. return nagiosSvc!=null;
  107. }
  108. }.property('dataLoadList.services'),
  109. /**
  110. * Sorted list of alerts.
  111. * Changes whenever alerts are loaded.
  112. */
  113. alerts: function () {
  114. var alerts = App.Alert.find();
  115. var alertsArray = alerts.toArray();
  116. var sortedArray = alertsArray.sort(function (left, right) {
  117. var statusDiff = right.get('status') - left.get('status');
  118. if (statusDiff == 0) { // same error severity - sort by time
  119. var rightTime = right.get('date');
  120. var leftTime = left.get('time');
  121. rightTime = rightTime ? rightTime.getTime() : 0;
  122. leftTime = leftTime ? leftTime.getTime() : 0;
  123. statusDiff = rightTime - leftTime;
  124. }
  125. return statusDiff;
  126. });
  127. return sortedArray;
  128. }.property('dataLoadList.alerts'),
  129. /**
  130. * This method automatically loads alerts when Nagios URL
  131. * changes. Once done it will trigger dataLoadList.alerts
  132. * property, which will trigger the alerts property.
  133. */
  134. loadAlerts: function () {
  135. var nagiosUrl = this.get('nagiosUrl');
  136. if (nagiosUrl) {
  137. var lastSlash = nagiosUrl.lastIndexOf('/');
  138. if (lastSlash > -1) {
  139. nagiosUrl = nagiosUrl.substring(0, lastSlash);
  140. }
  141. var dataUrl;
  142. var ajaxOptions = {
  143. dataType: "jsonp",
  144. jsonp: "jsonp",
  145. context: this,
  146. complete: function (jqXHR, textStatus) {
  147. this.updateLoadStatus('alerts')
  148. }
  149. };
  150. if (App.testMode) {
  151. dataUrl = "/data/alerts/alerts.jsonp";
  152. ajaxOptions.jsonpCallback = "jQuery172040994187095202506_1352498338217";
  153. } else {
  154. dataUrl = nagiosUrl + "/hdp/nagios/nagios_alerts.php?q1=alerts&alert_type=all";
  155. }
  156. App.HttpClient.get(dataUrl, App.alertsMapper, ajaxOptions);
  157. } else {
  158. this.updateLoadStatus('alerts');
  159. console.log("No Nagios URL provided.")
  160. }
  161. }.observes('nagiosUrl'),
  162. /**
  163. *
  164. * load all data and update load status
  165. */
  166. loadClusterData: function(){
  167. var self = this;
  168. if(!this.get('clusterName')){
  169. return;
  170. }
  171. var clusterUrl = this.getUrl('/data/clusters/cluster.json', '?fields=Clusters');
  172. var hostsUrl = this.getUrl('/data/hosts/hosts.json', '/hosts?fields=*');
  173. var servicesUrl1 = this.getUrl('/data/dashboard/services.json', '/services?ServiceInfo/service_name!=MISCELLANEOUS&ServiceInfo/service_name!=DASHBOARD&fields=components/host_components/*');
  174. var servicesUrl2 = this.getUrl('/data/dashboard/serviceComponents.json', '/services?ServiceInfo/service_name!=MISCELLANEOUS&ServiceInfo/service_name!=DASHBOARD&fields=components/ServiceComponentInfo');
  175. var usersUrl = App.testMode ? '/data/users/users.json' : '/api/users/?fields=*';
  176. var runsUrl = App.testMode ? "/data/apps/runs.json" : "/api/jobhistory/workflow";
  177. var racksUrl = "/data/racks/racks.json";
  178. App.HttpClient.get(racksUrl, App.racksMapper,{
  179. complete:function(jqXHR, textStatus){
  180. self.updateLoadStatus('racks');
  181. }
  182. },function(jqXHR, textStatus){
  183. self.updateLoadStatus('racks');
  184. });
  185. App.HttpClient.get(clusterUrl, App.clusterMapper,{
  186. complete:function(jqXHR, textStatus){
  187. self.updateLoadStatus('cluster');
  188. }
  189. },function(jqXHR, textStatus){
  190. self.updateLoadStatus('cluster');
  191. });
  192. App.HttpClient.get(runsUrl, App.runsMapper,{
  193. complete:function(jqXHR, textStatus) {
  194. self.updateLoadStatus('runs');
  195. }
  196. },function(jqXHR, textStatus){
  197. self.updateLoadStatus('runs');
  198. });
  199. App.HttpClient.get(hostsUrl, App.hostsMapper,{
  200. complete:function(jqXHR, textStatus){
  201. self.updateLoadStatus('hosts');
  202. }
  203. },function(jqXHR, textStatus){
  204. self.updateLoadStatus('hosts');
  205. });
  206. App.HttpClient.get(usersUrl, App.usersMapper,{
  207. complete:function(jqXHR, textStatus){
  208. self.updateLoadStatus('users');
  209. }
  210. },function(jqXHR, textStatus){
  211. self.updateLoadStatus('users');
  212. });
  213. //////////////////////////////
  214. // Hack for services START //
  215. //////////////////////////////
  216. var metricsJson = null;
  217. var serviceComponentJson = null;
  218. var metricsMapper = {
  219. map: function(data){
  220. metricsJson = data;
  221. }
  222. };
  223. var serviceComponentMapper = {
  224. map: function (data) {
  225. serviceComponentJson = data;
  226. if (metricsJson != null && serviceComponentJson != null) {
  227. var hdfsSvc1 = null;
  228. var hdfsSvc2 = null;
  229. var mrSvc1 = null;
  230. var mrSvc2 = null;
  231. var hbaseSvc1 = null;
  232. var hbaseSvc2 = null;
  233. metricsJson.items.forEach(function (svc) {
  234. if (svc.ServiceInfo.service_name == "HDFS") {
  235. hdfsSvc1 = svc;
  236. }
  237. if (svc.ServiceInfo.service_name == "MAPREDUCE") {
  238. mrSvc1 = svc;
  239. }
  240. if (svc.ServiceInfo.service_name == "HBASE") {
  241. hbaseSvc1 = svc;
  242. }
  243. });
  244. serviceComponentJson.items.forEach(function (svc) {
  245. if (svc.ServiceInfo.service_name == "HDFS") {
  246. hdfsSvc2 = svc;
  247. }
  248. if (svc.ServiceInfo.service_name == "MAPREDUCE") {
  249. mrSvc2 = svc;
  250. }
  251. if (svc.ServiceInfo.service_name == "HBASE") {
  252. hbaseSvc2 = svc;
  253. }
  254. });
  255. var nnC1 = null;
  256. var nnC2 = null;
  257. var jtC1 = null;
  258. var jtC2 = null;
  259. var hbm1 = null;
  260. var hbm2 = null;
  261. if (hdfsSvc1) {
  262. hdfsSvc1.components.forEach(function (c) {
  263. if (c.ServiceComponentInfo.component_name == "NAMENODE") {
  264. nnC1 = c;
  265. }
  266. });
  267. }
  268. if (hdfsSvc2) {
  269. hdfsSvc2.components.forEach(function (c) {
  270. if (c.ServiceComponentInfo.component_name == "NAMENODE") {
  271. nnC2 = c;
  272. }
  273. });
  274. }
  275. if (mrSvc1) {
  276. mrSvc1.components.forEach(function (c) {
  277. if (c.ServiceComponentInfo.component_name == "JOBTRACKER") {
  278. jtC1 = c;
  279. }
  280. });
  281. }
  282. if (mrSvc2) {
  283. mrSvc2.components.forEach(function (c) {
  284. if (c.ServiceComponentInfo.component_name == "JOBTRACKER") {
  285. jtC2 = c;
  286. }
  287. });
  288. }
  289. if (hbaseSvc1) {
  290. hbaseSvc1.components.forEach(function (c) {
  291. if (c.ServiceComponentInfo.component_name == "HBASE_MASTER") {
  292. hbm1 = c;
  293. }
  294. });
  295. }
  296. if (hbaseSvc2) {
  297. hbaseSvc2.components.forEach(function (c) {
  298. if (c.ServiceComponentInfo.component_name == "HBASE_MASTER") {
  299. hbm2 = c;
  300. }
  301. });
  302. }
  303. if (nnC1 && nnC2) {
  304. nnC1.ServiceComponentInfo = nnC2.ServiceComponentInfo;
  305. }
  306. if (jtC1 && jtC2) {
  307. jtC1.ServiceComponentInfo = jtC2.ServiceComponentInfo;
  308. }
  309. if (hbm1 && hbm2) {
  310. hbm1.ServiceComponentInfo = hbm2.ServiceComponentInfo;
  311. }
  312. App.servicesMapper.map(metricsJson);
  313. self.updateLoadStatus('services');
  314. }
  315. }
  316. }
  317. App.HttpClient.get(servicesUrl1, metricsMapper,{
  318. complete:function(jqXHR, textStatus){
  319. App.HttpClient.get(servicesUrl2, serviceComponentMapper,{
  320. complete:function(jqXHR, textStatus){
  321. }
  322. });
  323. }
  324. });
  325. /////////////////////////////
  326. // Hack for services END //
  327. /////////////////////////////
  328. },
  329. clusterName: function(){
  330. return (this.get('cluster')) ? this.get('cluster').Clusters.cluster_name : null;
  331. }.property('cluster')
  332. })