forked from FortnoxAB/prometheus-net-discovery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.go
112 lines (96 loc) · 2.15 KB
/
helpers.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
package main
import (
"bufio"
"bytes"
"context"
"crypto/tls"
"encoding/json"
"fmt"
"net"
"net/http"
"time"
"github.com/sirupsen/logrus"
)
func inc(ip net.IP) {
for j := len(ip) - 1; j >= 0; j-- {
ip[j]++
if ip[j] > 0 {
break
}
}
}
func getFirst(s []string) string {
for _, v := range s {
return v
}
return ""
}
var client = &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, // #nosec only cockroach at the moment
},
}
func checkExporterExporter(parentCtx context.Context, host, port string) ([]string, error) {
u := fmt.Sprintf("http://%s", net.JoinHostPort(host, port))
ctx, cancel := context.WithTimeout(parentCtx, time.Second*3)
defer cancel()
req, err := http.NewRequestWithContext(ctx, "GET", u, nil)
if err != nil {
return nil, err
}
req.Header.Set("Accept", "application/json")
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("error http get %s: %w", u, err)
}
defer resp.Body.Close()
var anyJSON map[string]interface{}
err = json.NewDecoder(resp.Body).Decode(&anyJSON)
if err != nil {
return nil, fmt.Errorf("error decoding json body: %w", err)
}
exporters := make([]string, len(anyJSON))
i := 0
for k := range anyJSON {
exporters[i] = k
i++
}
return exporters, nil
}
func alive(parentCtx context.Context, host, port, path string) bool {
if path != "" {
u := fmt.Sprintf(path, net.JoinHostPort(host, port))
ctx, cancel := context.WithTimeout(parentCtx, time.Second*3)
defer cancel()
req, err := http.NewRequestWithContext(ctx, "GET", u, nil)
if err != nil {
logrus.Errorf("error creating request: %s", err)
return false
}
resp, err := client.Do(req)
if err != nil {
return false
}
defer resp.Body.Close()
r := bufio.NewReader(resp.Body)
for i := 0; i < 10; i++ {
line, _, err := r.ReadLine()
if err != nil {
return false
}
if bytes.Contains(line, []byte("# TYPE")) {
return true
}
}
return false
}
conn, err := net.DialTimeout("tcp", net.JoinHostPort(host, port), 200*time.Millisecond)
if err != nil {
return false
}
if conn != nil {
conn.Close()
return true
}
return false
}