host.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. var misc = require('utils/misc');
  20. App.Host = DS.Model.extend({
  21. hostName: DS.attr('string'),
  22. publicHostName: DS.attr('string'),
  23. cluster: DS.belongsTo('App.Cluster'),
  24. hostComponents: DS.hasMany('App.HostComponent'),
  25. cpu: DS.attr('string'),
  26. cpuPhysical: DS.attr('string'),
  27. memory: DS.attr('string'),
  28. diskTotal: DS.attr('number'),
  29. diskFree: DS.attr('number'),
  30. osArch: DS.attr('string'),
  31. ip: DS.attr('string'),
  32. rack: DS.attr('string'),
  33. healthStatus: DS.attr('string'),
  34. lastHeartBeatTime: DS.attr('number'),
  35. osType: DS.attr("string"),
  36. diskInfo: DS.attr('object'),
  37. loadOne:DS.attr('number'),
  38. loadFive:DS.attr('number'),
  39. loadFifteen:DS.attr('number'),
  40. memTotal:DS.attr('number'),
  41. memFree:DS.attr('number'),
  42. cpuSystem:DS.attr('number'),
  43. cpuUser:DS.attr('number'),
  44. criticalWarningAlertsCount: DS.attr('number'),
  45. alertsSummary: DS.attr('object'),
  46. passiveState: DS.attr('string'),
  47. index: DS.attr('number'),
  48. stackVersions: DS.hasMany('App.HostStackVersion'),
  49. /**
  50. * Is host checked at the main Hosts page
  51. */
  52. selected:DS.attr('boolean'),
  53. /**
  54. * determine whether host is requested from server
  55. */
  56. isRequested: DS.attr('boolean'),
  57. currentVersion: function () {
  58. return this.get('stackVersions').findProperty('isCurrent').get('repoVersion');
  59. }.property('stackVersions.@each.isCurrent'),
  60. /**
  61. * Overall CPU usage (system and user)
  62. * @returns {Number}
  63. */
  64. cpuUsage: function () {
  65. if (this.get('cpuSystem') && this.get('cpuUser')) {
  66. return this.get('cpuSystem') + this.get('cpuUser');
  67. }
  68. return 0;
  69. }.property('cpuSystem', 'cpuUser'),
  70. /**
  71. * Percent value of used memory
  72. * @returns {Number}
  73. */
  74. memoryUsage: function () {
  75. if (this.get('memFree') && this.get('memTotal')) {
  76. var memUsed = this.get('memTotal') - this.get('memFree');
  77. return (100 * memUsed) / this.get('memTotal');
  78. }
  79. return 0;
  80. }.property('memTotal', 'memFree'),
  81. componentsWithStaleConfigs: function () {
  82. return this.get('hostComponents').filterProperty('staleConfigs', true);
  83. }.property('hostComponents.@each.staleConfigs'),
  84. /**
  85. * Get count of host components with stale configs
  86. * @returns {Number}
  87. */
  88. componentsWithStaleConfigsCount: function() {
  89. return this.get('componentsWithStaleConfigs').length;
  90. }.property('componentsWithStaleConfigs.length'),
  91. /**
  92. * Get count of host components in passive state
  93. * @returns {Number}
  94. */
  95. componentsInPassiveStateCount: function() {
  96. return this.get('hostComponents').filter(function(component) {
  97. return component.get('passiveState') !== 'OFF';
  98. }).length;
  99. }.property('hostComponents.@each.passiveState'),
  100. /**
  101. * Count of mounted on host disks
  102. * @returns {Number}
  103. */
  104. disksMounted: function() {
  105. return this.get('diskInfo.length');
  106. }.property('diskInfo.length'),
  107. coresFormatted: function() {
  108. return this.get('cpu') + ' (' + this.get('cpuPhysical') + ')';
  109. }.property('cpu', 'cpuPhysical'),
  110. /**
  111. * API return diskTotal and diskFree. Need to save their different
  112. * @returns {Number}
  113. */
  114. diskUsed: function(){
  115. return this.get('diskTotal') - this.get('diskFree');
  116. }.property('diskFree', 'diskTotal'),
  117. /**
  118. * Format diskUsed value to float with 2 digits (also convert to GB)
  119. * @returns {String} Format: '*** GB'
  120. */
  121. diskUsedFormatted: function() {
  122. return Math.round(this.get('diskUsed') * Math.pow(10, 2)) / Math.pow(10, 2) + 'GB';
  123. }.property('diskUsed'),
  124. /**
  125. * Format diskTotal value to float with 2 digits (also convert to GB)
  126. * @returns {String} Format: '*** GB'
  127. */
  128. diskTotalFormatted: function() {
  129. return Math.round(this.get('diskTotal') * Math.pow(10, 2)) / Math.pow(10, 2) + 'GB';
  130. }.property('diskTotal'),
  131. /**
  132. * Percent value of used disk space
  133. * @returns {Number}
  134. */
  135. diskUsage: function() {
  136. return (this.get('diskUsed')) / this.get('diskTotal') * 100;
  137. }.property('diskUsed', 'diskTotal'),
  138. /**
  139. * Format diskUsage to float with 2 digits
  140. * @returns {String} Format: '**.** %'
  141. */
  142. diskUsageFormatted: function() {
  143. if (isNaN(this.get('diskUsage')) || this.get('diskUsage') < 0) {
  144. return Em.I18n.t('hosts.host.metrics.dataUnavailable');
  145. }
  146. var s = Math.round(this.get('diskUsage') * Math.pow(10, 2)) / Math.pow(10, 2);
  147. if (isNaN(s)) {
  148. s = 0;
  149. }
  150. return s + '%';
  151. }.property('diskUsage'),
  152. /**
  153. * Formatted string with data about disk usage
  154. * @returns {String}
  155. */
  156. diskInfoBar: function() {
  157. if (isNaN(this.get('diskUsage')) || this.get('diskUsage') < 0) {
  158. return this.get('diskUsageFormatted');
  159. }
  160. return this.get('diskUsedFormatted') + '/' + this.get('diskTotalFormatted') + ' (' + this.get('diskUsageFormatted')
  161. + ' ' + Em.I18n.t('services.service.summary.diskInfoBar.used') + ')';
  162. }.property('diskUsedFormatted', 'diskTotalFormatted'),
  163. /**
  164. * Formatted bytes to appropriate value
  165. * @returns {String}
  166. */
  167. memoryFormatted: function () {
  168. return misc.formatBandwidth(this.get('memory') * 1024);
  169. }.property('memory'),
  170. /**
  171. * Return true if the host <code>healthStatus</code> is UNKNOWN
  172. * @returns {bool}
  173. */
  174. isNotHeartBeating : function() {
  175. return (App.get('testMode')) ? false : (this.get('healthStatus') === "UNKNOWN");
  176. }.property('lastHeartBeatTime'),
  177. /**
  178. * Average load
  179. * @returns {Number}
  180. */
  181. loadAvg: function() {
  182. if (this.get('loadOne') != null) return this.get('loadOne').toFixed(2);
  183. if (this.get('loadFive') != null) return this.get('loadFive').toFixed(2);
  184. if (this.get('loadFifteen') != null) return this.get('loadFifteen').toFixed(2);
  185. return null;
  186. }.property('loadOne', 'loadFive', 'loadFifteen'),
  187. /**
  188. * Host health indicator
  189. * Based on <code>healthStatus</code>
  190. * @returns {String}
  191. */
  192. healthClass: function(){
  193. if (this.get('passiveState')!= 'OFF') {
  194. return 'icon-medkit';
  195. }
  196. var statusMap = {
  197. 'UNKNOWN': 'health-status-DEAD-YELLOW',
  198. 'HEALTHY': 'health-status-LIVE',
  199. 'UNHEALTHY': 'health-status-DEAD-RED',
  200. 'ALERT': 'health-status-DEAD-ORANGE'
  201. };
  202. return statusMap[this.get('healthStatus')] || 'health-status-DEAD-YELLOW';
  203. }.property('healthStatus'),
  204. healthIconClass: function () {
  205. switch (this.get('healthClass')) {
  206. case 'health-status-LIVE':
  207. return App.healthIconClassGreen;
  208. break;
  209. case 'health-status-DEAD-RED':
  210. return App.healthIconClassRed;
  211. break;
  212. case 'health-status-DEAD-YELLOW':
  213. return App.healthIconClassYellow;
  214. break;
  215. case 'health-status-DEAD-ORANGE':
  216. return App.healthIconClassOrange;
  217. break;
  218. default:
  219. return "";
  220. break;
  221. }
  222. }.property('healthClass'),
  223. /**
  224. * Tooltip for host indicator
  225. * Contains affected host components names (based on <code>healthClass</code>)
  226. * @returns {String}
  227. */
  228. healthToolTip: function(){
  229. var hostComponents = this.get('hostComponents').filter(function(item){
  230. return item.get('workStatus') !== App.HostComponentStatus.started;
  231. });
  232. var output = '';
  233. if (this.get('passiveState') != 'OFF') {
  234. return Em.I18n.t('hosts.host.passive.mode');
  235. }
  236. switch (this.get('healthClass')){
  237. case 'health-status-DEAD-RED':
  238. hostComponents = hostComponents.filterProperty('isMaster', true);
  239. output = Em.I18n.t('hosts.host.healthStatus.mastersDown');
  240. hostComponents.forEach(function(hc, index){
  241. output += (index == (hostComponents.length-1)) ? hc.get('displayName') : (hc.get('displayName')+", ");
  242. }, this);
  243. break;
  244. case 'health-status-DEAD-YELLOW':
  245. output = Em.I18n.t('hosts.host.healthStatus.heartBeatNotReceived');
  246. break;
  247. case 'health-status-DEAD-ORANGE':
  248. hostComponents = hostComponents.filterProperty('isSlave', true);
  249. output = Em.I18n.t('hosts.host.healthStatus.slavesDown');
  250. hostComponents.forEach(function(hc, index){
  251. output += (index == (hostComponents.length-1)) ? hc.get('displayName') : (hc.get('displayName')+", ");
  252. }, this);
  253. break;
  254. case 'health-status-LIVE':
  255. output = Em.I18n.t('hosts.host.healthStatus.allUp');
  256. break;
  257. }
  258. return output;
  259. }.property('hostComponents.@each.workStatus','hostComponents.@each.passiveState')
  260. });
  261. App.Host.FIXTURES = [];