host.js 8.8 KB

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