diff --git a/src/main/java/org/elasticsearch/alerting/AlertActionManager.java b/src/main/java/org/elasticsearch/alerting/AlertActionManager.java index c29035125a8..b523efdf5d8 100644 --- a/src/main/java/org/elasticsearch/alerting/AlertActionManager.java +++ b/src/main/java/org/elasticsearch/alerting/AlertActionManager.java @@ -25,6 +25,7 @@ public class AlertActionManager extends AbstractComponent { this.alertManager = alertManager; this.actionImplemented = new HashMap<>(); registerAction("email", new EmailAlertActionFactory()); + registerAction("index", new IndexAlertActionFactory()); } public void registerAction(String name, AlertActionFactory actionFactory){ diff --git a/src/main/java/org/elasticsearch/alerting/AlertResult.java b/src/main/java/org/elasticsearch/alerting/AlertResult.java index 6ce2fa2e8eb..d25b4216438 100644 --- a/src/main/java/org/elasticsearch/alerting/AlertResult.java +++ b/src/main/java/org/elasticsearch/alerting/AlertResult.java @@ -6,6 +6,7 @@ package org.elasticsearch.alerting; import org.elasticsearch.action.search.SearchResponse; +import org.elasticsearch.common.joda.time.DateTime; import org.elasticsearch.common.xcontent.XContentBuilder; import java.util.Arrays; @@ -14,14 +15,16 @@ public class AlertResult { public SearchResponse searchResponse; public AlertTrigger trigger; public String alertName; + public DateTime fireTime; - public AlertResult(String alertName, SearchResponse searchResponse, AlertTrigger trigger, boolean isTriggered, XContentBuilder query, String[] indices) { + public AlertResult(String alertName, SearchResponse searchResponse, AlertTrigger trigger, boolean isTriggered, XContentBuilder query, String[] indices, DateTime fireTime) { this.searchResponse = searchResponse; this.trigger = trigger; this.isTriggered = isTriggered; this.query = query; this.indices = indices; this.alertName = alertName; + this.fireTime = fireTime; } public boolean isTriggered; diff --git a/src/main/java/org/elasticsearch/alerting/AlertScheduler.java b/src/main/java/org/elasticsearch/alerting/AlertScheduler.java index 11bb5730d60..dd1b6340f42 100644 --- a/src/main/java/org/elasticsearch/alerting/AlertScheduler.java +++ b/src/main/java/org/elasticsearch/alerting/AlertScheduler.java @@ -80,7 +80,9 @@ public class AlertScheduler extends AbstractLifecycleComponent { } SearchResponse sr = srb.execute().get(); logger.warn("Got search response hits : [{}]", sr.getHits().getTotalHits() ); - AlertResult result = new AlertResult(alertName, sr, alert.trigger(), triggerManager.isTriggered(alertName,sr), builder, indices); + AlertResult result = new AlertResult(alertName, sr, alert.trigger(), + triggerManager.isTriggered(alertName,sr), builder, indices, + new DateTime(jobExecutionContext.getScheduledFireTime())); if (result.isTriggered) { logger.warn("We have triggered"); diff --git a/src/main/java/org/elasticsearch/alerting/IndexAlertAction.java b/src/main/java/org/elasticsearch/alerting/IndexAlertAction.java new file mode 100644 index 00000000000..5c021a907bf --- /dev/null +++ b/src/main/java/org/elasticsearch/alerting/IndexAlertAction.java @@ -0,0 +1,61 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ +package org.elasticsearch.alerting; + +import org.elasticsearch.ElasticsearchException; +import org.elasticsearch.action.index.IndexRequest; +import org.elasticsearch.client.Client; +import org.elasticsearch.common.inject.Inject; +import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.common.xcontent.XContentFactory; + +import java.io.IOException; + +public class IndexAlertAction implements AlertAction { + private final String index; + private final String type; + private Client client = null; + + public IndexAlertAction(String index, String type){ + this.index = index; + this.type = type; + } + + @Inject + public void setClient(Client client){ + this.client = client; + } + + @Override + public String getActionName() { + return "index"; + } + + @Override + public XContentBuilder toXContent(XContentBuilder builder) throws IOException { + builder.startObject(); + builder.field("index", index); + builder.field("type", type); + builder.endObject(); + return builder; + } + + @Override + public boolean doAction(String alertName, AlertResult alertResult) { + IndexRequest indexRequest = new IndexRequest(); + indexRequest.index(index); + indexRequest.type(type); + try { + XContentBuilder resultBuilder = XContentFactory.jsonBuilder(); + alertResult.searchResponse.toXContent(resultBuilder,null); + resultBuilder.field("timestamp", alertResult.fireTime); + indexRequest.source(resultBuilder); + } catch (IOException ie) { + throw new ElasticsearchException("Unable to create XContentBuilder",ie); + } + return client.index(indexRequest).actionGet().isCreated(); + } +} diff --git a/src/main/java/org/elasticsearch/alerting/IndexAlertActionFactory.java b/src/main/java/org/elasticsearch/alerting/IndexAlertActionFactory.java new file mode 100644 index 00000000000..a4613d625bc --- /dev/null +++ b/src/main/java/org/elasticsearch/alerting/IndexAlertActionFactory.java @@ -0,0 +1,35 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ +package org.elasticsearch.alerting; + +import org.elasticsearch.ElasticsearchIllegalArgumentException; +import org.elasticsearch.client.Client; + +import java.util.Map; + +/** + * Created by brian on 8/17/14. + */ +public class IndexAlertActionFactory implements AlertActionFactory { + + + + @Override + public AlertAction createAction(Object parameters) { + try { + if (parameters instanceof Map) { + Map paramMap = (Map) parameters; + String index = paramMap.get("index").toString(); + String type = paramMap.get("type").toString(); + return new IndexAlertAction(index, type); + } else { + throw new ElasticsearchIllegalArgumentException("Unable to parse [" + parameters + "] as an EmailAlertAction"); + } + } catch (Throwable t){ + throw new ElasticsearchIllegalArgumentException("Unable to parse [" + parameters + "] as an EmailAlertAction"); + } + } +}