BigPandaNotificationService implements NotificationService { @Override public void sendNotification(Check check, Subscription subscription, List<Alert> alerts) throws NotificationFailedException { String bigPandaAppKey = StringUtils.trimToNull(subscription.getTarget()); if (bigPandaAppKey == null) { LOGGER.warn("BigPanda Integration App Key in Subscription Target needs to be set before sending notifications to BigPanda"); return; } for (Alert alert : alerts) { String level = alert.getToType().toString(); String bigPandaStatus; if(level == "ERROR") { bigPandaStatus = "critical"; } else if(level == "WARN") { bigPandaStatus = "warning"; } else if(level == "OK") { bigPandaStatus = "ok"; } else { LOGGER.warn("Unknown level {} specified in alert. Can't send to BigPanda.", level); return; } String checkUrl = seyrenConfig.getBaseUrl() + "/#/checks/" + check.getId(); Long tstamp = alert.getTimestamp().getMillis() / 1000; Map<String, Object> body = new HashMap<String, Object>(); body.put("app_key", bigPandaAppKey); body.put("status", bigPandaStatus); body.put("service", check.getName()); body.put("check", alert.getTarget()); body.put("description", check.getDescription()); body.put("timestamp", tstamp); body.put("seyrenCheckUrl", checkUrl); body.put("currentValue", alert.getValue()); body.put("thresholdWarning", alert.getWarn()); body.put("thresholdCritical", alert.getError()); body.put("previewGraph", getPreviewImageUrl(check)); HttpClient client = HttpClientBuilder.create().useSystemProperties().build(); HttpPost post; if(StringUtils.isNotBlank(seyrenConfig.getBigPandaNotificationUrl())) { post = new HttpPost(seyrenConfig.getBigPandaNotificationUrl()); } else { LOGGER.warn("BigPanda API URL in Seyren Config needs to be set before sending notifications to BigPanda"); return; } String authBearer; if(StringUtils.isNotBlank(seyrenConfig.getBigPandaAuthBearer())) { authBearer = seyrenConfig.getBigPandaAuthBearer(); } else { LOGGER.warn("BigPanda Auth Bearer in Seyren Config needs to be set before sending notifications to BigPanda"); return; } try { post.addHeader("Authorization", "Bearer " + authBearer); HttpEntity entity = new StringEntity(MAPPER.writeValueAsString(body), ContentType.APPLICATION_JSON); post.setEntity(entity); LOGGER.info("Sending alert to BigPanda (AppKey: {}, Check: {}, Target: {}, Status: {})", bigPandaAppKey, check.getName(), alert.getTarget(), bigPandaStatus); HttpResponse response = client.execute(post); HttpEntity responseEntity = response.getEntity(); if(responseEntity!=null) { LOGGER.info("Response : {} ", EntityUtils.toString(responseEntity)); } } catch (Exception e) { throw new NotificationFailedException("Failed to send notification to HTTP", e); } finally { post.releaseConnection(); HttpClientUtils.closeQuietly(client); } } } @Inject  BigPandaNotificationService(SeyrenConfig seyrenConfig); @Override void sendNotification(Check check, Subscription subscription, List<Alert> alerts); @Override boolean canHandle(SubscriptionType subscriptionType);  }
@Test public void checkingOutTheHappyPath() { String seyrenUrl = clientDriver.getBaseUrl() + "/seyren"; when(mockSeyrenConfig.getGraphiteUrl()).thenReturn(clientDriver.getBaseUrl() + "/graphite"); when(mockSeyrenConfig.getBaseUrl()).thenReturn(seyrenUrl); when(mockSeyrenConfig.getBigPandaNotificationUrl()).thenReturn(clientDriver.getBaseUrl() + "/bigpanda/test"); when(mockSeyrenConfig.getBigPandaAuthBearer()).thenReturn("test-auth-bearer"); Check check = new Check() .withEnabled(true) .withName("check-name") .withDescription("Testing Description") .withTarget("the.target.name") .withState(AlertType.ERROR) .withWarn(BigDecimal.ONE) .withError(BigDecimal.TEN) .withId("testing"); Subscription subscription = new Subscription() .withType(SubscriptionType.BIGPANDA) .withTarget("testing_app_key"); DateTime timestamp = new DateTime(1420070400000L); Alert alert = new Alert() .withTarget("the.target.name") .withValue(BigDecimal.valueOf(12)) .withWarn(BigDecimal.valueOf(5)) .withError(BigDecimal.valueOf(10)) .withFromType(AlertType.WARN) .withToType(AlertType.ERROR) .withTimestamp(timestamp); List<Alert> alerts = Arrays.asList(alert); BodyCapture<JsonNode> bodyCapture = new JsonBodyCapture(); clientDriver.addExpectation( onRequestTo("/bigpanda/test") .withMethod(Method.POST) .capturingBodyIn(bodyCapture), giveResponse("success", "text/plain")); service.sendNotification(check, subscription, alerts); JsonNode node = bodyCapture.getContent(); assertThat(node, hasJsonPath("$.seyrenCheckUrl", is(seyrenUrl + "/#/checks/" + check.getId()))); assertThat(node, hasJsonPath("$.app_key", is("testing_app_key"))); assertThat(node, hasJsonPath("$.check", is("the.target.name"))); assertThat(node, hasJsonPath("$.status", is("critical"))); assertThat(node, hasJsonPath("$.service", is("check-name"))); assertThat(node, hasJsonPath("$.description", is("Testing Description"))); assertThat(node, hasJsonPath("$.timestamp", is(1420070400L))); assertThat(node, hasJsonPath("$.currentValue", is(12))); assertThat(node, hasJsonPath("$.thresholdWarning", is(5))); assertThat(node, hasJsonPath("$.thresholdCritical", is(10))); assertThat(node, hasJsonPath("$.previewGraph", containsString("the.target.name"))); assertThat(node, hasJsonPath("$.previewGraph", containsString("/graphite"))); }