From 2bd2068c999cda5bda8c0787ed0dcaac6cb7afdb Mon Sep 17 00:00:00 2001 From: zhangzengfei <zhangzengfei@iotlink.com> Date: 星期四, 31 十月 2019 13:27:46 +0800 Subject: [PATCH] feat: add system reboot api --- controllers/es.go | 112 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 112 insertions(+), 0 deletions(-) diff --git a/controllers/es.go b/controllers/es.go index d566366..dd53714 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" @@ -18,6 +21,7 @@ type EsController struct{} +// @Security ApiKeyAuth // @Summary 姣斿鏁版嵁鏌ヨ // @Description 姣斿鏁版嵁鏌ヨ // @Accept json @@ -153,3 +157,111 @@ } 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 +} + +func AddEsCluster(hosts []string) (string){ + msg := "鍔犲叆澶辫触" + for i,val := range hosts{ + val =val+":9300" + hosts[i] = val + } + verificationHosts := "[\""+strings.Replace(strings.Trim(fmt.Sprint(hosts), "[]"), " ", "\",\"", -1)+"\"]" + for i,val := range hosts{ + val ="\\\""+val+"\\\"" + hosts[i] = val + } + oldUnicastHost := "\\[\\\"0.0.0.0:9300\\\"\\]" + newUnicastHost := strings.Replace(strings.Trim(fmt.Sprint(hosts), ""), " ", ",", -1) + str := "sed -ie 's/discovery.zen.ping.unicast.hosts: "+oldUnicastHost+"/discovery.zen.ping.unicast.hosts: "+newUnicastHost+"/g' /opt/elasticsearch/config/elasticsearch.yml" + fmt.Println(str) + cmd := exec.Command("sh","-c",str) + var out bytes.Buffer + cmd.Stdout = &out + err := cmd.Run() + if err != nil { + + } + res := getUnicastHosts() + fmt.Println("res: ",res) + res1 := "discovery.zen.ping.unicast.hosts: "+verificationHosts+"" + fmt.Println("res1: ",res1) + if res == res1{ + msg = "鍔犲叆鎴愬姛" + } + str2 := "echo \"node.master: true\" >> /opt/elasticsearch/config/elasticsearch.yml" + cmd2 := exec.Command("sh","-c",str2) + var out2 bytes.Buffer + cmd2.Stdout = &out2 + err2 := cmd2.Run() + if err2 != nil { + msg = "鍔犲叆澶辫触" + } + return msg + +} + +func getUnicastHosts() (string){ + str := "cat /opt/elasticsearch/config/elasticsearch.yml | grep discovery.zen.ping.unicast.hosts:" + cmd := exec.Command("sh","-c",str) + var out bytes.Buffer + cmd.Stdout = &out + err := cmd.Run() + if err != nil { + + } + infos := strings.Split(string(out.String()),"\n")[0] + return infos + +} -- Gitblit v1.8.0