alerts_mapper.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. App.alertsMapper = App.QuickDataMapper.create({
  19. model: App.Alert,
  20. config:{
  21. $alert_id:'' ,
  22. title: "service_description",
  23. service_type: "service_type",
  24. date: "last_hard_state_change",
  25. status: "current_state",
  26. message: "plugin_output",
  27. host_name: "host_name",
  28. current_attempt: "current_attempt",
  29. last_hard_state_change: "last_hard_state_change",
  30. last_hard_state: "last_hard_state",
  31. last_time_ok: "last_time_ok",
  32. last_time_warning: "last_time_warning",
  33. last_time_unknown: "last_time_unknown",
  34. last_time_critical: "last_time_critical",
  35. is_flapping: "is_flapping",
  36. last_check: "last_check"
  37. },
  38. map: function (json) {
  39. if (!this.get('model')) {
  40. return;
  41. }
  42. if (json && json.items && json.items.length>0 && json.items[0].HostRoles && json.items[0].HostRoles.nagios_alerts) {
  43. var alerts = json.items[0].HostRoles.nagios_alerts.alerts;
  44. if (App.Alert.find().content.length > 0) {
  45. this.update(alerts);
  46. } else {
  47. var result = [];
  48. alerts.forEach(function(item){
  49. var applyConfig = jQuery.extend({}, this.config);
  50. if (item.current_state && item.last_hard_state && item.current_state != item.last_hard_state) {
  51. switch (item.current_state) {
  52. case "0":
  53. applyConfig['date'] = 'last_time_ok';
  54. break;
  55. case "1":
  56. applyConfig['date'] = 'last_time_warning';
  57. break;
  58. case "2":
  59. applyConfig['date'] = 'last_time_critical';
  60. break;
  61. case "3":
  62. applyConfig['date'] = 'last_time_unknown';
  63. break;
  64. }
  65. }
  66. result.push(this.parseIt(item, applyConfig));
  67. }, this);
  68. App.store.loadMany(this.get('model'), result);
  69. }
  70. }
  71. },
  72. update: function(alerts){
  73. var alertsList = App.Alert.find();
  74. var titleToAlertMap = {};
  75. alertsList.forEach(function(alert){
  76. titleToAlertMap[alert.get('serviceType') + alert.get('title') + alert.get('hostName')] = alert;
  77. });
  78. var newRecords = [];
  79. alerts.forEach(function(item){
  80. var existAlert = titleToAlertMap[item.service_type + item.service_description + item.host_name];
  81. if (existAlert == null) {
  82. var applyConfig = jQuery.extend({}, this.config);
  83. if (item.current_state && item.last_hard_state && item.current_state != item.last_hard_state) {
  84. switch (item.current_state) {
  85. case "0":
  86. applyConfig['date'] = 'last_time_ok';
  87. break;
  88. case "1":
  89. applyConfig['date'] = 'last_time_warning';
  90. break;
  91. case "2":
  92. applyConfig['date'] = 'last_time_critical';
  93. break;
  94. case "3":
  95. applyConfig['date'] = 'last_time_unknown';
  96. break;
  97. }
  98. }
  99. newRecords.push(this.parseIt(item, applyConfig));
  100. } else {
  101. // update record
  102. existAlert.set('serviceType', item.service_type);
  103. if (item.current_state && item.last_hard_state && item.current_state != item.last_hard_state) {
  104. switch (item.current_state) {
  105. case "0":
  106. existAlert.set('date', DS.attr.transforms.date.from(item.last_time_ok));
  107. break;
  108. case "1":
  109. existAlert.set('date', DS.attr.transforms.date.from(item.last_time_warning));
  110. break;
  111. case "2":
  112. existAlert.set('date', DS.attr.transforms.date.from(item.last_time_critical));
  113. break;
  114. case "3":
  115. existAlert.set('date', DS.attr.transforms.date.from(item.last_time_unknown));
  116. break;
  117. default:
  118. existAlert.set('date', DS.attr.transforms.date.from(item.last_hard_state_change));
  119. break;
  120. }
  121. }else{
  122. existAlert.set('date', DS.attr.transforms.date.from(item.last_hard_state_change));
  123. }
  124. existAlert.set('status', item.current_state);
  125. existAlert.set('message', item.plugin_output);
  126. existAlert.set('lastHardStateChange', item.last_hard_state_change);
  127. existAlert.set('lastHardState', item.last_hard_state);
  128. existAlert.set('lastTimeOk', item.last_time_ok);
  129. existAlert.set('lastTimeWarning', item.last_time_warning);
  130. existAlert.set('lastTimeUnknown', item.last_time_unknown);
  131. existAlert.set('lastTimeCritical', item.last_time_critical);
  132. existAlert.set('lastCheck', item.last_check);
  133. existAlert.set('isFlapping', item.is_flapping);
  134. delete titleToAlertMap[item.service_type + item.service_description + item.host_name];
  135. }
  136. }, this);
  137. for ( var e in titleToAlertMap) {
  138. titleToAlertMap[e].deleteRecord();
  139. }
  140. if (newRecords.length > 0) {
  141. App.store.loadMany(this.get('model'), newRecords); // Add new records
  142. }
  143. }
  144. });