-
Notifications
You must be signed in to change notification settings - Fork 2
/
ping.go
194 lines (167 loc) · 4.4 KB
/
ping.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
package prober
import (
"context"
"encoding/xml"
"go.uber.org/zap"
"net"
"time"
"mellium.im/xmlstream"
"mellium.im/xmpp"
"mellium.im/xmpp/jid"
"mellium.im/xmpp/stanza"
"github.com/prometheus/client_golang/prometheus"
"github.com/horazont/xmpp-blackbox-exporter/internal/config"
)
type teeLogger struct {
prefix string
logger *zap.Logger
}
func (l teeLogger) Write(p []byte) (n int, err error) {
l.logger.Debug(l.prefix,
zap.Binary("data", p),
)
return len(p), nil
}
func ProbePing(ctx context.Context, module, target string, cfg config.Module, clients Clients, registry *prometheus.Registry) bool {
sl := zap.S()
durationGaugeVec := prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "probe_xmpp_duration_seconds",
Help: "Duration of xmpp connection by phase",
}, []string{"phase"})
pingTimeoutGauge := prometheus.NewGauge(prometheus.GaugeOpts{
Name: "probe_ping_timeout",
Help: "Indicator that the ping timed out",
})
registry.MustRegister(pingTimeoutGauge)
pingRTTGauge := prometheus.NewGauge(prometheus.GaugeOpts{
Name: "probe_xmpp_ping_duration_seconds",
Help: "Ping round-trip time",
})
var session *xmpp.Session
var conn net.Conn
var ct connTrace
client, ok := clients[cfg.Ping.Account]
if !ok {
sl.Errorw("undeclared client; probably config reload race",
"account", cfg.Ping.Account,
"module", module,
)
return false
}
target_addr, err := jid.Parse(target)
if err != nil {
sl.Errorw("invalid ping probe target JID",
"target", target,
"module", module,
"err", err,
)
return false
}
if cfg.Ping.NoSharedConnection {
registry.MustRegister(durationGaugeVec)
ct, conn, session, err = client.Config.Login(ctx)
if err != nil {
sl.Errorw("failed to established non-shared session for ping",
"target", target,
"account", cfg.Ping.Account,
"module", module,
"err", err,
)
return false
}
defer conn.Close()
defer session.Close()
durationGaugeVec.WithLabelValues("connect").Set(ct.connectDone.Sub(ct.start).Seconds())
durationGaugeVec.WithLabelValues("starttls").Set(ct.starttlsDone.Sub(ct.connectDone).Seconds())
durationGaugeVec.WithLabelValues("auth").Set(ct.authDone.Sub(ct.starttlsDone).Seconds())
go session.Serve(nil)
} else {
session, err = client.AcquireSession(ctx)
if err != nil {
sl.Errorw("failed to acquire shared session for ping",
"target", target,
"account", cfg.Ping.Account,
"module", module,
"err", err,
)
return false
}
}
tping := time.Now()
iq := stanza.IQ{
To: target_addr,
Type: stanza.GetIQ,
}
response_stream, err := session.SendIQElement(ctx, xmlstream.Wrap(
nil,
xml.StartElement{Name: xml.Name{Local: "ping", Space: "urn:xmpp:ping"}},
), iq)
if response_stream != nil {
defer response_stream.Close()
}
tpong := time.Now()
registry.MustRegister(pingRTTGauge)
pingRTTGauge.Set(tpong.Sub(tping).Seconds())
if err != nil {
sl.Errorw("failed to send stanza",
"target", target,
"account", cfg.Ping.Account,
"module", module,
"err", err,
)
pingTimeoutGauge.Set(1)
if !cfg.Ping.NoSharedConnection {
// ensure that it’s still alive
client.Healthcheck()
}
return false
}
response := struct {
stanza.IQ
Error stanza.Error `xml:"jabber:client error"`
Ping struct{} `xml:"urn:xmpp:ping ping"`
}{}
d := xml.NewTokenDecoder(response_stream)
start_token, err := d.Token()
start := start_token.(xml.StartElement)
err = d.DecodeElement(&response, &start)
if err != nil {
sl.Errorw("failed to parse ping reply",
"target", target,
"account", cfg.Ping.Account,
"module", module,
"err", err,
)
return false
}
var result config.PingResult
if response.Type == stanza.ResultIQ {
result.Success = true
} else if response.Type == stanza.ErrorIQ {
result.ErrorType = string(response.Error.Type)
result.ErrorCondition = string(response.Error.Condition)
} else {
sl.Errorw("failed to parse ping result",
"target", target,
"account", cfg.Ping.Account,
"module", module,
"err", err,
)
return false
}
permittedResults := cfg.Ping.ExpectedResults
if permittedResults == nil {
permittedResults = []config.PingResult{config.PingResult{Success: true}}
}
for _, permitted := range permittedResults {
if permitted.Matches(result) {
return true
}
}
sl.Debugw("ping result did not match requirements, returning error",
"target", target,
"module", module,
"account", cfg.Ping.Account,
)
return false
}