From b01bab835d32a0c51076f81269df8c9cb77bf757 Mon Sep 17 00:00:00 2001 From: sunty <1172534965@qq.com> Date: 星期五, 01 十一月 2019 13:43:04 +0800 Subject: [PATCH] add es cluster manger --- extend/code/code.go | 33 +++--- controllers/es.go | 256 ++++++++++++++++++++++++++++++++++++++++---------- router/router.go | 6 + 3 files changed, 225 insertions(+), 70 deletions(-) diff --git a/controllers/es.go b/controllers/es.go index d67a80b..412d64f 100644 --- a/controllers/es.go +++ b/controllers/es.go @@ -21,6 +21,12 @@ type EsController struct{} +type EsManagementController struct{} + +type EsClusterInfo struct { + Ip string `json:"ip"` +} + // @Summary 姣斿鏁版嵁鏌ヨ // @Description 姣斿鏁版嵁鏌ヨ // @Accept json @@ -37,28 +43,28 @@ util.ResponseFormat(c, code.RequestParamError, "鍙傛暟鏈夎") return } - if searchBody.CompareNum !="" { + if searchBody.CompareNum != "" { //浜屾鎼滅储锛屼笉闇�瑕佸啀姣斿浜� co := service.GetCompResultByNum(searchBody.CompareNum) if co != nil { //浜屾鎼滅储鍜屾帓搴� - twiceM := GetCompareDataTwice(co,searchBody) - util.ResponseFormat(c,code.Success,twiceM) + twiceM := GetCompareDataTwice(co, searchBody) + util.ResponseFormat(c, code.Success, twiceM) return } else { - m :=make(map[string]interface{},0) + m := make(map[string]interface{}, 0) m["compareNum"] = searchBody.CompareNum m["total"] = 0 m["totalList"] = []CompareResult{} - util.ResponseFormat(c,code.CompareResultGone,m) + util.ResponseFormat(c, code.CompareResultGone, m) return } } - m :=make(map[string]interface{},0) + m := make(map[string]interface{}, 0) m["compareNum"] = searchBody.CompareNum m["total"] = 0 m["totalList"] = []CompareResult{} - util.ResponseFormat(c,code.CompareResultGone,m) + util.ResponseFormat(c, code.CompareResultGone, m) } func searchEsData(searchBody models.EsSearch) map[string]interface{} { @@ -108,7 +114,7 @@ //浣跨敤es搴曞眰鏈哄埗澶勭悊鍒嗛〉 //璇锋眰澶� localConf, err2 := cache.GetServerInfo() - if err2 !=nil || localConf.AlarmIp == "" || localConf.ServerId == "" { + if err2 != nil || localConf.AlarmIp == "" || localConf.ServerId == "" { logger.Debug("localConfig is wrong!!!") return nil } @@ -157,47 +163,62 @@ return dataSource } -func GetEsClusterInfo(ip string) ([]map[string]interface{}, error){ +//鏌ヨES闆嗙兢淇℃伅-鍏ュ彛 +func (em *EsManagementController) GetEsClusterInfo(c *gin.Context) { + var body EsClusterInfo + c.BindJSON(&body) + ip := body.Ip serverIp := "" if ip != "" { serverIp = ip } else { localConf, err2 := cache.GetServerInfo() - if err2 !=nil || localConf.ServerIp == "" { + if err2 != nil || localConf.ServerIp == "" { logger.Debug("localConfig is wrong!!!") - return nil,err2 + util.ResponseFormat(c, code.QueryClusterInfoErr, err2) + return } serverIp = localConf.ServerIp } - str := "curl "+serverIp+":9200/_cat/nodes?v" - cmd := exec.Command("sh","-c",str) + nodeInfos, err := getEsClusterInfors(serverIp) + if err != nil { + util.ResponseFormat(c, code.QueryClusterInfoErr, err) + return + } + util.ResponseFormat(c, code.QueryClusterInfoErr, nodeInfos) +} + +//鏌ヨES闆嗙兢淇℃伅-涓氬姟閫昏緫 +func getEsClusterInfors(ip string) ([]map[string]interface{}, error) { + str := "curl " + ip + ":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 + return nil, err } - infos := strings.Split(string(out.String()),"\n") - totalNodes := len(infos)-1 + 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{}) + 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 == "*"{ + if Type == "*" { nodeType = "涓昏妭鐐�" } - if Type == "-"{ + if Type == "-" { nodeType = "浠庤妭鐐�" } nodeInfo["ip"] = nodeIp nodeInfo["nodeType"] = nodeType - url := "http://"+nodeIp+":9200" + url := "http://" + nodeIp + ":9200" buf := esutil.HttpGet(url) var info interface{} - json.Unmarshal(buf,&info) + json.Unmarshal(buf, &info) tmpInfo := info.(map[string]interface{}) tmpName := tmpInfo["name"].(string) versinInfo := tmpInfo["version"].(map[string]interface{}) @@ -206,61 +227,188 @@ nodeInfo["buildDate"] = buildDate nodeInfos = append(nodeInfos, nodeInfo) } - - return nodeInfos,nil + return nodeInfos, err } -func AddEsCluster(hosts []string) (string){ - msg := "鍔犲叆澶辫触" - for i,val := range hosts{ - val =val+":9300" +//鍒涘缓鑺傜偣 +func (em *EsManagementController) CreateNode(c *gin.Context) { + msg := "鍒涘缓鑺傜偣澶辫触锛岃鑱旂郴绠$悊鍛�" + str := "sh /opt/script/create_first_node.sh" + 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") + len := len(infos) + res := infos[len-1] + if res == "鏈嶅姟鍚姩鎴愬姛" { + msg = "鍒涘缓鑺傜偣鎴愬姛" + util.ResponseFormat(c, code.Success, msg) + return + } + util.ResponseFormat(c, code.CreateFirstNodeErr, msg) +} + +//鍔犲叆闆嗙兢 +func (em *EsManagementController) AddCluster(c *gin.Context) { + var ac AddCluster + err := c.BindJSON(&ac) + if err != nil { + util.ResponseFormat(c, code.RequestParamError, "鍙傛暟鏈夎") + return + } + str := "sh /opt/script/create_node.sh " + ac.Option + "" + if ac.Option == "1" { + info, err := updateUnicastHosts(ac.Ip) + if err != nil || info == false { + util.ResponseFormat(c, code.QueryClusterInfoErr, err) + return + } + if info == true { + info := runScript(str) + if info == "杩愯澶辫触" { + util.ResponseFormat(c, code.AddClusterInfoErr, info) + return + } + } + } + if ac.Option == "2" { + info, err := updateUnicastHosts(ac.Ip) + if err != nil || info == false { + util.ResponseFormat(c, code.QueryClusterInfoErr, err) + return + } + if info == true { + info := runScript(str) + if info == "杩愯澶辫触" { + util.ResponseFormat(c, code.AddClusterInfoErr, info) + return + } + } + } else { + util.ResponseFormat(c, code.RequestParamError, "鍙傛暟鏈夎") + return + } + util.ResponseFormat(c, code.Success, "鍔犲叆鎴愬姛") +} + +//鑴氭湰灏佽 +func runScript(str string) string { + + cmd := exec.Command("sh", "-c", str) + var out bytes.Buffer + cmd.Stdout = &out + err1 := cmd.Run() + if err1 != nil { + return "杩愯澶辫触" + } + return out.String() +} + +type AddCluster struct { + Ip string `json:"ip"` //闆嗙兢IP + Option string `json:option` //鑺傜偣绫诲瀷 +} + +//鏇存柊缁勬挱鍒楄〃 +func updateUnicastHosts(host string) (bool, error) { + nodeInfos, err := getEsClusterInfors(host) + if err != nil { + return false, err + } + var hosts []string + for _, val := range nodeInfos { + nodeType := val["nodeType"].(string) + if nodeType == "涓昏妭鐐�" { + ip := val["ip"].(string) + hosts = append(hosts, ip) + } + } + msg := false + 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+"\\\"" + 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" + 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) + cmd := exec.Command("sh", "-c", str) var out bytes.Buffer cmd.Stdout = &out - err := cmd.Run() - if err != nil { - + err1 := cmd.Run() + if err1 != nil { + return false, err } res := getUnicastHosts() - fmt.Println("res: ",res) - res1 := "discovery.zen.ping.unicast.hosts: "+verificationHosts+"" - fmt.Println("res1: ",res1) - if res == res1{ - msg = "鍔犲叆鎴愬姛" + fmt.Println("res: ", res) + res1 := "discovery.zen.ping.unicast.hosts: " + verificationHosts + "" + fmt.Println("res1: ", res1) + if res == res1 { + msg = true } - 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 - + return msg, err } -func getUnicastHosts() (string){ +//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) + 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] + infos := strings.Split(string(out.String()), "\n")[0] return infos } diff --git a/extend/code/code.go b/extend/code/code.go index 7027f13..44ec884 100644 --- a/extend/code/code.go +++ b/extend/code/code.go @@ -11,11 +11,11 @@ var ( // Success 璇锋眰澶勭悊鎴愬姛 - Success = &Code{http.StatusOK, true, "璇锋眰澶勭悊鎴愬姛"} - AddSuccess = &Code{http.StatusOK,true,"娣诲姞鎴愬姛"} - UpdateSuccess = &Code{http.StatusOK,true,"鏇存柊鎴愬姛"} - UpdateFail = &Code{http.StatusBadRequest,false,"鏇存柊澶辫触"} - DelSuccess = &Code{http.StatusOK,true,"鍒犻櫎鎴愬姛"} + Success = &Code{http.StatusOK, true, "璇锋眰澶勭悊鎴愬姛"} + AddSuccess = &Code{http.StatusOK, true, "娣诲姞鎴愬姛"} + UpdateSuccess = &Code{http.StatusOK, true, "鏇存柊鎴愬姛"} + UpdateFail = &Code{http.StatusBadRequest, false, "鏇存柊澶辫触"} + DelSuccess = &Code{http.StatusOK, true, "鍒犻櫎鎴愬姛"} // RequestParamError 璇锋眰鍙傛暟閿欒 RequestParamError = &Code{http.StatusBadRequest, false, "璇锋眰鍙傛暟鏈夎"} // AccountPassUnmatch 璇ヨ处鍙峰師瀵嗙爜涓嶅尮閰� @@ -28,10 +28,10 @@ UploadSuffixError = &Code{http.StatusBadRequest, false, "璇ヤ笂浼犳枃浠舵牸寮忕洰鍓嶆殏涓嶆敮鎸�"} // UploadSizeLimit 鐩墠涓婁紶浠呮敮鎸佸皬浜�5M鐨勬枃浠跺唴瀹� UploadSizeLimit = &Code{http.StatusBadRequest, false, "鐩墠涓婁紶浠呮敮鎸佸皬浜�5M鐨勬枃浠跺唴瀹�"} - LoginSuccess = &Code{http.StatusOK,true,"鐧诲綍鎴愬姛"} + LoginSuccess = &Code{http.StatusOK, true, "鐧诲綍鎴愬姛"} // SigninInfoError 璐︽埛鍚嶆垨瀵嗙爜鏈夎 LoginInfoError = &Code{http.StatusUnauthorized, false, "鐢ㄦ埛鍚嶆垨瀵嗙爜閿欒"} - LogoutSuccess = &Code{http.StatusOK,true,"閫�鍑烘垚鍔�"} + LogoutSuccess = &Code{http.StatusOK, true, "閫�鍑烘垚鍔�"} // TokenNotFound 璇锋眰鏈惡甯oken, 鏃犳潈璁块棶 TokenNotFound = &Code{http.StatusUnauthorized, false, "璇锋眰鏈惡甯oken, 鏃犳潈璁块棶"} // TokenInvalid 鏃犳晥鐨凾oken淇℃伅 @@ -49,16 +49,19 @@ //鍒犻櫎鑺傜偣澶辫触 HasChildNodeError = &Code{http.StatusInternalServerError, false, "鏁版嵁鑺傜偣瀛樺湪瀛愯妭鐐� 鏃犳硶鍒犻櫎"} - NotLogin = &Code{ http.StatusUnauthorized, false, "鐧诲綍澶辨晥锛岃閲嶆柊鐧诲綍"} - DbPersonUploadSuccess = &Code{ http.StatusOK, true, "浜哄憳涓婁紶鎴愬姛"} - DbPersonUploadFail = &Code{ http.StatusInternalServerError, false, "浜哄憳涓婁紶澶辫触"} - DbPersonUpdateSuccess = &Code{ http.StatusOK, true, "浜哄憳鏇存柊鎴愬姛"} + NotLogin = &Code{http.StatusUnauthorized, false, "鐧诲綍澶辨晥锛岃閲嶆柊鐧诲綍"} + DbPersonUploadSuccess = &Code{http.StatusOK, true, "浜哄憳涓婁紶鎴愬姛"} + DbPersonUploadFail = &Code{http.StatusInternalServerError, false, "浜哄憳涓婁紶澶辫触"} + DbPersonUpdateSuccess = &Code{http.StatusOK, true, "浜哄憳鏇存柊鎴愬姛"} - CompareResultGone = &Code{ http.StatusOK,true,"涓婃姣斿宸插け鏁堬紝璇蜂粠鏂版瘮瀵�"} + CompareResultGone = &Code{http.StatusOK, true, "涓婃姣斿宸插け鏁堬紝璇蜂粠鏂版瘮瀵�"} TaskStoped = &Code{http.StatusOK, false, "姝や换鍔′负鍋滅敤鐘舵�侊紝璇峰湪浠诲姟绠$悊涓紑鍚紒"} - ComError = &Code{http.StatusInternalServerError, false, ""} - ClusterNodesEmpty = &Code{http.StatusOK, true, "闆嗙兢鑺傜偣涓虹┖"} - AddTaskErr = &Code{http.StatusInternalServerError, false, "姝ゅ浗鏍囨憚鍍忔満宸插湪鍏跺畠鏈嶅姟鍣ㄩ厤缃换鍔★紒"} + ComError = &Code{http.StatusInternalServerError, false, ""} + ClusterNodesEmpty = &Code{http.StatusOK, true, "闆嗙兢鑺傜偣涓虹┖"} + AddTaskErr = &Code{http.StatusInternalServerError, false, "姝ゅ浗鏍囨憚鍍忔満宸插湪鍏跺畠鏈嶅姟鍣ㄩ厤缃换鍔★紒"} + CreateFirstNodeErr = &Code{http.StatusInternalServerError, false, "鍒涘缓鑺傜偣澶辫触锛�"} + QueryClusterInfoErr = &Code{http.StatusInternalServerError, false, "鏌ヨ澶辫触锛岃纭鎮ㄧ殑ip鏄纭殑锛�"} + AddClusterInfoErr = &Code{http.StatusInternalServerError, false, "鍔犲叆鑺傜偣澶辫触锛�"} ) diff --git a/router/router.go b/router/router.go index abc6ffe..5234b24 100644 --- a/router/router.go +++ b/router/router.go @@ -29,6 +29,7 @@ ssController := new(controllers.SysSetController) sdkController := new(controllers.SdkController) esSearchController := new(controllers.EsSearchController) + esManagementController := new(controllers.EsManagementController) realTimeController := new(controllers.RealTimeController) cameraTimeRuleController := new(controllers.CameraTimeruleController) polygonController := new(controllers.CameraPolygonController) @@ -145,13 +146,16 @@ task.POST("/saveTaskSdkRule", taskController.SaveTaskSdkRule) } - // 妫�绱� 鏌ヨ + // 妫�绱� 鏌ヨ 鑺傜偣鎿嶄綔 es := r.Group(urlPrefix + "/es") { es.POST("/tagList", esSearchController.PostEsTagList) es.POST("/taskList", esSearchController.PostEsTaskList) es.POST("/esSearch", esSearchController.PostEsSearch) es.POST("/queryEsCompareData", controllers.PostEsCompareData) + es.POST("/getEsClusterInfo", esManagementController.GetEsClusterInfo) + es.POST("/addCluster", esManagementController.AddCluster) + es.POST("/createNode", esManagementController.CreateNode) } //瀹炴椂琚皟鏁版嵁 -- Gitblit v1.8.0