host.js 9.0 KB

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