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-10-28 18:49:23 +01:00
|
|
|
import org.elasticsearch.alerts.actions.*;
|
2014-10-24 14:31:20 +02:00
|
|
|
import org.elasticsearch.alerts.plugin.AlertsPlugin;
|
2014-10-24 13:01:45 +01:00
|
|
|
import org.elasticsearch.alerts.triggers.AlertTrigger;
|
|
|
|
import org.elasticsearch.alerts.triggers.ScriptedAlertTrigger;
|
2014-11-04 15:01:25 +00:00
|
|
|
import org.elasticsearch.common.io.stream.StreamInput;
|
|
|
|
import org.elasticsearch.common.io.stream.StreamOutput;
|
2014-10-24 12:49:33 +02:00
|
|
|
import org.elasticsearch.common.settings.ImmutableSettings;
|
|
|
|
import org.elasticsearch.common.settings.Settings;
|
|
|
|
import org.elasticsearch.common.xcontent.XContentBuilder;
|
2014-10-29 00:54:18 +01:00
|
|
|
import org.elasticsearch.common.xcontent.XContentParser;
|
2014-11-04 11:27:58 +01:00
|
|
|
import org.elasticsearch.index.query.QueryBuilders;
|
2014-10-24 12:49:33 +02:00
|
|
|
import org.elasticsearch.script.ScriptService;
|
|
|
|
import org.elasticsearch.test.ElasticsearchIntegrationTest;
|
|
|
|
import org.junit.Test;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.Arrays;
|
2014-10-24 16:51:12 +02:00
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
import java.util.concurrent.atomic.AtomicBoolean;
|
2014-10-24 12:49:33 +02:00
|
|
|
|
|
|
|
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
|
|
|
|
import static org.hamcrest.core.Is.is;
|
|
|
|
|
|
|
|
/**
|
|
|
|
*/
|
2014-10-24 12:24:04 +01:00
|
|
|
@ElasticsearchIntegrationTest.ClusterScope(scope = ElasticsearchIntegrationTest.Scope.SUITE, numClientNodes = 0, transportClientRatio = 0, maxNumDataNodes = 1, minNumDataNodes = 1, numDataNodes = 1)
|
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
|
|
|
|
// TODO: add request, response & request builder etc.
|
|
|
|
public void testAlerSchedulerStartsProperly() throws Exception {
|
|
|
|
createIndex("my-index");
|
2014-10-31 10:28:42 +01:00
|
|
|
createIndex(AlertsStore.ALERT_INDEX);
|
2014-10-24 12:24:04 +01:00
|
|
|
createIndex(AlertActionManager.ALERT_HISTORY_INDEX);
|
2014-10-31 10:28:42 +01:00
|
|
|
ensureGreen("my-index", AlertsStore.ALERT_INDEX, AlertActionManager.ALERT_HISTORY_INDEX);
|
2014-10-24 12:49:33 +02:00
|
|
|
|
2014-10-25 23:36:15 +02:00
|
|
|
final AlertManager alertManager = internalCluster().getInstance(AlertManager.class, internalCluster().getMasterName());
|
|
|
|
assertBusy(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
assertThat(alertManager.isStarted(), is(true));
|
|
|
|
}
|
|
|
|
});
|
2014-10-24 16:51:12 +02:00
|
|
|
final AtomicBoolean alertActionInvoked = new AtomicBoolean(false);
|
2014-10-24 12:49:33 +02:00
|
|
|
final AlertAction alertAction = new AlertAction() {
|
|
|
|
@Override
|
|
|
|
public String getActionName() {
|
|
|
|
return "test";
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
|
|
|
|
builder.startObject();
|
|
|
|
builder.endObject();
|
|
|
|
return builder;
|
|
|
|
}
|
|
|
|
|
2014-11-04 15:01:25 +00:00
|
|
|
@Override
|
|
|
|
public void writeTo(StreamOutput out) throws IOException {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void readFrom(StreamInput in) throws IOException {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-10-24 12:49:33 +02:00
|
|
|
@Override
|
2014-10-24 12:24:04 +01:00
|
|
|
public boolean doAction(Alert alert, AlertActionEntry actionEntry) {
|
|
|
|
logger.info("Alert {} invoked: {}", alert.alertName(), actionEntry);
|
2014-10-24 16:51:12 +02:00
|
|
|
alertActionInvoked.set(true);
|
2014-10-24 12:49:33 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
2014-10-24 12:24:04 +01:00
|
|
|
AlertActionRegistry alertActionRegistry = internalCluster().getInstance(AlertActionRegistry.class, internalCluster().getMasterName());
|
|
|
|
alertActionRegistry.registerAction("test", new AlertActionFactory() {
|
2014-10-24 12:49:33 +02:00
|
|
|
@Override
|
2014-10-29 00:54:18 +01:00
|
|
|
public AlertAction createAction(XContentParser parser) throws IOException {
|
|
|
|
parser.nextToken();
|
2014-10-24 12:49:33 +02:00
|
|
|
return alertAction;
|
|
|
|
}
|
2014-11-04 15:01:25 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public AlertAction readFrom(StreamInput in) throws IOException {
|
|
|
|
return alertAction;
|
|
|
|
}
|
2014-10-24 12:49:33 +02:00
|
|
|
});
|
|
|
|
AlertTrigger alertTrigger = new AlertTrigger(new ScriptedAlertTrigger("return true", ScriptService.ScriptType.INLINE, "groovy"));
|
|
|
|
Alert alert = new Alert(
|
|
|
|
"my-first-alert",
|
2014-11-04 11:27:58 +01:00
|
|
|
client().prepareSearch("my-index").setQuery(QueryBuilders.matchAllQuery()).request(),
|
2014-10-24 12:49:33 +02:00
|
|
|
alertTrigger,
|
|
|
|
Arrays.asList(alertAction),
|
2014-10-24 16:51:12 +02:00
|
|
|
"0/5 * * * * ? *",
|
2014-10-24 12:49:33 +02:00
|
|
|
null,
|
|
|
|
1,
|
|
|
|
true
|
2014-10-24 18:50:47 +02:00
|
|
|
);
|
2014-10-28 18:49:23 +01:00
|
|
|
alertManager.addAlert("my-first-alert", jsonBuilder().value(alert).bytes());
|
2014-10-24 16:51:12 +02:00
|
|
|
assertBusy(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
assertThat(alertActionInvoked.get(), is(true));
|
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));
|
|
|
|
}
|
|
|
|
}, 30, TimeUnit.SECONDS);
|
2014-10-24 12:49:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|