ElasticSearch-叢集健康度監控
阿新 • • 發佈:2019-02-04
簡單的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; } } }