8
8
"fmt"
9
9
"io/ioutil"
10
10
"net/http"
11
+ "strings"
11
12
12
13
"esalert/config"
13
14
log "github.com/sirupsen/logrus"
@@ -101,7 +102,6 @@ func mapToDict(m map[interface{}]interface{}) (Dict, error) {
101
102
// (see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-body.html)
102
103
func Search (index , typ string , search interface {}) (Result , error ) {
103
104
//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 )
105
105
bodyReq , err := json .Marshal (search )
106
106
if err != nil {
107
107
return Result {}, err
@@ -111,50 +111,59 @@ func Search(index, typ string, search interface{}) (Result, error) {
111
111
"body" : string (bodyReq ),
112
112
}).Debugln ("search query" )
113
113
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 )
118
118
119
- if config .Opts .ElasticSearchUser != "" && config .Opts .ElasticSearchPass != "" {
120
- req .SetBasicAuth (config .Opts .ElasticSearchUser , config .Opts .ElasticSearchPass )
121
- }
122
119
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
+ }
124
124
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
+ }
130
128
131
- body , err := ioutil .ReadAll (resp .Body )
132
- if err != nil {
133
- return Result {}, err
134
- }
129
+ req .Header .Set ("Content-Type" , "application/json" )
135
130
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 ()
139
136
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 {
144
139
return Result {}, err
145
140
}
146
- return Result {}, errors .New (fmt .Sprintf ("HTTP status code: %v" , resp .StatusCode ))
147
- }
148
141
149
- var result Result
150
- if err := json .Unmarshal (body , & result ); err != nil {
151
142
log .WithFields (log.Fields {
152
143
"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
157
166
}
167
+ return Result {}, errors .New (fmt .Sprintf ("not valid hosts[%s]" , addrs ))
158
168
159
- return result , nil
160
169
}
0 commit comments