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. /**
  71. * request alerts from server, which belong to particular host
  72. * @return {Boolean}
  73. */
  74. getAlertsByHost: function () {
  75. if (this.get('resourceName')) {
  76. App.ajax.send({
  77. name: 'alerts.get_by_host',
  78. sender: this,
  79. data: {
  80. hostName: this.get('resourceName')
  81. },
  82. success: 'getAlertsSuccessCallback',
  83. error: 'getAlertsErrorCallback'
  84. });
  85. return true;
  86. } else {
  87. console.warn('GET Alerts error: hostName parameter is missing');
  88. return false;
  89. }
  90. },
  91. /**
  92. * Saved request for alerts
  93. * Should be aborted if user navigate away from current page (aborting done in router)
  94. * @type {$.ajax|null}
  95. */
  96. servicesRequest: null,
  97. /**
  98. * request alerts from server, which belong to particular service
  99. * @return {Boolean}
  100. */
  101. getAlertsByService: function () {
  102. if (this.get('resourceName')) {
  103. var request = App.ajax.send({
  104. name: 'alerts.get_by_service',
  105. sender: this,
  106. data: {
  107. serviceName: this.get('resourceName')
  108. },
  109. success: 'getAlertsSuccessCallback',
  110. error: 'getAlertsErrorCallback'
  111. });
  112. this.set('servicesRequest', request);
  113. return true;
  114. } else {
  115. console.warn('GET Alerts error: serviceName parameter is missing');
  116. return false;
  117. }
  118. },
  119. /**
  120. * map to associate old status format with and maintain sorting
  121. */
  122. statusNumberMap: {
  123. "OK": "0",
  124. "WARNING": "1",
  125. "CRITICAL": "2",
  126. "PASSIVE": "3"
  127. },
  128. /**
  129. * format json data and push into @alerts array
  130. * @param json
  131. */
  132. getAlertsSuccessCallback: function (json) {
  133. var alerts = [];
  134. if (json && json.legacy_alerts && json.legacy_alerts.detail) {
  135. json.legacy_alerts.detail.forEach(function (_alert) {
  136. alerts.pushObject(App.Alert.create({
  137. title: _alert.description,
  138. serviceType: _alert.service_name,
  139. lastTime: _alert.status_time,
  140. status: this.get('statusNumberMap')[_alert.status] || "4",
  141. message: _alert.output,
  142. hostName: _alert.host_name,
  143. lastCheck: _alert.last_status_time
  144. }));
  145. }, this);
  146. }
  147. this.set('alerts', alerts.sortProperty('status', 'date').reverse());
  148. this.set('isLoaded', true);
  149. },
  150. /**
  151. * finish loading if call failed
  152. */
  153. getAlertsErrorCallback: function () {
  154. this.set('isLoaded', true);
  155. }
  156. });