001/*
002 * (C) Copyright 2017 Nuxeo (http://nuxeo.com/) and others.
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 * Contributors:
017 *     Mariana Cedica
018 */
019package org.nuxeo.elasticsearch.status;
020
021import java.util.List;
022
023import org.elasticsearch.cluster.health.ClusterHealthStatus;
024import org.nuxeo.ecm.core.management.api.Probe;
025import org.nuxeo.ecm.core.management.api.ProbeStatus;
026import org.nuxeo.elasticsearch.api.ElasticSearchAdmin;
027import org.nuxeo.runtime.api.Framework;
028
029/**
030 * Probe to check the status of the ES cluster. Returns success if the cluster is GREEN or YELLOW, Failure otherwise
031 *
032 * @since 9.3
033 */
034public class ElasticSearchStatusProbe implements Probe {
035
036    @Override
037    public ProbeStatus run() {
038        String[] indices = getIndexNames();
039        ClusterHealthStatus clusterStatus = Framework.getService(ElasticSearchAdmin.class).getClient().getHealthStatus(
040                indices);
041        switch (clusterStatus) {
042        case GREEN:
043        case YELLOW:
044            return ProbeStatus.newSuccess(clusterStatus.toString());
045        default:
046            return ProbeStatus.newFailure(clusterStatus.toString());
047        }
048    }
049
050    protected String[] getIndexNames() {
051        ElasticSearchAdmin esa = Framework.getService(ElasticSearchAdmin.class);
052        List<String> repositoryNames = Framework.getService(ElasticSearchAdmin.class).getRepositoryNames();
053        String indices[] = new String[repositoryNames.size()];
054        int i = 0;
055        for (String repo : repositoryNames) {
056            indices[i++] = esa.getIndexNameForRepository(repo);
057        }
058        return indices;
059    }
060
061}