forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
task.go
57 lines (49 loc) · 1.21 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
package tcp
import (
"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/reason"
)
func pingHost(
dialer transport.Dialer,
host string,
timeout time.Duration,
validator ConnCheck,
) (common.MapStr, error) {
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{
"tcp": common.MapStr{
"rtt": common.MapStr{
"validate": look.RTT(end.Sub(validateStart)),
},
},
}
if err != nil {
event["error"] = reason.FailValidate(err)
}
return event, nil
}