forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
task.go
210 lines (182 loc) · 5.14 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
package tcp
import (
"fmt"
"net"
"strconv"
"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 newTCPMonitorHostJob(
scheme, host string, port uint16,
tls *transport.TLSConfig,
config *Config,
) (monitors.Job, error) {
typ := config.Name
timeout := config.Timeout
jobName := jobName(typ, jobType(scheme), host, []uint16{port})
validator := makeValidateConn(config)
pingAddr := net.JoinHostPort(host, strconv.Itoa(int(port)))
taskDialer, err := buildDialerChain(scheme, tls, config)
if err != nil {
return nil, err
}
return monitors.MakeSimpleJob(jobName, typ, func() (common.MapStr, error) {
event := common.MapStr{
"scheme": scheme,
"port": port,
"host": host,
}
dialer, err := taskDialer.BuildWithMeasures(event)
if err != nil {
return event, err
}
results, err := pingHost(dialer, pingAddr, timeout, validator)
event.Update(results)
return event, err
}), nil
}
func newTCPMonitorIPsJob(
addr connURL,
tls *transport.TLSConfig,
config *Config,
) (monitors.Job, error) {
typ := config.Name
timeout := config.Timeout
jobType := jobType(addr.Scheme)
jobName := jobName(typ, jobType, addr.Host, addr.Ports)
validator := makeValidateConn(config)
dialerFactory, err := buildHostDialerChainFactory(addr.Scheme, tls, config)
if err != nil {
return nil, err
}
pingFactory := createPingFactory(dialerFactory, addr, timeout, validator)
if ip := net.ParseIP(addr.Host); ip != nil {
debugf("Make TCP by IP job: %v:%v", ip, addr.Ports)
return monitors.MakeByIPJob(jobName, typ, ip, pingFactory)
}
debugf("Make TCP by Host job: %v:%v (mode=%#v)", addr.Host, addr.Ports, config.Mode)
return monitors.MakeByHostJob(jobName, typ, addr.Host, config.Mode, pingFactory)
}
func createPingFactory(
makeDialerChain func(string) *dialchain.DialerChain,
addr connURL,
timeout time.Duration,
validator ConnCheck,
) func(*net.IPAddr) monitors.TaskRunner {
fields := common.MapStr{"scheme": addr.Scheme}
return monitors.MakePingAllIPPortFactory(fields, addr.Ports,
func(ip *net.IPAddr, port uint16) (common.MapStr, error) {
host := net.JoinHostPort(ip.String(), strconv.Itoa(int(port)))
pingAddr := net.JoinHostPort(addr.Host, strconv.Itoa(int(port)))
event := common.MapStr{}
dialer, err := makeDialerChain(host).BuildWithMeasures(event)
if err != nil {
return event, err
}
results, err := pingHost(dialer, pingAddr, timeout, validator)
event.Update(results)
return event, err
})
}
func pingHost(
dialer transport.Dialer,
host string,
timeout time.Duration,
validator ConnCheck,
) (common.MapStr, reason.Reason) {
start := time.Now()
deadline := start.Add(timeout)
conn, err := dialer.Dial("tcp", host)
if err != nil {
debugf("dial failed with: %v", err)
return nil, reason.IOFailed(err)
}
defer conn.Close()
if validator == nil {
// no additional validation step => ping success
return common.MapStr{}, nil
}
if err := conn.SetDeadline(deadline); err != nil {
debugf("setting connection deadline failed with: %v", err)
return nil, reason.IOFailed(err)
}
validateStart := time.Now()
err = validator.Validate(conn)
if err != nil && err != errRecvMismatch {
debugf("check failed with: %v", err)
return nil, reason.IOFailed(err)
}
end := time.Now()
event := common.MapStr{
"validate_rtt": look.RTT(end.Sub(validateStart)),
}
if err != nil {
event["error"] = reason.FailValidate(err)
}
return event, nil
}
func isTLSAddr(scheme string) bool {
return scheme == "tls" || scheme == "ssl"
}
func jobType(scheme string) string {
switch scheme {
case "tls", "ssl":
return scheme
}
return "plain"
}
func jobName(typ, jobType, host string, ports []uint16) string {
var h string
if len(ports) == 1 {
h = fmt.Sprintf("%v:%v", host, ports[0])
} else {
h = fmt.Sprintf("%v:%v", host, ports)
}
return fmt.Sprintf("%v-%v@%v", typ, jobType, h)
}
func buildDialerChain(
scheme string,
tls *transport.TLSConfig,
config *Config,
) (*dialchain.DialerChain, error) {
d := &dialchain.DialerChain{
Net: dialchain.TCPDialer("tcp_connect_rtt", config.Timeout),
}
if config.Socks5.URL != "" {
d.AddLayer(dialchain.SOCKS5Layer("socks5_connect_rtt", &config.Socks5))
}
if isTLSAddr(scheme) {
d.AddLayer(dialchain.TLSLayer("tls_handshake_rtt", tls, config.Timeout))
}
if err := d.TestBuild(); err != nil {
return nil, err
}
return d, nil
}
func buildHostDialerChainFactory(
scheme string,
tls *transport.TLSConfig,
config *Config,
) (func(string) *dialchain.DialerChain, error) {
template, err := buildDialerChain(scheme, tls, config)
if err != nil {
return nil, err
}
withProxy := config.Socks5.URL != ""
return func(addr string) *dialchain.DialerChain {
if withProxy {
d := template.Clone()
d.Layers[0] = dialchain.ConstAddrLayer(addr, d.Layers[0])
return d
}
return &dialchain.DialerChain{
Net: dialchain.ConstAddrDialer("tcp_connect_rtt", addr, config.Timeout),
Layers: template.Layers,
}
}, nil
}