2014-10-24 12:49:33 +02:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2014-10-24 13:01:45 +01:00
|
|
|
package org.elasticsearch.alerts;
|
2014-10-24 12:49:33 +02:00
|
|
|
|
2014-10-24 16:51:12 +02:00
|
|
|
import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse;
|
2014-11-06 21:25:53 +01:00
|
|
|
import org.elasticsearch.action.search.SearchRequest;
|
|
|
|
import org.elasticsearch.action.search.SearchResponse;
|
|
|
|
import org.elasticsearch.alerts.actions.AlertActionManager;
|
|
|
|
import org.elasticsearch.alerts.actions.AlertActionState;
|
2014-11-05 10:05:17 +00:00
|
|
|
import org.elasticsearch.alerts.client.AlertsClient;
|
|
|
|
import org.elasticsearch.alerts.client.AlertsClientInterface;
|
2014-10-24 14:31:20 +02:00
|
|
|
import org.elasticsearch.alerts.plugin.AlertsPlugin;
|
2014-11-05 10:05:17 +00:00
|
|
|
import org.elasticsearch.alerts.transport.actions.delete.DeleteAlertRequest;
|
|
|
|
import org.elasticsearch.alerts.transport.actions.delete.DeleteAlertResponse;
|
2014-11-06 21:25:53 +01:00
|
|
|
import org.elasticsearch.common.bytes.BytesReference;
|
2014-10-24 12:49:33 +02:00
|
|
|
import org.elasticsearch.common.settings.ImmutableSettings;
|
|
|
|
import org.elasticsearch.common.settings.Settings;
|
2014-11-06 11:08:57 +00:00
|
|
|
import org.elasticsearch.common.xcontent.ToXContent;
|
2014-10-24 12:49:33 +02:00
|
|
|
import org.elasticsearch.common.xcontent.XContentBuilder;
|
2014-11-06 21:25:53 +01:00
|
|
|
import org.elasticsearch.common.xcontent.XContentHelper;
|
2014-10-24 12:49:33 +02:00
|
|
|
import org.elasticsearch.test.ElasticsearchIntegrationTest;
|
2014-11-06 22:15:11 +01:00
|
|
|
import org.junit.After;
|
2014-10-24 12:49:33 +02:00
|
|
|
import org.junit.Test;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
2014-10-24 16:51:12 +02:00
|
|
|
import java.util.concurrent.TimeUnit;
|
2014-10-24 12:49:33 +02:00
|
|
|
|
2014-11-06 21:25:53 +01:00
|
|
|
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
|
|
|
|
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
|
|
|
|
import static org.elasticsearch.index.query.QueryBuilders.termQuery;
|
|
|
|
import static org.elasticsearch.search.builder.SearchSourceBuilder.searchSource;
|
|
|
|
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount;
|
|
|
|
import static org.hamcrest.Matchers.equalTo;
|
2014-10-24 12:49:33 +02:00
|
|
|
import static org.hamcrest.core.Is.is;
|
|
|
|
|
|
|
|
/**
|
|
|
|
*/
|
2014-11-06 22:52:44 +01:00
|
|
|
@ElasticsearchIntegrationTest.ClusterScope(scope = ElasticsearchIntegrationTest.Scope.SUITE, numClientNodes = 0, transportClientRatio = 0)
|
2014-10-24 12:49:33 +02:00
|
|
|
public class BasicAlertingTest extends ElasticsearchIntegrationTest {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected Settings nodeSettings(int nodeOrdinal) {
|
|
|
|
return ImmutableSettings.builder()
|
|
|
|
.put(super.nodeSettings(nodeOrdinal))
|
2014-10-29 10:20:36 +01:00
|
|
|
.put("scroll.size", randomIntBetween(1, 100))
|
2014-10-24 14:31:20 +02:00
|
|
|
.put("plugin.mandatory", "alerts")
|
|
|
|
.put("plugin.types", AlertsPlugin.class.getName())
|
2014-10-24 12:49:33 +02:00
|
|
|
.put("node.mode", "network")
|
2014-11-04 11:27:58 +01:00
|
|
|
.put("http.enabled", true)
|
2014-10-24 12:49:33 +02:00
|
|
|
.put("plugins.load_classpath_plugins", false)
|
|
|
|
.build();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2014-11-06 21:25:53 +01:00
|
|
|
public void testIndexAlert() throws Exception {
|
|
|
|
AlertsClientInterface alertsClient = alertClient();
|
2014-10-24 12:49:33 +02:00
|
|
|
createIndex("my-index");
|
2014-11-06 21:25:53 +01:00
|
|
|
// Have a sample document in the index, the alert is going to evaluate
|
|
|
|
client().prepareIndex("my-index", "my-type").setSource("field", "value").get();
|
|
|
|
SearchRequest searchRequest = new SearchRequest("my-index").source(searchSource().query(termQuery("field", "value")));
|
|
|
|
BytesReference alertSource = createAlertSource("0/5 * * * * ? *", searchRequest, "hits.total == 1");
|
2014-11-06 23:49:50 +01:00
|
|
|
alertsClient.prepareIndexAlert("my-first-alert")
|
2014-11-06 21:25:53 +01:00
|
|
|
.setAlertSource(alertSource)
|
|
|
|
.get();
|
2014-10-24 12:49:33 +02:00
|
|
|
|
2014-10-25 23:36:15 +02:00
|
|
|
assertBusy(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2014-10-24 12:24:04 +01:00
|
|
|
IndicesExistsResponse indicesExistsResponse = client().admin().indices().prepareExists(AlertActionManager.ALERT_HISTORY_INDEX).get();
|
2014-10-24 16:51:12 +02:00
|
|
|
assertThat(indicesExistsResponse.isExists(), is(true));
|
2014-11-06 21:25:53 +01:00
|
|
|
|
|
|
|
SearchResponse searchResponse = client().prepareSearch(AlertActionManager.ALERT_HISTORY_INDEX)
|
|
|
|
.setQuery(termQuery("state", AlertActionState.ACTION_PERFORMED.toString()))
|
|
|
|
.addField("response.hits.total")
|
|
|
|
.setSize(1)
|
|
|
|
.get();
|
|
|
|
assertThat(searchResponse.getHits().getHits().length, equalTo(1));
|
|
|
|
assertThat((Integer) searchResponse.getHits().getAt(0).field("response.hits.total").getValue(), equalTo(1));
|
2014-10-24 16:51:12 +02:00
|
|
|
}
|
|
|
|
}, 30, TimeUnit.SECONDS);
|
2014-11-06 21:25:53 +01:00
|
|
|
}
|
2014-11-05 10:05:17 +00:00
|
|
|
|
2014-11-06 21:25:53 +01:00
|
|
|
@Test
|
|
|
|
public void testDeleteAlert() throws Exception {
|
|
|
|
AlertsClientInterface alertsClient = alertClient();
|
|
|
|
createIndex("my-index");
|
|
|
|
// Have a sample document in the index, the alert is going to evaluate
|
|
|
|
client().prepareIndex("my-index", "my-type").setSource("field", "value").get();
|
|
|
|
SearchRequest searchRequest = new SearchRequest("my-index").source(searchSource().query(matchAllQuery()));
|
|
|
|
BytesReference alertSource = createAlertSource("0/5 * * * * ? *", searchRequest, "hits.total == 1");
|
2014-11-06 23:49:50 +01:00
|
|
|
alertsClient.prepareIndexAlert("my-first-alert")
|
2014-11-06 21:25:53 +01:00
|
|
|
.setAlertSource(alertSource)
|
|
|
|
.get();
|
|
|
|
|
|
|
|
DeleteAlertRequest deleteAlertRequest = new DeleteAlertRequest("my-first-alert");
|
2014-11-05 10:05:17 +00:00
|
|
|
DeleteAlertResponse deleteAlertResponse = alertsClient.deleteAlert(deleteAlertRequest).actionGet();
|
2014-11-05 17:50:05 +00:00
|
|
|
assertNotNull(deleteAlertResponse.deleteResponse());
|
|
|
|
assertTrue(deleteAlertResponse.deleteResponse().isFound());
|
2014-11-05 10:05:17 +00:00
|
|
|
|
2014-11-06 21:25:53 +01:00
|
|
|
assertHitCount(client().prepareCount(AlertsStore.ALERT_INDEX).get(), 0l);
|
|
|
|
}
|
|
|
|
|
2014-11-06 22:15:11 +01:00
|
|
|
@After
|
2014-11-06 21:25:53 +01:00
|
|
|
public void clearAlerts() {
|
|
|
|
// Clear all in-memory alerts on all nodes, perhaps there isn't an elected master at this point
|
|
|
|
for (AlertManager manager : internalCluster().getInstances(AlertManager.class)) {
|
|
|
|
manager.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private BytesReference createAlertSource(String cron, SearchRequest request, String scriptTrigger) throws IOException {
|
|
|
|
XContentBuilder builder = jsonBuilder().startObject();
|
|
|
|
builder.field("schedule", cron);
|
|
|
|
builder.field("enable", true);
|
|
|
|
|
|
|
|
builder.startObject("request");
|
|
|
|
XContentHelper.writeRawField("body", request.source(), builder, ToXContent.EMPTY_PARAMS);
|
|
|
|
builder.startArray("indices");
|
|
|
|
for (String index : request.indices()) {
|
|
|
|
builder.value(index);
|
|
|
|
}
|
|
|
|
builder.endArray();
|
|
|
|
builder.endObject();
|
|
|
|
|
|
|
|
builder.startObject("trigger");
|
|
|
|
builder.startObject("script");
|
|
|
|
builder.field("script", scriptTrigger);
|
|
|
|
builder.endObject();
|
|
|
|
builder.endObject();
|
|
|
|
|
|
|
|
builder.startObject("actions");
|
|
|
|
builder.startObject("index");
|
|
|
|
builder.field("index", "my-index");
|
|
|
|
builder.field("type", "trail");
|
|
|
|
builder.endObject();
|
|
|
|
builder.endObject();
|
|
|
|
|
|
|
|
return builder.endObject().bytes();
|
|
|
|
}
|
|
|
|
|
|
|
|
private AlertsClient alertClient() {
|
2014-11-06 22:52:44 +01:00
|
|
|
return internalCluster().getInstance(AlertsClient.class);
|
2014-10-24 12:49:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|