ElasticSearch-集群健康度监控

简单的ES集群健康度监控  ES集群健康度分为:红 黄 绿三种颜色

红色:个别分片 副本不可用

黄色:个别副本不可用  

绿色:为健康

本监控方法为简单的定时调度,可以查看ES集群是否健康

properties属性文件如下:

#es监控配置
es.cluster=10.2.4.15,10.2.4.42,10.2.4.43
es.port=9300
es.name=es_cluster

监控Timer

import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.Timer;
import java.util.TimerTask;

import org.elasticsearch.action.admin.cluster.stats.ClusterStatsNodeResponse;
import org.elasticsearch.action.admin.cluster.stats.ClusterStatsResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.transport.TransportAddress;

/**
 * ElasticMonitor schedule
 */
public class ElasticMonitor {

    private static Timer timer = new Timer();
    private static TransportClient client;
    private static String es_cluster = "";
    private static String es_cluster_name = "";
    private static int es_port = 0;
    private static List<String> eshostlist = new ArrayList<String>();

    static {
        InputStream is = ElasticMonitor.class
            .getResourceAsStream("/monconfig.properties");
        Properties props = new Properties();
        try {
            props.load(is);
        } catch (IOException e) {
            e.printStackTrace();
        }
        // kafka broker地址
        es_cluster = props.getProperty("es.cluster");
        es_port = Integer.parseInt(props.getProperty("es.port"));
        es_cluster_name = props.getProperty("es.name");

        if (null == es_cluster || "".equals(es_cluster)) {
            System.out
                .println("es.cluster is not config at monconfig.properties");
            if (0 == es_port) {
                System.out
                    .println("es.port is not config at monconfig.properties");
                if (null == es_cluster_name || "".equals(es_cluster_name)) {
                    System.out
                        .println("cluster.name is not config at monconfig.properties");
                }
            }

        } else {

            // 截取初始化属性文件eshost
            String[] sourceStrArray = es_cluster.split(",");
            for (int i = 0; i < sourceStrArray.length; i++) {
                eshostlist.add(sourceStrArray[i]);
            }

            try {
                timer.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        System.out.println(start());
                    }
                }, 5000, 5000);
            } catch (Exception e) {

            }
        }
    }

    public static void main(String[] args) {
    }

    public static boolean start() {
        try {
            Settings settings = Settings.settingsBuilder()
                .put("cluster.name", es_cluster_name)
                .put("transport.tcp.compress", true).build();

            TransportAddress[] addressArr = new TransportAddress[eshostlist
                .size()];

            for (int i = 0; i < eshostlist.size(); i++) {
                try {
                    addressArr[i] = new InetSocketTransportAddress(
                        InetAddress.getByName(eshostlist.get(i)), es_port);
                } catch (UnknownHostException e) {
                    e.printStackTrace();
                }
            }

            client = TransportClient.builder().settings(settings).build().addTransportAddresses(addressArr);
            return checkEsClusterHealth();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    
    
    public static boolean checkEsClusterHealth(){
        //获取集群信息健康度 黄 红 报警  绿色不报警
        ClusterStatsResponse esStatus = client.admin().cluster().prepareClusterStats().execute().actionGet();
        esStatus.getNodes();
        ClusterStatsNodeResponse[] csnr = esStatus.getNodes();
        for(int i = 0 ;i<csnr.length;i++){
            System.out.println(csnr[i].nodeInfo());
        }
        String clusterStatus = String.valueOf(esStatus.getStatus());

        System.out.println("clusterStatus:"+clusterStatus);
        if("GREEN".equals(clusterStatus)){
            client.close();
            return true;
        }else{
            client.close();
            return false;
        }
    }

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值