@Override public void sendNotification(Check check, Subscription subscription, List<Alert> alerts) { try { VelocityContext context = createVelocityContext(check, subscription, alerts); StringWriter w = new StringWriter(); Velocity.evaluate(context, w, "EmailNotificationService", getTemplateAsString()); Email email = new Email() .withTo(subscription.getTarget()) .withFrom(seyrenConfig.getFromEmail()) .withSubject(createSubject(check)) .withMessage(w.getBuffer().toString()); mailSender.send(createMimeMessage(email)); } catch (Exception e) { throw new NotificationFailedException( "Failed to send notification to " + subscription.getTarget() + " from " + seyrenConfig.getFromEmail(), e); } }
@Inject public SeyrenMailSender(SeyrenConfig seyrenConfig) { int port = seyrenConfig.getSmtpPort(); String host = seyrenConfig.getSmtpHost(); String username = seyrenConfig.getSmtpUsername(); String password = seyrenConfig.getSmtpPassword(); String protocol = seyrenConfig.getSmtpProtocol(); setPort(port); setHost(host); Properties props = new Properties(); if (StringUtils.isNotEmpty(username) && StringUtils.isNotEmpty(password)) { props.setProperty("mail.smtp.auth", "true"); setUsername(username); setPassword(password); } if (getPort() == 587) { props.put("mail.smtp.starttls.enable", "true"); } if (props.size() > 0) { setJavaMailProperties(props); } setProtocol(protocol); LOGGER.info("{}:{}@{}", username, password, host); }
private VelocityContext createVelocityContext( Check check, Subscription subscription, List<Alert> alerts) { VelocityContext result = new VelocityContext(); result.put("CHECK", check); result.put("ALERTS", alerts); result.put("SEYREN_URL", seyrenConfig.getBaseUrl()); return result; }
private Map<String, String> details(Check check, List<Alert> alerts) throws JsonProcessingException { ObjectMapper mapper = new ObjectMapper(); mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); mapper.configure(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES, false); mapper.setPropertyNamingStrategy(new PropertyNamingStrategy.LowerCaseWithUnderscoresStrategy()); return ImmutableMap.<String, String>builder() .put("CHECK", mapper.writeValueAsString(check)) .put("STATE", check.getState().name()) .put("ALERTS", mapper.writeValueAsString(alerts)) .put("SEYREN_URL", seyrenConfig.getBaseUrl()) .build(); }
@Inject public MongoStore(SeyrenConfig seyrenConfig) { try { String uri = seyrenConfig.getMongoUrl(); MongoURI mongoUri = new MongoURI(uri); DB mongo = mongoUri.connectDB(); if (mongoUri.getUsername() != null) { mongo.authenticate(mongoUri.getUsername(), mongoUri.getPassword()); } this.mongo = mongo; } catch (Exception e) { throw new RuntimeException(e); } }
@Override public void sendNotification(Check check, Subscription subscription, List<Alert> alerts) throws NotificationFailedException { LOGGER.info("Script Location: {}", seyrenConfig.getScriptPath()); ProcessBuilder pb = new ProcessBuilder( seyrenConfig.getScriptType(), seyrenConfig.getScriptPath(), subscription.getTarget(), new Gson().toJson(check)); try { LOGGER.info("Script Start"); Process p = pb.start(); BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream())); String line; while ((line = reader.readLine()) != null) { LOGGER.info(line); } LOGGER.info("Script End"); } catch (IOException e) { LOGGER.info("Script could not be sent: {}", e.getMessage()); throw new NotificationFailedException("Could not send message through the script"); } }
@Test public void defaultGraphiteTrustStoreIsCorrect() { assertThat(config.getGraphiteTrustStore(), is("")); }
@Test public void defaultGraphiteKeyStorePasswordIsCorrect() { assertThat(config.getGraphiteKeyStorePassword(), is("")); }
@Test public void defaultGraphitePathIsCorrect() { assertThat(config.getGraphitePath(), is("")); }
@Test public void defaultHipChatUsernameIsCorrect() { assertThat(config.getHipChatUsername(), is("Seyren Alert")); }
@Test public void defaultSmtpPortIsCorrect() { assertThat(config.getSmtpPort(), is(25)); }
@Test public void defaultMongoUrlIsCorrect() { assertThat(config.getMongoUrl(), is("mongodb://localhost:27017/seyren")); }
@Test public void defaultSmtpPasswordIsCorrect() { assertThat(config.getSmtpPassword(), is("")); }
@Test public void defaultFlowdockEmojisIsCorrect() { assertThat(config.getFlowdockEmojis(), is("")); }
@Test public void defaultBaseUrlIsCorrect() { if (config.isBaseUrlSetToDefault()) { assertThat(config.getBaseUrl(), is("http://localhost:8080/seyren")); } }
@Test public void defaultFlowdockExternalUsernameIsCorrect() { assertThat(config.getFlowdockExternalUsername(), is("Seyren")); }
@Test public void defaultHubotUrlIsCorrect() { assertThat(config.getHubotUrl(), is("")); }
@Test public void defaultPagerDutyDomainIsCorrect() { assertThat(config.getPagerDutyDomain(), is("")); }
@Test public void defaultSmtpFromIsCorrect() { assertThat(config.getSmtpFrom(), is("alert@seyren")); }
@Test public void defaultGraphiteUrlIsCorrect() { assertThat(config.getGraphiteUrl(), is("http://localhost:80")); }
@Test public void defaultSmtpUsernameIsCorrect() { assertThat(config.getSmtpUsername(), is("")); }
private String url(Check check) { return seyrenConfig.getBaseUrl() + "/#/checks/" + check.getId(); }
@Test public void defaultSmtpHostIsCorrect() { assertThat(config.getSmtpHost(), is("localhost")); }
@Test public void defaultGraphiteSchemeIsCorrect() { assertThat(config.getGraphiteScheme(), is("http")); }
@Test public void defaultSmtpProtocolIsCorrect() { assertThat(config.getSmtpProtocol(), is("smtp")); }
@Test public void defaultGraphiteHostIsCorrect() { assertThat(config.getGraphiteHost(), is("localhost:80")); }
@Test public void defaultGraphiteUsernameIsCorrect() { assertThat(config.getGraphiteUsername(), is("")); }
@Test public void defaultHipChatAuthTokenIsCorrect() { assertThat(config.getHipChatAuthToken(), is("")); }