001/*
002 * Copyright 2012-2018 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.boot.actuate.elasticsearch;
018
019import java.util.List;
020
021import org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest;
022import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
023import org.elasticsearch.client.Client;
024import org.elasticsearch.client.Requests;
025
026import org.springframework.boot.actuate.health.AbstractHealthIndicator;
027import org.springframework.boot.actuate.health.Health;
028import org.springframework.boot.actuate.health.HealthIndicator;
029import org.springframework.util.ObjectUtils;
030import org.springframework.util.StringUtils;
031
032/**
033 * {@link HealthIndicator} for an Elasticsearch cluster.
034 *
035 * @author Binwei Yang
036 * @author Andy Wilkinson
037 * @since 2.0.0
038 */
039public class ElasticsearchHealthIndicator extends AbstractHealthIndicator {
040
041        private static final String[] ALL_INDICES = { "_all" };
042
043        private final Client client;
044
045        private final String[] indices;
046
047        private final long responseTimeout;
048
049        /**
050         * Create a new {@link ElasticsearchHealthIndicator} instance.
051         * @param client the Elasticsearch client
052         * @param responseTimeout the request timeout in milliseconds
053         * @param indices the indices to check
054         */
055        public ElasticsearchHealthIndicator(Client client, long responseTimeout,
056                        List<String> indices) {
057                this(client, responseTimeout,
058                                (indices != null) ? StringUtils.toStringArray(indices) : null);
059        }
060
061        /**
062         * Create a new {@link ElasticsearchHealthIndicator} instance.
063         * @param client the Elasticsearch client
064         * @param responseTimeout the request timeout in milliseconds
065         * @param indices the indices to check
066         */
067        public ElasticsearchHealthIndicator(Client client, long responseTimeout,
068                        String... indices) {
069                super("Elasticsearch health check failed");
070                this.client = client;
071                this.responseTimeout = responseTimeout;
072                this.indices = indices;
073        }
074
075        @Override
076        protected void doHealthCheck(Health.Builder builder) throws Exception {
077                ClusterHealthRequest request = Requests.clusterHealthRequest(
078                                ObjectUtils.isEmpty(this.indices) ? ALL_INDICES : this.indices);
079                ClusterHealthResponse response = this.client.admin().cluster().health(request)
080                                .actionGet(this.responseTimeout);
081                switch (response.getStatus()) {
082                case GREEN:
083                case YELLOW:
084                        builder.up();
085                        break;
086                case RED:
087                default:
088                        builder.down();
089                        break;
090                }
091                builder.withDetail("clusterName", response.getClusterName());
092                builder.withDetail("numberOfNodes", response.getNumberOfNodes());
093                builder.withDetail("numberOfDataNodes", response.getNumberOfDataNodes());
094                builder.withDetail("activePrimaryShards", response.getActivePrimaryShards());
095                builder.withDetail("activeShards", response.getActiveShards());
096                builder.withDetail("relocatingShards", response.getRelocatingShards());
097                builder.withDetail("initializingShards", response.getInitializingShards());
098                builder.withDetail("unassignedShards", response.getUnassignedShards());
099        }
100
101}