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.io.InputStream;
020import java.nio.charset.StandardCharsets;
021
022import org.apache.http.HttpStatus;
023import org.elasticsearch.client.Request;
024import org.elasticsearch.client.Response;
025import org.elasticsearch.client.RestClient;
026
027import org.springframework.boot.actuate.health.AbstractHealthIndicator;
028import org.springframework.boot.actuate.health.Health;
029import org.springframework.boot.actuate.health.HealthIndicator;
030import org.springframework.boot.json.JsonParser;
031import org.springframework.boot.json.JsonParserFactory;
032import org.springframework.util.StreamUtils;
033
034/**
035 * {@link HealthIndicator} for an Elasticsearch cluster using a {@link RestClient}.
036 *
037 * @author Artsiom Yudovin
038 * @author Brian Clozel
039 * @since 2.1.1
040 */
041public class ElasticsearchRestHealthIndicator extends AbstractHealthIndicator {
042
043        private static final String RED_STATUS = "red";
044
045        private final RestClient client;
046
047        private final JsonParser jsonParser;
048
049        public ElasticsearchRestHealthIndicator(RestClient client) {
050                super("Elasticsearch health check failed");
051                this.client = client;
052                this.jsonParser = JsonParserFactory.getJsonParser();
053        }
054
055        @Override
056        protected void doHealthCheck(Health.Builder builder) throws Exception {
057                Response response = this.client
058                                .performRequest(new Request("GET", "/_cluster/health/"));
059                if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
060                        builder.down();
061                        return;
062                }
063                try (InputStream inputStream = response.getEntity().getContent()) {
064                        doHealthCheck(builder,
065                                        StreamUtils.copyToString(inputStream, StandardCharsets.UTF_8));
066                }
067        }
068
069        private void doHealthCheck(Health.Builder builder, String json) {
070                String status = (String) this.jsonParser.parseMap(json).get("status");
071                if (RED_STATUS.equals(status)) {
072                        builder.outOfService();
073                        return;
074                }
075                builder.up();
076        }
077
078}