forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcp.go
129 lines (108 loc) · 2.71 KB
/
tcp.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
package tcp
import (
"fmt"
"net/url"
"strconv"
"strings"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"
"github.com/elastic/beats/libbeat/outputs"
"github.com/elastic/beats/libbeat/outputs/transport"
"github.com/elastic/beats/heartbeat/monitors"
"github.com/elastic/beats/heartbeat/monitors/active/dialchain"
)
func init() {
monitors.RegisterActive("tcp", create)
}
var debugf = logp.MakeDebug("tcp")
type connURL struct {
Scheme string
Host string
Ports []uint16
}
func create(
info monitors.Info,
cfg *common.Config,
) ([]monitors.Job, error) {
config := DefaultConfig
if err := cfg.Unpack(&config); err != nil {
return nil, err
}
tls, err := outputs.LoadTLSConfig(config.TLS)
if err != nil {
return nil, err
}
defaultScheme := "tcp"
if tls != nil {
defaultScheme = "ssl"
}
endpoints, err := collectHosts(&config, defaultScheme)
if err != nil {
return nil, err
}
typ := config.Name
timeout := config.Timeout
validator := makeValidateConn(&config)
var jobs []monitors.Job
for scheme, eps := range endpoints {
schemeTLS := tls
if scheme == "tcp" || scheme == "plain" {
schemeTLS = nil
}
db, err := dialchain.NewBuilder(dialchain.BuilderSettings{
Timeout: timeout,
Socks5: config.Socks5,
TLS: schemeTLS,
})
if err != nil {
return nil, err
}
epJobs, err := dialchain.MakeDialerJobs(db, typ, scheme, eps, config.Mode,
func(dialer transport.Dialer, addr string) (common.MapStr, error) {
return pingHost(dialer, addr, timeout, validator)
})
if err != nil {
return nil, err
}
jobs = append(jobs, epJobs...)
}
return jobs, nil
}
func collectHosts(config *Config, defaultScheme string) (map[string][]dialchain.Endpoint, error) {
endpoints := map[string][]dialchain.Endpoint{}
for _, h := range config.Hosts {
scheme := defaultScheme
host := ""
u, err := url.Parse(h)
if err != nil || u.Host == "" {
host = h
} else {
scheme = u.Scheme
host = u.Host
}
debugf("Add tcp endpoint '%v://%v'.", scheme, host)
switch scheme {
case "tcp", "plain", "tls", "ssl":
default:
err := fmt.Errorf("'%v' is no supported connection scheme in '%v'", scheme, h)
return nil, err
}
pair := strings.SplitN(host, ":", 2)
ports := config.Ports
if len(pair) == 2 {
port, err := strconv.ParseUint(pair[1], 10, 16)
if err != nil {
return nil, fmt.Errorf("'%v' is no valid port number in '%v'", pair[1], h)
}
ports = []uint16{uint16(port)}
host = pair[0]
} else if len(config.Ports) == 0 {
return nil, fmt.Errorf("host '%v' missing port number", h)
}
endpoints[scheme] = append(endpoints[scheme], dialchain.Endpoint{
Host: host,
Ports: ports,
})
}
return endpoints, nil
}