|
| 1 | +package de.gdata.mobilelab.alertmanagercallback; |
| 2 | + |
| 3 | +import org.graylog2.plugin.alarms.AlertCondition; |
| 4 | +import org.graylog2.plugin.configuration.Configuration; |
| 5 | +import org.graylog2.plugin.streams.Stream; |
| 6 | +import org.joda.time.DateTime; |
| 7 | + |
| 8 | +import java.util.HashMap; |
| 9 | +import java.util.Map; |
| 10 | + |
| 11 | +class AlertManagerPayloadBuilder { |
| 12 | + |
| 13 | + private static final String STREAM_TITLE_KEY = "stream_title"; |
| 14 | + private static final String ALERTMANAGER_ALERT_NAME_KEY = "alertmanager_alert_name"; |
| 15 | + private static final String ALERTNAME_KEY = "alertname"; |
| 16 | + private Stream stream; |
| 17 | + private AlertCondition.CheckResult checkResult; |
| 18 | + private Configuration configuration; |
| 19 | + |
| 20 | + private AlertManagerPayloadBuilder() { |
| 21 | + // Private constructor to hide the implicit one |
| 22 | + } |
| 23 | + |
| 24 | + static AlertManagerPayloadBuilder newInstance() { |
| 25 | + return new AlertManagerPayloadBuilder(); |
| 26 | + } |
| 27 | + |
| 28 | + AlertManagerPayloadBuilder withStream(Stream stream) { |
| 29 | + this.stream = stream; |
| 30 | + |
| 31 | + return this; |
| 32 | + } |
| 33 | + |
| 34 | + AlertManagerPayloadBuilder withCheckResult(AlertCondition.CheckResult checkResult) { |
| 35 | + this.checkResult = checkResult; |
| 36 | + |
| 37 | + return this; |
| 38 | + } |
| 39 | + |
| 40 | + AlertManagerPayloadBuilder withConfiguration(Configuration configuration) { |
| 41 | + this.configuration = configuration; |
| 42 | + |
| 43 | + return this; |
| 44 | + } |
| 45 | + |
| 46 | + AlertManagerPayload build() { |
| 47 | + AlertManagerPayload alertManagerPayload = new AlertManagerPayload(); |
| 48 | + alertManagerPayload.setAnnotations(extractAnnotations()); |
| 49 | + alertManagerPayload.setLabels(extractLabels()); |
| 50 | + alertManagerPayload.setGeneratorURL(extractStreamUrl()); |
| 51 | + alertManagerPayload.setStartsAt(extractStartsAt()); |
| 52 | + alertManagerPayload.setEndsAt(extractEndsAt()); |
| 53 | + |
| 54 | + return alertManagerPayload; |
| 55 | + } |
| 56 | + |
| 57 | + private Map<String, Object> extractLabels() { |
| 58 | + Map<String, Object> labels = new HashMap<>(); |
| 59 | + if(configuration != null && configuration.getString(ALERTMANAGER_ALERT_NAME_KEY) != null) { |
| 60 | + labels.put(ALERTNAME_KEY, configuration.getString(ALERTMANAGER_ALERT_NAME_KEY)); |
| 61 | + } else { |
| 62 | + labels.put(ALERTNAME_KEY, "Please add a valid configuration object to AlertManager plugin."); |
| 63 | + } |
| 64 | + |
| 65 | + return labels; |
| 66 | + } |
| 67 | + |
| 68 | + private Map<String, Object> extractAnnotations() { |
| 69 | + Map<String, Object> annotations = new HashMap<>(); |
| 70 | + |
| 71 | + if(stream != null && stream.getTitle() != null) { |
| 72 | + annotations.put(STREAM_TITLE_KEY, stream.getTitle()); |
| 73 | + } |
| 74 | + |
| 75 | + if(checkResult != null) { |
| 76 | + annotations.put("triggered_at", checkResult.getTriggeredAt() != null ? checkResult.getTriggeredAt().toString() : null); |
| 77 | + if(checkResult.getTriggeredCondition() != null) { |
| 78 | + annotations.put("triggered_rule_description", checkResult.getTriggeredCondition().getDescription()); |
| 79 | + annotations.put("triggered_rule_title", checkResult.getTriggeredCondition().getTitle()); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + return annotations; |
| 84 | + } |
| 85 | + |
| 86 | + private String extractEndsAt() { |
| 87 | + if(checkResult == null || checkResult.getTriggeredAt() == null || checkResult.getTriggeredCondition() == null) { |
| 88 | + return new DateTime().plusMinutes(1).toString(); |
| 89 | + } |
| 90 | + |
| 91 | + return checkResult.getTriggeredAt().plusMinutes(checkResult.getTriggeredCondition().getGrace()).toString(); |
| 92 | + } |
| 93 | + |
| 94 | + private String extractStartsAt() { |
| 95 | + if(checkResult == null || checkResult.getTriggeredAt() == null) { |
| 96 | + return new DateTime().toString(); |
| 97 | + } |
| 98 | + |
| 99 | + return checkResult.getTriggeredAt().toString(); |
| 100 | + } |
| 101 | + |
| 102 | + private String extractStreamUrl() { |
| 103 | + if(checkResult == null || checkResult.getTriggeredCondition() == null || checkResult.getTriggeredCondition().getParameters() == null) { |
| 104 | + return null; |
| 105 | + } |
| 106 | + |
| 107 | + return String.valueOf(checkResult.getTriggeredCondition().getParameters().get("stream_url")); |
| 108 | + } |
| 109 | +} |
0 commit comments