forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
task.go
242 lines (208 loc) · 5.38 KB
/
task.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package http
import (
"bytes"
"context"
"fmt"
"io/ioutil"
"net"
"net/http"
"strconv"
"strings"
"time"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/outputs/transport"
"github.com/elastic/beats/heartbeat/look"
"github.com/elastic/beats/heartbeat/monitors"
"github.com/elastic/beats/heartbeat/monitors/active/dialchain"
"github.com/elastic/beats/heartbeat/reason"
)
func newHTTPMonitorHostJob(
addr string,
config *Config,
transport *http.Transport,
enc contentEncoder,
body []byte,
validator RespCheck,
) (monitors.Job, error) {
typ := config.Name
jobName := fmt.Sprintf("%v@%v", typ, addr)
client := &http.Client{
CheckRedirect: makeCheckRedirect(config.MaxRedirects),
Transport: transport,
Timeout: config.Timeout,
}
request, err := buildRequest(addr, config, enc)
if err != nil {
return nil, err
}
hostname, port, err := splitHostnamePort(request)
if err != nil {
return nil, err
}
timeout := config.Timeout
fields := common.MapStr{
"scheme": request.URL.Scheme,
"host": hostname,
"port": port,
"url": request.URL.String(),
}
return monitors.MakeSimpleJob(jobName, typ, func() (common.MapStr, error) {
event, err := execPing(client, request, body, timeout, validator)
if event == nil {
event = common.MapStr{}
}
event.Update(fields)
return event, err
}), nil
}
func newHTTPMonitorIPsJob(
config *Config,
addr string,
tls *transport.TLSConfig,
enc contentEncoder,
body []byte,
validator RespCheck,
) (monitors.Job, error) {
typ := config.Name
jobName := fmt.Sprintf("%v@%v", typ, addr)
req, err := buildRequest(addr, config, enc)
if err != nil {
return nil, err
}
hostname, port, err := splitHostnamePort(req)
if err != nil {
return nil, err
}
pingFactory := createPingFactory(config, hostname, port, tls, req, body, validator)
if ip := net.ParseIP(hostname); ip != nil {
return monitors.MakeByIPJob(jobName, typ, ip, pingFactory)
}
return monitors.MakeByHostJob(jobName, typ, hostname, config.Mode, pingFactory)
}
func createPingFactory(
config *Config,
hostname string,
port uint16,
tls *transport.TLSConfig,
request *http.Request,
body []byte,
validator RespCheck,
) func(*net.IPAddr) monitors.TaskRunner {
fields := common.MapStr{
"scheme": request.URL.Scheme,
"port": port,
"url": request.URL.String(),
}
timeout := config.Timeout
isTLS := request.URL.Scheme == "https"
checkRedirect := makeCheckRedirect(config.MaxRedirects)
return monitors.MakePingIPFactory(fields, func(ip *net.IPAddr) (common.MapStr, error) {
addr := net.JoinHostPort(ip.String(), strconv.Itoa(int(port)))
d := &dialchain.DialerChain{
Net: dialchain.ConstAddrDialer("tcp_connect_rtt", addr, timeout),
}
if isTLS {
d.AddLayer(dialchain.TLSLayer("tls_handshake_rtt", tls, timeout))
}
measures := common.MapStr{}
dialer, err := d.BuildWithMeasures(measures)
if err != nil {
return nil, err
}
var httpStart, httpEnd time.Time
client := &http.Client{
CheckRedirect: checkRedirect,
Timeout: timeout,
Transport: &SimpleTransport{
Dialer: dialer,
OnStartWrite: func() { httpStart = time.Now() },
OnStartRead: func() { httpEnd = time.Now() },
},
}
event, err := execPing(client, request, body, timeout, validator)
if event == nil {
event = measures
} else {
event.Update(measures)
}
if !httpEnd.IsZero() {
event["http_rtt"] = look.RTT(httpEnd.Sub(httpStart))
}
return event, err
})
}
func buildRequest(addr string, config *Config, enc contentEncoder) (*http.Request, error) {
method := strings.ToUpper(config.Check.Request.Method)
request, err := http.NewRequest(method, addr, nil)
if err != nil {
return nil, err
}
request.Close = true
if config.Username != "" {
request.SetBasicAuth(config.Username, config.Password)
}
for k, v := range config.Check.Request.SendHeaders {
request.Header.Add(k, v)
}
if enc != nil {
enc.AddHeaders(&request.Header)
}
return request, nil
}
func execPing(
client *http.Client,
req *http.Request,
body []byte,
timeout time.Duration,
validator func(*http.Response) error,
) (common.MapStr, reason.Reason) {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
req = req.WithContext(ctx)
if len(body) > 0 {
req.Body = ioutil.NopCloser(bytes.NewBuffer(body))
req.ContentLength = int64(len(body))
}
start := time.Now()
resp, err := client.Do(req)
end := time.Now()
if err != nil {
return nil, reason.IOFailed(err)
}
defer resp.Body.Close()
if err := validator(resp); err != nil {
return nil, reason.ValidateFailed(err)
}
rtt := end.Sub(start)
event := common.MapStr{
"response": common.MapStr{
"status": resp.StatusCode,
},
"rtt": look.RTT(rtt),
}
return event, nil
}
func splitHostnamePort(requ *http.Request) (string, uint16, error) {
host, port, err := net.SplitHostPort(requ.URL.Host)
if err != nil {
return "", 0, err
}
p, err := strconv.ParseUint(port, 10, 16)
if err != nil {
return "", 0, fmt.Errorf("'%v' is no valid port number in '%v'", port, requ.URL.Host)
}
return host, uint16(p), nil
}
func makeCheckRedirect(max int) func(*http.Request, []*http.Request) error {
if max == 0 {
return func(_ *http.Request, _ []*http.Request) error {
return http.ErrUseLastResponse
}
}
return func(_ *http.Request, via []*http.Request) error {
if max == len(via) {
return http.ErrUseLastResponse
}
return nil
}
}