sunty
2019-10-30 a1ef2d42d320f075c75e8cef52a879114441c103
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"
@@ -33,40 +37,28 @@
      util.ResponseFormat(c, code.RequestParamError, "参数有误")
      return
   }
   databases := searchBody.DataBases
   from := 0
   to := 0
   data := make(map[string]interface{})
   searchBody.Page = 1
   searchBody.Size = 15000
   if len(databases) == 1 && databases[0] == "esData" {
      searchBody.IsAggs = false
   } else {
      data = service.QueryDbPersonsForCompare(*searchBody)
   if searchBody.CompareNum !="" {
      //二次搜索,不需要再比对了
      co := service.GetCompResultByNum(searchBody.CompareNum)
      if co != nil {
         //二次搜索和排序
         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"] = []CompareResult{}
         util.ResponseFormat(c,code.CompareResultGone,m)
         return
      }
   }
   sources := data["datalist"].([]interface{})
   if len(sources) > 0 {
      //进行比对
   } else {
      fmt.Println("查询条件下无数据 source 数据为空:" + string(len(sources)))
   }
   dataLen := len(sources)
   if from > dataLen-1 {
      from = 0
      to = 0
   }
   if to > dataLen {
      to = dataLen
   }
   dmap := make(map[string]interface{}, 2)
   dmap["datalist"] = sources
   dmap["total"] = dataLen
   util.ResponseFormat(c, code.Success, dmap)
   m :=make(map[string]interface{},0)
   m["compareNum"] = searchBody.CompareNum
   m["total"] = 0
   m["totalList"] = []CompareResult{}
   util.ResponseFormat(c,code.CompareResultGone,m)
}
func searchEsData(searchBody models.EsSearch) map[string]interface{} {
@@ -115,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 + "\"," +
@@ -161,3 +156,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
}