alerts_controller.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. App.MainAlertsController = Em.Controller.extend({
  20. name: 'mainAlertsController',
  21. alerts: [],
  22. isLoaded: false,
  23. isUpdating: false,
  24. // name of resource(hostName or ServiceName)
  25. resourceName: null,
  26. //"HOST" or "SERVICE"
  27. resourceType: null,
  28. updateTimer: null,
  29. /**
  30. * load alerts for service or host
  31. * @param name
  32. * @param type
  33. */
  34. loadAlerts: function (name, type) {
  35. this.set('isLoaded', false);
  36. this.set('resourceName', name);
  37. this.set('resourceType', type);
  38. this.getFromServer();
  39. },
  40. /**
  41. * update alerts
  42. */
  43. update: function () {
  44. var self = this;
  45. if (this.get('isUpdating')) {
  46. this.set('updateTimer', setTimeout(function () {
  47. self.getFromServer();
  48. self.update();
  49. }, App.componentsUpdateInterval));
  50. } else {
  51. clearTimeout(this.get('updateTimer'));
  52. }
  53. }.observes('isUpdating'),
  54. /**
  55. * ask alerts from server by type
  56. */
  57. getFromServer: function () {
  58. if (App.router.get('clusterController.isNagiosInstalled')) {
  59. if (this.get('resourceType') === "SERVICE") {
  60. this.getAlertsByService();
  61. } else if (this.get('resourceType') === "HOST") {
  62. this.getAlertsByHost();
  63. } else {
  64. console.warn("GET Alerts error: unknown resourceType");
  65. }
  66. } else {
  67. this.set('isLoaded', true);
  68. }
  69. },
  70. getAlertsByHost: function () {
  71. if (this.get('resourceName')) {
  72. App.ajax.send({
  73. name: 'alerts.get_by_host',
  74. sender: this,
  75. data: {
  76. hostName: this.get('resourceName')
  77. },
  78. success: 'getAlertsSuccessCallback',
  79. error: 'getAlertsErrorCallback'
  80. })
  81. } else {
  82. console.warn('GET Alerts error: hostName parameter is missing');
  83. }
  84. },
  85. getAlertsByService: function () {
  86. if (this.get('resourceName')) {
  87. App.ajax.send({
  88. name: 'alerts.get_by_service',
  89. sender: this,
  90. data: {
  91. serviceName: this.get('resourceName')
  92. },
  93. success: 'getAlertsSuccessCallback',
  94. error: 'getAlertsErrorCallback'
  95. })
  96. } else {
  97. console.warn('GET Alerts error: serviceName parameter is missing');
  98. }
  99. },
  100. /**
  101. * map to associate old status format with and maintain sorting
  102. */
  103. statusNumberMap: {
  104. "OK" : "0",
  105. "WARNING": "1",
  106. "CRITICAL": "2",
  107. "PASSIVE": "3"
  108. },
  109. getAlertsSuccessCallback: function (json) {
  110. var alerts = [];
  111. if (json && json.alerts && json.alerts.detail) {
  112. json.alerts.detail.forEach(function (_alert) {
  113. alerts.pushObject(App.Alert.create({
  114. title: _alert.description,
  115. serviceType: _alert.service_name,
  116. lastTime: _alert.status_time,
  117. status: this.get('statusNumberMap')[_alert.status] || "4",
  118. message: _alert.output,
  119. hostName: _alert.host_name,
  120. lastCheck: _alert.last_status_time
  121. }));
  122. }, this);
  123. }
  124. this.set('alerts', this.sortAlerts(alerts));
  125. this.set('isLoaded', true);
  126. },
  127. /**
  128. * alerts sorting
  129. * firstly by status
  130. * 1. UNKNOWN
  131. * 2. CRITICAL
  132. * 3. WARNING
  133. * 4. OK
  134. * secondly by date
  135. * @param array
  136. * @return {*}
  137. */
  138. sortAlerts: function (array) {
  139. return array.sort(function (left, right) {
  140. var statusDiff = right.get('status') - left.get('status');
  141. if (statusDiff == 0) { // same error severity - sort by time
  142. var rightTime = right.get('date');
  143. var leftTime = left.get('date');
  144. rightTime = rightTime ? rightTime.getTime() : 0;
  145. leftTime = leftTime ? leftTime.getTime() : 0;
  146. statusDiff = rightTime - leftTime;
  147. }
  148. return statusDiff;
  149. });
  150. },
  151. getAlertsErrorCallback: function(){
  152. this.set('isLoaded', true);
  153. }
  154. });