-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
tcp.go
182 lines (158 loc) · 4.28 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
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
package maprobe
import (
"bufio"
"context"
"crypto/tls"
"encoding/json"
"io"
"log"
"net"
"regexp"
"time"
mackerel "github.com/mackerelio/mackerel-client-go"
"github.com/pkg/errors"
)
var (
DefaultTCPTimeout = 5 * time.Second
DefaultTCPMaxBytes = 32 * 1024
DefaultTCPMetricKeyPrefix = "tcp"
)
type TCPProbeConfig struct {
Host string `yaml:"host"`
Port string `yaml:"port"`
Timeout time.Duration `yaml:"timeout"`
Send string `yaml:"send"`
Quit string `yaml:"quiet"`
MaxBytes int `yaml:"max_bytes"`
ExpectPattern string `yaml:"expect_pattern"`
TLS bool `yaml:"tls"`
NoCheckCertificate bool `yaml:"no_check_certificate"`
MetricKeyPrefix string `yaml:"metric_key_prefix"`
}
func (pc *TCPProbeConfig) GenerateProbe(host *mackerel.Host) (Probe, error) {
p := &TCPProbe{
hostID: host.ID,
metricKeyPrefix: pc.MetricKeyPrefix,
Timeout: pc.Timeout,
MaxBytes: pc.MaxBytes,
TLS: pc.TLS,
NoCheckCertificate: pc.NoCheckCertificate,
}
var err error
p.Host, err = expandPlaceHolder(pc.Host, host)
if err != nil {
return nil, errors.Wrap(err, "invalid host")
}
p.Port, err = expandPlaceHolder(pc.Port, host)
if err != nil {
return nil, errors.Wrap(err, "invaild port")
}
p.Send, err = expandPlaceHolder(pc.Send, host)
if err != nil {
return nil, errors.Wrap(err, "invalid send")
}
var pattern string
pattern, err = expandPlaceHolder(pc.ExpectPattern, host)
if err != nil {
return nil, errors.Wrap(err, "invalid expect_pattern")
}
if pattern != "" {
p.ExpectPattern, err = regexp.Compile(pattern)
if err != nil {
return nil, errors.Wrap(err, "invalid expect_pattern")
}
}
if p.Timeout == 0 {
p.Timeout = DefaultTCPTimeout
}
if p.MaxBytes == 0 {
p.MaxBytes = DefaultTCPMaxBytes
}
if p.metricKeyPrefix == "" {
p.metricKeyPrefix = DefaultTCPMetricKeyPrefix
}
return p, nil
}
type TCPProbe struct {
hostID string
metricKeyPrefix string
Host string
Port string
Send string
Quit string
MaxBytes int
ExpectPattern *regexp.Regexp
Timeout time.Duration
TLS bool
NoCheckCertificate bool
}
func (p *TCPProbe) HostID() string {
return p.hostID
}
func (p *TCPProbe) MetricName(name string) string {
return p.metricKeyPrefix + "." + name
}
func (p *TCPProbe) String() string {
b, _ := json.Marshal(p)
return string(b)
}
func (p *TCPProbe) Run(ctx context.Context) (ms HostMetrics, err error) {
var ok bool
start := time.Now()
defer func() {
log.Println("[debug] defer", ok)
elapsed := time.Now().Sub(start)
ms = append(ms, newMetric(p, "elapsed.seconds", elapsed.Seconds()))
if ok {
ms = append(ms, newMetric(p, "check.ok", 1))
} else {
ms = append(ms, newMetric(p, "check.ok", 0))
}
log.Println("[debug]", ms.String())
}()
ctx, cancel := context.WithTimeout(ctx, p.Timeout)
defer cancel()
addr := net.JoinHostPort(p.Host, p.Port)
log.Println("[debug] dialing", addr)
conn, err := dialTCP(addr, p.TLS, p.NoCheckCertificate, p.Timeout)
if err != nil {
return ms, errors.Wrap(err, "connect failed")
}
defer conn.Close()
conn.SetDeadline(time.Now().Add(p.Timeout))
log.Println("[debug] connected", addr)
if p.Send != "" {
log.Println("[debug] send", p.Send)
_, err := io.WriteString(conn, p.Send)
if err != nil {
return ms, errors.Wrap(err, "send failed")
}
}
if p.ExpectPattern != nil {
buf := make([]byte, p.MaxBytes)
r := bufio.NewReader(conn)
n, err := r.Read(buf)
if err != nil {
return ms, errors.Wrap(err, "read failed")
}
log.Println("[debug] read", string(buf[:n]))
if !p.ExpectPattern.Match(buf[:n]) {
return ms, errors.Wrap(err, "unexpected response")
}
}
if p.Quit != "" {
log.Println("[debug]", p.Quit)
io.WriteString(conn, p.Quit)
}
ok = true
return
}
func dialTCP(address string, useTLS bool, noCheckCertificate bool, timeout time.Duration) (net.Conn, error) {
d := &net.Dialer{Timeout: timeout}
if useTLS {
return tls.DialWithDialer(d, "tcp", address, &tls.Config{
InsecureSkipVerify: noCheckCertificate,
})
}
return d.Dial("tcp", address)
}