forked from percona/pmm-client
/
check_network.go
361 lines (313 loc) · 11.7 KB
/
check_network.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*
Copyright (c) 2016, Percona LLC and/or its affiliates. All rights reserved.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
*/
package pmm
import (
"crypto/tls"
"fmt"
"math"
"net"
"net/http"
"regexp"
"sort"
"strconv"
"strings"
"time"
"github.com/beevik/ntp"
"github.com/fatih/color"
"golang.org/x/net/context"
)
// CheckNetwork check connectivity between client and server.
func (a *Admin) CheckNetwork() error {
// Check QAN API health.
qanStatus := false
url := a.qanAPI.URL(a.serverURL, qanAPIBasePath, "ping")
if resp, _, err := a.qanAPI.Get(url); err == nil {
if resp.StatusCode == http.StatusOK && resp.Header.Get("X-Percona-Qan-Api-Version") != "" {
qanStatus = true
}
}
// Check Prometheus API by retrieving all "up" time series.
promStatus := true
promData, err := a.promQueryAPI.Query(context.Background(), "up", time.Now())
if err != nil {
promStatus = false
}
bindAddress := ""
if a.Config.ClientAddress != a.Config.BindAddress {
bindAddress = fmt.Sprintf("(%s)", a.Config.BindAddress)
}
fmt.Print("PMM Network Status\n\n")
fmt.Printf("%-14s | %s\n", "Server Address", a.Config.ServerAddress)
fmt.Printf("%-14s | %s %s\n\n", "Client Address", a.Config.ClientAddress, bindAddress)
t := a.getNginxHeader("X-Server-Time")
if t != "" {
timeFormat := "2006-01-02 15:04:05 -0700 MST"
// Real time (ntp server time)
ntpHost := "0.pool.ntp.org"
ntpTime, ntpTimeErr := ntp.Time(ntpHost)
ntpTimeText := ""
if ntpTimeErr != nil {
ntpTimeText = fmt.Sprintf("unable to get ntp time: %s", err)
} else {
ntpTimeText = ntpTime.Format(timeFormat)
}
// Server time
color.New(color.Bold).Println("* System Time")
var serverTime time.Time
if s, err := strconv.ParseInt(t, 10, 64); err == nil {
serverTime = time.Unix(s, 0)
} else {
serverTime, _ = time.Parse("Monday, 02-Jan-2006 15:04:05 MST", t)
}
// Client Time
clientTime := time.Now()
// Print times
if ntpTimeErr == nil {
fmt.Printf("%-35s | %s\n", fmt.Sprintf("NTP Server (%s)", ntpHost), ntpTimeText)
}
fmt.Printf("%-35s | %s\n", "PMM Server", serverTime.Format(timeFormat))
fmt.Printf("%-35s | %s\n", "PMM Client", clientTime.Format(timeFormat))
allowedDriftTime := float64(60) // seconds
if ntpTimeErr == nil {
// Calculate time drift between NTP Server and PMM Server
drift := math.Abs(float64(serverTime.Unix()) - float64(ntpTime.Unix()))
fmt.Printf("%-35s | %s\n", "PMM Server Time Drift", colorStatus("OK", fmt.Sprintf("%.0fs", drift), drift <= allowedDriftTime))
if drift > allowedDriftTime {
fmt.Print("Time is out of sync. Please make sure the server time is correct to see the metrics.\n")
}
// Calculate time drift between NTP Server and PMM Client
drift = math.Abs(float64(clientTime.Unix()) - float64(ntpTime.Unix()))
fmt.Printf("%-35s | %s\n", "PMM Client Time Drift", colorStatus("OK", fmt.Sprintf("%.0fs", drift), drift <= allowedDriftTime))
if drift > allowedDriftTime {
fmt.Print("Time is out of sync. Please make sure the client time is correct to see the metrics.\n")
}
}
// Calculate time drift between server and client
drift := math.Abs(float64(serverTime.Unix()) - float64(clientTime.Unix()))
fmt.Printf("%-35s | %s\n", "PMM Client to PMM Server Time Drift", colorStatus("OK", fmt.Sprintf("%.0fs", drift), drift <= allowedDriftTime))
if drift > allowedDriftTime {
fmt.Print("Time is out of sync. Please make sure the server time is correct to see the metrics.\n")
}
}
fmt.Println()
color.New(color.Bold).Println("* Connection: Client --> Server")
fmt.Printf("%-20s %-13s\n", strings.Repeat("-", 20), strings.Repeat("-", 7))
fmt.Printf("%-20s %-13s\n", "SERVER SERVICE", "STATUS")
fmt.Printf("%-20s %-13s\n", strings.Repeat("-", 20), strings.Repeat("-", 7))
// Consul is always alive if we are at this point.
fmt.Printf("%-20s %-13s\n", "Consul API", colorStatus("OK", "", true))
fmt.Printf("%-20s %-13s\n", "Prometheus API", colorStatus("OK", "DOWN", promStatus))
fmt.Printf("%-20s %-13s\n\n", "Query Analytics API", colorStatus("OK", "DOWN", qanStatus))
a.testNetwork()
fmt.Println()
node, _, err := a.consulAPI.Catalog().Node(a.Config.ClientName, nil)
if err != nil || node == nil {
fmt.Printf("%s '%s'.\n\n", noMonitoring, a.Config.ClientName)
return nil
}
if !promStatus {
fmt.Print("Prometheus is down. Please check if PMM server container runs properly.\n\n")
return nil
}
fmt.Println()
color.New(color.Bold).Println("* Connection: Client <-- Server")
if len(node.Services) == 0 {
fmt.Print("No metric endpoints registered.\n\n")
return nil
}
// Check Prometheus endpoint status.
svcTable := []ServiceStatus{}
errStatus := false
for _, svc := range node.Services {
if !strings.HasSuffix(svc.Service, ":metrics") {
continue
}
name := "-"
for _, tag := range svc.Tags {
if strings.HasPrefix(tag, "alias_") {
name = tag[6:]
continue
}
}
running := checkPromTargetStatus(promData.String(), name, strings.Split(svc.Service, ":")[0])
if !running {
errStatus = true
}
// Check protection status.
localStatus := getServiceStatus(fmt.Sprintf("pmm-%s-%d", strings.Replace(svc.Service, ":", "-", 1), svc.Port))
sslVal := "-"
protectedVal := "-"
if localStatus {
sslVal = colorStatus("YES", "NO", a.isSSLProtected(svc.Service, svc.Port))
if a.Config.ServerUser != "" {
protectedVal = colorStatus("YES", "NO", a.isPasswordProtected(svc.Service, svc.Port))
}
}
row := ServiceStatus{
Type: svc.Service,
Name: name,
Port: fmt.Sprintf("%d", svc.Port),
Running: running,
SSL: sslVal,
Password: protectedVal,
}
svcTable = append(svcTable, row)
}
maxTypeLen := len("SERVICE TYPE")
maxNameLen := len("NAME")
for _, in := range svcTable {
if len(in.Type) > maxTypeLen {
maxTypeLen = len(in.Type)
}
if len(in.Name) > maxNameLen {
maxNameLen = len(in.Name)
}
}
maxTypeLen++
maxNameLen++
maxAddrLen := len(a.Config.ClientAddress) + 7
maxStatusLen := 7
maxProtectedLen := 9
maxSSLLen := 10
if a.Config.ClientAddress != a.Config.BindAddress {
maxAddrLen = len(a.Config.ClientAddress) + len(a.Config.BindAddress) + 10
}
fmtPattern := "%%-%ds %%-%ds %%-%ds %%-%ds %%-%ds %%-%ds\n"
linefmt := fmt.Sprintf(fmtPattern, maxTypeLen, maxNameLen, maxAddrLen, maxStatusLen, maxSSLLen, maxProtectedLen)
fmt.Printf(linefmt, strings.Repeat("-", maxTypeLen), strings.Repeat("-", maxNameLen), strings.Repeat("-", maxAddrLen),
strings.Repeat("-", maxStatusLen), strings.Repeat("-", maxSSLLen), strings.Repeat("-", maxProtectedLen))
fmt.Printf(linefmt, "SERVICE TYPE", "NAME", "REMOTE ENDPOINT", "STATUS", "HTTPS/TLS", "PASSWORD")
fmt.Printf(linefmt, strings.Repeat("-", maxTypeLen), strings.Repeat("-", maxNameLen), strings.Repeat("-", maxAddrLen),
strings.Repeat("-", maxStatusLen), strings.Repeat("-", maxSSLLen), strings.Repeat("-", maxProtectedLen))
sort.Sort(sortOutput(svcTable))
maxStatusLen += 11
linefmt = fmt.Sprintf(fmtPattern, maxTypeLen, maxNameLen, maxAddrLen, maxStatusLen, maxSSLLen, maxProtectedLen)
for _, i := range svcTable {
if i.SSL != "-" {
linefmt = fmt.Sprintf(fmtPattern, maxTypeLen, maxNameLen, maxAddrLen, maxStatusLen, maxSSLLen+11, maxProtectedLen)
}
if a.Config.ClientAddress != a.Config.BindAddress {
fmt.Printf(linefmt, i.Type, i.Name, a.Config.ClientAddress+"-->"+a.Config.BindAddress+":"+i.Port,
colorStatus("OK", "DOWN", i.Running), i.SSL, i.Password)
} else {
fmt.Printf(linefmt, i.Type, i.Name, a.Config.ClientAddress+":"+i.Port,
colorStatus("OK", "DOWN", i.Running), i.SSL, i.Password)
}
}
if errStatus {
scheme := "http"
if a.Config.ServerInsecureSSL || a.Config.ServerSSL {
scheme = "https"
}
url := fmt.Sprintf("%s://%s/prometheus/targets", scheme, a.Config.ServerAddress)
fmt.Printf(`
When an endpoint is down it may indicate that the corresponding service is stopped (run 'pmm-admin list' to verify).
If it's running, check out the logs /var/log/pmm-*.log
When all endpoints are down but 'pmm-admin list' shows they are up and no errors in the logs,
check the firewall settings whether this system allows incoming connections from server to address:port in question.
Also you can check the endpoint status by the URL: %s
`, url)
if a.Config.ClientAddress != a.Config.BindAddress {
fmt.Println(`
IMPORTANT: client and bind addresses are not the same which means you need to configure NAT/port forwarding to map them.`)
}
}
fmt.Println()
return nil
}
// testNetwork measure round trip duration of server connection.
func (a *Admin) testNetwork() {
insecureFlag := false
if a.Config.ServerInsecureSSL {
insecureFlag = true
}
conn := &networkTransport{
dialer: &net.Dialer{
Timeout: a.apiTimeout,
KeepAlive: a.apiTimeout,
},
}
conn.rtp = &http.Transport{
Proxy: http.ProxyFromEnvironment,
Dial: conn.dial,
TLSClientConfig: &tls.Config{InsecureSkipVerify: insecureFlag},
}
client := &http.Client{Transport: conn}
resp, err := client.Get(a.serverURL)
if err != nil {
fmt.Println("Unable to measure the connection performance.")
return
}
defer resp.Body.Close()
fmt.Printf("%-19s | %v\n", "Connection duration", conn.connEnd.Sub(conn.connStart))
fmt.Printf("%-19s | %v\n", "Request duration", conn.reqEnd.Sub(conn.reqStart)-conn.connEnd.Sub(conn.connStart))
fmt.Printf("%-19s | %v\n", "Full round trip", conn.reqEnd.Sub(conn.reqStart))
}
type networkTransport struct {
rtp http.RoundTripper
dialer *net.Dialer
connStart time.Time
connEnd time.Time
reqStart time.Time
reqEnd time.Time
}
func (conn *networkTransport) RoundTrip(r *http.Request) (*http.Response, error) {
conn.reqStart = time.Now()
resp, err := conn.rtp.RoundTrip(r)
conn.reqEnd = time.Now()
return resp, err
}
func (conn *networkTransport) dial(network, addr string) (net.Conn, error) {
conn.connStart = time.Now()
cn, err := conn.dialer.Dial(network, addr)
conn.connEnd = time.Now()
return cn, err
}
// checkPromTargetStatus check Prometheus target state by metric labels.
func checkPromTargetStatus(data, alias, job string) bool {
r, _ := regexp.Compile(fmt.Sprintf(`up{.*instance="%s".*job="%s".*} => 1`, alias, job))
for _, row := range strings.Split(data, "\n") {
if r.MatchString(row) {
return true
}
}
return false
}
// isPasswordProtected check if endpoint is password protected.
func (a *Admin) isPasswordProtected(svcType string, port int) bool {
urlPath := "metrics"
if svcType == "mysql:metrics" {
urlPath = "metrics-hr"
}
scheme := "http"
api := a.qanAPI
if a.isSSLProtected(svcType, port) {
scheme = "https"
// Enforce InsecureSkipVerify true to bypass err and check http code.
api = NewAPI(true, apiTimeout, a.Verbose)
}
url := api.URL(fmt.Sprintf("%s://%s:%d", scheme, a.Config.BindAddress, port), urlPath)
if resp, _, err := api.Get(url); err == nil && resp.StatusCode == http.StatusUnauthorized {
return true
}
return false
}
// isSSLProtected check if endpoint is https/tls protected.
func (a *Admin) isSSLProtected(svcType string, port int) bool {
url := a.qanAPI.URL(fmt.Sprintf("http://%s:%d", a.Config.BindAddress, port))
if _, _, err := a.qanAPI.Get(url); err != nil && strings.Contains(err.Error(), "malformed HTTP response") {
return true
}
return false
}