Exemplo n.º 1
0
  @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);
    }
  }
Exemplo n.º 2
0
 @Override
 public void updateSubscription(String checkId, Subscription subscription) {
   DBObject subscriptionObject = mapper.subscriptionToDBObject(subscription);
   DBObject subscriptionFindObject = forId(subscription.getId());
   DBObject checkFindObject =
       forId(checkId).with("subscriptions", object("$elemMatch", subscriptionFindObject));
   DBObject updateObject = object("$set", object("subscriptions.$", subscriptionObject));
   getChecksCollection().update(checkFindObject, updateObject);
 }
Exemplo n.º 3
0
 @Override
 public Subscription createSubscription(String checkId, Subscription subscription) {
   subscription.setId(ObjectId.get().toString());
   DBObject check = forId(checkId);
   DBObject query =
       object("$push", object("subscriptions", mapper.subscriptionToDBObject(subscription)));
   getChecksCollection().update(check, query);
   return subscription;
 }
  @Override
  public void sendNotification(Check check, Subscription subscription, List<Alert> alerts)
      throws NotificationFailedException {
    HttpClient httpclient = HttpClientBuilder.create().useSystemProperties().build();
    RestAdapter restadapter =
        new RestAdapter.Builder()
            .setClient(new ApacheClient(httpclient))
            .setEndpoint(Endpoints.newFixedEndpoint(baseUrl))
            .build();
    PagerDuty pagerDuty = PagerDuty.create(subscription.getTarget(), restadapter);
    NotifyResult result = null;

    try {
      if (check.getState() == AlertType.ERROR || check.getState() == AlertType.WARN) {
        Trigger trigger =
            new Trigger.Builder("Check '" + check.getName() + "' has exceeded its threshold.")
                .withIncidentKey(incidentKey(check))
                .client("Seyren")
                .clientUrl(url(check))
                .addDetails(details(check, alerts))
                .build();
        result = pagerDuty.notify(trigger);
      } else if (check.getState() == AlertType.OK) {
        Resolution resolution =
            new Resolution.Builder(incidentKey(check))
                .withDescription("Check '" + check.getName() + "' has been resolved.")
                .addDetails(details(check, alerts))
                .build();
        result = pagerDuty.notify(resolution);
      } else {
        LOGGER.warn(
            "Did not send notification to PagerDuty for check in state: {}", check.getState());
      }
    } catch (Exception e) {
      throw new NotificationFailedException("Failed to send notification to PagerDuty", e);
    }

    if (result != null && !"success".equals(result.status())) {
      throw new NotificationFailedException(
          "Failed to send notification to PagerDuty: '"
              + result.status()
              + "', "
              + result.message());
    }
  }
 @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");
   }
 }