Alerting : Add index action.

This commit adds support for indexing on alert trigger.

Original commit: elastic/x-pack-elasticsearch@d6508ce16d
This commit is contained in:
Brian Murphy 2014-08-17 13:49:29 +01:00
parent d1af9f15fe
commit 83287e009a
5 changed files with 104 additions and 2 deletions

View File

@ -25,6 +25,7 @@ public class AlertActionManager extends AbstractComponent {
this.alertManager = alertManager; this.alertManager = alertManager;
this.actionImplemented = new HashMap<>(); this.actionImplemented = new HashMap<>();
registerAction("email", new EmailAlertActionFactory()); registerAction("email", new EmailAlertActionFactory());
registerAction("index", new IndexAlertActionFactory());
} }
public void registerAction(String name, AlertActionFactory actionFactory){ public void registerAction(String name, AlertActionFactory actionFactory){

View File

@ -6,6 +6,7 @@
package org.elasticsearch.alerting; package org.elasticsearch.alerting;
import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.common.joda.time.DateTime;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import java.util.Arrays; import java.util.Arrays;
@ -14,14 +15,16 @@ public class AlertResult {
public SearchResponse searchResponse; public SearchResponse searchResponse;
public AlertTrigger trigger; public AlertTrigger trigger;
public String alertName; 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.searchResponse = searchResponse;
this.trigger = trigger; this.trigger = trigger;
this.isTriggered = isTriggered; this.isTriggered = isTriggered;
this.query = query; this.query = query;
this.indices = indices; this.indices = indices;
this.alertName = alertName; this.alertName = alertName;
this.fireTime = fireTime;
} }
public boolean isTriggered; public boolean isTriggered;

View File

@ -80,7 +80,9 @@ public class AlertScheduler extends AbstractLifecycleComponent {
} }
SearchResponse sr = srb.execute().get(); SearchResponse sr = srb.execute().get();
logger.warn("Got search response hits : [{}]", sr.getHits().getTotalHits() ); 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) { if (result.isTriggered) {
logger.warn("We have triggered"); logger.warn("We have triggered");

View File

@ -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();
}
}

View File

@ -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<String, Object> paramMap = (Map<String, Object>) 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");
}
}
}