host.js 8.2 KB

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