From b50ab8c04f6ae6097701a2a2b0f3cd9226ad77e3 Mon Sep 17 00:00:00 2001 From: zhangzengfei <zhangzengfei@iotlink.com> Date: 星期三, 30 十月 2019 15:19:23 +0800 Subject: [PATCH] fix: change network settings --- controllers/es.go | 76 ++++++++++++++++++++++++++++++++++--- 1 files changed, 69 insertions(+), 7 deletions(-) diff --git a/controllers/es.go b/controllers/es.go index 46cb2f5..93d686f 100644 --- a/controllers/es.go +++ b/controllers/es.go @@ -1,12 +1,16 @@ package controllers import ( - "basic.com/dbapi.git" + "basic.com/valib/logger.git" + "bytes" + "encoding/json" "fmt" "github.com/gin-gonic/gin" "math/rand" + "os/exec" "strconv" "strings" + "webserver/cache" "webserver/extend/code" "webserver/extend/config" "webserver/extend/esutil" @@ -40,18 +44,20 @@ //浜屾鎼滅储鍜屾帓搴� twiceM := GetCompareDataTwice(co,searchBody) util.ResponseFormat(c,code.Success,twiceM) + return } else { m :=make(map[string]interface{},0) m["compareNum"] = searchBody.CompareNum m["total"] = 0 - m["totalList"] = []service.CompareResult{} + m["totalList"] = []CompareResult{} util.ResponseFormat(c,code.CompareResultGone,m) + return } } m :=make(map[string]interface{},0) m["compareNum"] = searchBody.CompareNum m["total"] = 0 - m["totalList"] = []service.CompareResult{} + m["totalList"] = []CompareResult{} util.ResponseFormat(c,code.CompareResultGone,m) } @@ -101,12 +107,15 @@ esSize := strconv.Itoa(webSize) //浣跨敤es搴曞眰鏈哄埗澶勭悊鍒嗛〉 //璇锋眰澶� - url := "http://" + config.EsInfo.Masterip + ":" + config.EsInfo.Httpport + + localConf, err2 := cache.GetServerInfo() + if err2 !=nil || localConf.AlarmIp == "" || localConf.ServerId == "" { + logger.Debug("localConfig is wrong!!!") + return nil + } + url := "http://" + localConf.AlarmIp + ":" + strconv.Itoa(int(localConf.AlarmPort)) + "/" + index + "/_search?search_type=dfs_query_then_fetch" - var setApi dbapi.SysSetApi - _, sysconf := setApi.GetServerInfo() - analyServerFilterStr := "{\"term\":{\"analyServerId\":\"" + sysconf.ServerId + "\"}}," + analyServerFilterStr := "{\"term\":{\"analyServerId\":\"" + localConf.ServerId + "\"}}," //璇锋眰浣� prama := "{\"from\":\"" + esFrom + "\",\"size\":\"" + esSize + "\"," + @@ -147,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