Skip to content

Commit cc3a16c

Browse files
committed
<feature>support multi addr
1 parent 0229998 commit cc3a16c

File tree

1 file changed

+43
-34
lines changed

1 file changed

+43
-34
lines changed

search/search.go

+43-34
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"fmt"
99
"io/ioutil"
1010
"net/http"
11+
"strings"
1112

1213
"esalert/config"
1314
log "github.com/sirupsen/logrus"
@@ -101,7 +102,6 @@ func mapToDict(m map[interface{}]interface{}) (Dict, error) {
101102
// (see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-body.html)
102103
func Search(index, typ string, search interface{}) (Result, error) {
103104
//u := fmt.Sprintf("http://%s/%s/%s/_search", config.Opts.ElasticSearchAddr, index, typ)
104-
u := fmt.Sprintf("http://%s/%s/_search", config.Opts.ElasticSearchAddr, index)
105105
bodyReq, err := json.Marshal(search)
106106
if err != nil {
107107
return Result{}, err
@@ -111,50 +111,59 @@ func Search(index, typ string, search interface{}) (Result, error) {
111111
"body": string(bodyReq),
112112
}).Debugln("search query")
113113

114-
req, err := http.NewRequest(http.MethodPost, u, bytes.NewBuffer(bodyReq))
115-
if err != nil {
116-
return Result{}, err
117-
}
114+
addrs := strings.Split(config.Opts.ElasticSearchAddr, ",")
115+
for _, addr := range addrs {
116+
addr = strings.TrimSpace(addr)
117+
u := fmt.Sprintf("http://%s/%s/_search", addr, index)
118118

119-
if config.Opts.ElasticSearchUser != "" && config.Opts.ElasticSearchPass != "" {
120-
req.SetBasicAuth(config.Opts.ElasticSearchUser, config.Opts.ElasticSearchPass)
121-
}
122119

123-
req.Header.Set("Content-Type", "application/json")
120+
req, err := http.NewRequest(http.MethodPost, u, bytes.NewBuffer(bodyReq))
121+
if err != nil {
122+
return Result{}, err
123+
}
124124

125-
resp, err := http.DefaultClient.Do(req)
126-
if err != nil {
127-
return Result{}, err
128-
}
129-
defer resp.Body.Close()
125+
if config.Opts.ElasticSearchUser != "" && config.Opts.ElasticSearchPass != "" {
126+
req.SetBasicAuth(config.Opts.ElasticSearchUser, config.Opts.ElasticSearchPass)
127+
}
130128

131-
body, err := ioutil.ReadAll(resp.Body)
132-
if err != nil {
133-
return Result{}, err
134-
}
129+
req.Header.Set("Content-Type", "application/json")
135130

136-
log.WithFields(log.Fields{
137-
"body": string(body),
138-
}).Debugln("search results")
131+
resp, err := http.DefaultClient.Do(req)
132+
if err != nil {
133+
continue
134+
}
135+
defer resp.Body.Close()
139136

140-
if resp.StatusCode != 200 {
141-
var e elasticError
142-
if err := json.Unmarshal(body, &e); err != nil {
143-
log.Errorf("could not unmarshal error body, %v", err)
137+
body, err := ioutil.ReadAll(resp.Body)
138+
if err != nil {
144139
return Result{}, err
145140
}
146-
return Result{}, errors.New(fmt.Sprintf("HTTP status code: %v", resp.StatusCode))
147-
}
148141

149-
var result Result
150-
if err := json.Unmarshal(body, &result); err != nil {
151142
log.WithFields(log.Fields{
152143
"body": string(body),
153-
}).Errorf("could not unmarshal search result, %v", err)
154-
return result, err
155-
} else if result.TimedOut {
156-
return result, errors.New("search timed out in elasticsearch")
144+
}).Debugln("search results")
145+
146+
if resp.StatusCode != 200 {
147+
var e elasticError
148+
if err := json.Unmarshal(body, &e); err != nil {
149+
log.Errorf("could not unmarshal error body, %v", err)
150+
return Result{}, err
151+
}
152+
return Result{}, errors.New(fmt.Sprintf("HTTP status code: %v", resp.StatusCode))
153+
}
154+
155+
var result Result
156+
if err := json.Unmarshal(body, &result); err != nil {
157+
log.WithFields(log.Fields{
158+
"body": string(body),
159+
}).Errorf("could not unmarshal search result, %v", err)
160+
return result, err
161+
} else if result.TimedOut {
162+
return result, errors.New("search timed out in elasticsearch")
163+
}
164+
165+
return result, nil
157166
}
167+
return Result{}, errors.New(fmt.Sprintf("not valid hosts[%s]", addrs))
158168

159-
return result, nil
160169
}

0 commit comments

Comments
 (0)