From aa6a107abc0e321f2adb454116286c66e893ebff Mon Sep 17 00:00:00 2001
From: sunty <1172534965@qq.com>
Date: 星期二, 29 十月 2019 20:45:21 +0800
Subject: [PATCH] add es cluster manger

---
 controllers/es.go |   56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 56 insertions(+), 0 deletions(-)

diff --git a/controllers/es.go b/controllers/es.go
index d566366..93d686f 100644
--- a/controllers/es.go
+++ b/controllers/es.go
@@ -2,9 +2,12 @@
 
 import (
 	"basic.com/valib/logger.git"
+	"bytes"
+	"encoding/json"
 	"fmt"
 	"github.com/gin-gonic/gin"
 	"math/rand"
+	"os/exec"
 	"strconv"
 	"strings"
 	"webserver/cache"
@@ -153,3 +156,56 @@
 	}
 	return dataSource
 }
+
+func GetEsClusterInfo(ip string) ([]map[string]interface{}, error){
+	serverIp := ""
+	if ip != "" {
+		serverIp = ip
+	} else {
+		localConf, err2 := cache.GetServerInfo()
+		if err2 !=nil || localConf.ServerIp == "" {
+			logger.Debug("localConfig is wrong!!!")
+			return nil,err2
+		}
+		serverIp = localConf.ServerIp
+	}
+	str := "curl "+serverIp+":9200/_cat/nodes?v"
+	cmd := exec.Command("sh","-c",str)
+	var out bytes.Buffer
+	cmd.Stdout = &out
+	err := cmd.Run()
+	if err != nil {
+		return nil,err
+	}
+	infos := strings.Split(string(out.String()),"\n")
+	totalNodes := len(infos)-1
+	var nodeInfos []map[string]interface{}
+	for i:=1;i<totalNodes ;i++  {
+		nodeInfo :=  make(map[string]interface{})
+		context := strings.Fields(infos[i])
+		nodeIp := context[0]
+		Type := context[8]
+		var nodeType string
+		if Type == "*"{
+			nodeType = "涓昏妭鐐�"
+		}
+		if Type == "-"{
+			nodeType = "浠庤妭鐐�"
+		}
+		nodeInfo["ip"] = nodeIp
+		nodeInfo["nodeType"] = nodeType
+		url := "http://"+nodeIp+":9200"
+		buf := esutil.httpGet(url)
+		var info interface{}
+		json.Unmarshal(buf,&info)
+		tmpInfo := info.(map[string]interface{})
+		tmpName := tmpInfo["name"].(string)
+		versinInfo := tmpInfo["version"].(map[string]interface{})
+		buildDate := versinInfo["build_date"].(string)
+		nodeInfo["name"] = tmpName
+		nodeInfo["buildDate"] = buildDate
+		nodeInfos = append(nodeInfos, nodeInfo)
+	}
+
+	return nodeInfos,nil
+}

--
Gitblit v1.8.0