alert_definition.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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 dateUtils = require('utils/date');
  20. App.AlertDefinition = DS.Model.extend({
  21. name: DS.attr('string'),
  22. label: DS.attr('string'),
  23. service: DS.belongsTo('App.Service'),
  24. serviceName: DS.attr('string'),
  25. componentName: DS.attr('string'),
  26. enabled: DS.attr('boolean'),
  27. scope: DS.attr('string'),
  28. interval: DS.attr('number'),
  29. type: DS.attr('string'),
  30. groups: DS.hasMany('App.AlertGroup'),
  31. reporting: DS.hasMany('App.AlertReportDefinition'),
  32. lastTriggered: DS.attr('number'),
  33. /**
  34. * Raw data from AlertDefinition/source
  35. * used to format request content for updating alert definition
  36. * @type {Object}
  37. */
  38. rawSourceData: {},
  39. /**
  40. * Counts of alert grouped by their status
  41. * Format:
  42. * <code>
  43. * {
  44. * "CRITICAL": 1,
  45. * "OK": 1,
  46. * "UNKNOWN": 0,
  47. * "WARN": 0
  48. * }
  49. * </code>
  50. * @type {object}
  51. */
  52. summary: {},
  53. /**
  54. * Formatted timestamp for latest alert triggering for current alertDefinition
  55. * @type {string}
  56. */
  57. lastTriggeredFormatted: function () {
  58. return dateUtils.dateFormat(this.get('lastTriggered'));
  59. }.property('lastTriggered'),
  60. /**
  61. * Formatted timestamp with <code>$.timeago</code>
  62. * @type {string}
  63. */
  64. lastTriggeredAgoFormatted: function () {
  65. var lastTriggered = this.get('lastTriggered');
  66. return lastTriggered ? $.timeago(new Date(lastTriggered)): '';
  67. }.property('lastTriggered'),
  68. /**
  69. * Formatted displayName for <code>componentName</code>
  70. * @type {String}
  71. */
  72. componentNameFormatted: function () {
  73. return App.format.role(this.get('componentName'));
  74. }.property('componentName'),
  75. /**
  76. * Status generates from child-alerts
  77. * Format: OK(1) WARN(2) CRIT(1) UNKN(1)
  78. * If single host: show: OK/WARNING/CRITICAL/UNKNOWN
  79. * If some there are no alerts with some state, this state isn't shown
  80. * If no OK/WARN/CRIT/UNKN state, then show PENDING
  81. * Order is equal to example
  82. * @type {string}
  83. */
  84. status: function () {
  85. var order = this.get('order'),
  86. summary = this.get('summary'),
  87. hostCnt = 0;
  88. order.forEach(function(state) {
  89. var cnt = summary[state] ? summary[state] : 0;
  90. hostCnt += cnt;
  91. });
  92. if (hostCnt > 1) {
  93. // multiple hosts
  94. return order.map(function (state) {
  95. var shortState = state.substring(0, 4);
  96. return summary[state] ? '<span class="label alert-state-' + state + '">' + shortState + ' ( ' + summary[state] + ' )</span>' : null;
  97. }).compact().join(' ');
  98. } else if (hostCnt == 1) {
  99. // single host, single status
  100. return order.map(function (state) {
  101. return summary[state] ? '<span class="alert-state-single-host label alert-state-'+ state + '">' + state + '</span>' : null;
  102. }).compact().join(' ');
  103. } else if (hostCnt == 0) {
  104. // penging
  105. var state = 'PENDING';
  106. return '<span class="alert-state-single-host label alert-state-'+ state + '">' + state + '</span>';
  107. }
  108. return null;
  109. }.property('summary'),
  110. isHostAlertDefinition: function () {
  111. var serviceID = (this.get('service')._id === "AMBARI"),
  112. component = (this.get('componentName') === "AMBARI_AGENT");
  113. return serviceID && component;
  114. }.property('service', 'componentName'),
  115. typeIconClass: function () {
  116. var typeIcons = this.get('typeIcons'),
  117. type = this.get('type');
  118. return typeIcons[type];
  119. }.property('type'),
  120. /**
  121. * if this definition is in state: CRIT / WARNING, if true, will show up in alerts fast access popup
  122. * @type {boolean}
  123. */
  124. isCriticalOrWarning: function () {
  125. return this.get('isCritical') || this.get('isWarning');
  126. }.property('isCritical', 'isWarning'),
  127. /**
  128. * if this definition is in state: CRIT
  129. * @type {boolean}
  130. */
  131. isCritical: function () {
  132. var summary = this.get('summary');
  133. var state = 'CRITICAL';
  134. return !!summary[state];
  135. }.property('summary'),
  136. /**
  137. * if this definition is in state: WARNING
  138. * @type {boolean}
  139. */
  140. isWarning: function () {
  141. var summary = this.get('summary');
  142. var state = 'WARNING';
  143. return !!summary[state];
  144. }.property('summary'),
  145. /**
  146. * For alerts we will have processes which are not typical
  147. * cluster services - like Ambari-Server. This method unifies
  148. * cluster services and other services into a common display-name.
  149. * @see App.AlertInstance#serviceDisplayName()
  150. */
  151. serviceDisplayName : function() {
  152. var serviceName = this.get('service.displayName');
  153. if (!serviceName) {
  154. serviceName = this.get('serviceName');
  155. if (serviceName) {
  156. serviceName = serviceName.toCapital();
  157. }
  158. }
  159. return serviceName;
  160. }.property('serviceName', 'service.displayName'),
  161. /**
  162. * List of css-classes for alert types
  163. * @type {object}
  164. */
  165. typeIcons: {
  166. 'METRIC': 'icon-bolt',
  167. 'SCRIPT': 'icon-file-text',
  168. 'WEB': 'icon-globe',
  169. 'PORT': 'icon-signin',
  170. 'AGGREGATE': 'icon-plus'
  171. },
  172. /**
  173. * Sort on load definitions by this severity order
  174. */
  175. severityOrder: ['CRITICAL', 'WARNING', 'OK', 'UNKNOWN', 'PENDING'],
  176. order: ['OK', 'WARNING', 'CRITICAL', 'UNKNOWN'],
  177. // todo: in future be mapped from server response
  178. description: 'Description for the Alert Definition.',
  179. // todo: in future be mapped from server response
  180. thresholds: '5-10'
  181. });
  182. App.AlertDefinition.reopenClass({
  183. getAllDefinitions: function () {
  184. return Array.prototype.concat.call(
  185. Array.prototype, App.PortAlertDefinition.find().toArray(),
  186. App.MetricsAlertDefinition.find().toArray(),
  187. App.WebAlertDefinition.find().toArray(),
  188. App.AggregateAlertDefinition.find().toArray(),
  189. App.ScriptAlertDefinition.find().toArray()
  190. )
  191. }
  192. });
  193. App.AlertReportDefinition = DS.Model.extend({
  194. type: DS.attr('string'),
  195. text: DS.attr('string'),
  196. value: DS.attr('number')
  197. });
  198. App.AlertMetricsSourceDefinition = DS.Model.extend({
  199. propertyList: [],
  200. value: DS.attr('string')
  201. });
  202. App.AlertMetricsUriDefinition = DS.Model.extend({
  203. http: DS.attr('string'),
  204. https: DS.attr('string'),
  205. httpsProperty: DS.attr('string'),
  206. httpsPropertyValue: DS.attr('string')
  207. });
  208. App.PortAlertDefinition = App.AlertDefinition.extend({
  209. defaultPort: DS.attr('number'),
  210. uri: DS.attr('string')
  211. });
  212. App.MetricsAlertDefinition = App.AlertDefinition.extend({
  213. jmx: DS.belongsTo('App.AlertMetricsSourceDefinition'),
  214. ganglia: DS.belongsTo('App.AlertMetricsSourceDefinition'),
  215. uri: DS.belongsTo('App.AlertMetricsUriDefinition')
  216. });
  217. App.WebAlertDefinition = App.AlertDefinition.extend({
  218. uri: DS.belongsTo('App.AlertMetricsUriDefinition')
  219. });
  220. App.AggregateAlertDefinition = App.AlertDefinition.extend({
  221. alertName: DS.attr('string')
  222. });
  223. App.ScriptAlertDefinition = App.AlertDefinition.extend({
  224. location: DS.attr('string')
  225. });
  226. App.AlertDefinition.FIXTURES = [];
  227. App.AlertReportDefinition.FIXTURES = [];
  228. App.AlertMetricsSourceDefinition.FIXTURES = [];
  229. App.PortAlertDefinition.FIXTURES = [];
  230. App.AlertMetricsUriDefinition.FIXTURES = [];
  231. App.MetricsAlertDefinition.FIXTURES = [];
  232. App.WebAlertDefinition.FIXTURES = [];
  233. App.AggregateAlertDefinition.FIXTURES = [];
  234. App.ScriptAlertDefinition.FIXTURES = [];