forked from prometheus/blackbox_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
60 lines (47 loc) · 1.42 KB
/
utils.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
package main
import (
"net"
"time"
"github.com/prometheus/client_golang/prometheus"
)
// Returns the preferedIPProtocol, the dialProtocol, and sets the probeIPProtocolGauge.
func chooseProtocol(preferredIPProtocol string, target string, registry *prometheus.Registry) (*net.IPAddr, error) {
var fallbackProtocol string
probeDNSLookupTimeSeconds := prometheus.NewGauge(prometheus.GaugeOpts{
Name: "probe_dns_lookup_time_seconds",
Help: "Returns the time taken for probe dns lookup in seconds",
})
probeIPProtocolGauge := prometheus.NewGauge(prometheus.GaugeOpts{
Name: "probe_ip_protocol",
Help: "Specifies whether probe ip protocol is IP4 or IP6",
})
registry.MustRegister(probeIPProtocolGauge)
registry.MustRegister(probeDNSLookupTimeSeconds)
if preferredIPProtocol == "ip6" || preferredIPProtocol == "" {
preferredIPProtocol = "ip6"
fallbackProtocol = "ip4"
} else {
preferredIPProtocol = "ip4"
fallbackProtocol = "ip6"
}
if preferredIPProtocol == "ip6" {
fallbackProtocol = "ip4"
} else {
fallbackProtocol = "ip6"
}
resolveStart := time.Now()
ip, err := net.ResolveIPAddr(preferredIPProtocol, target)
if err != nil {
ip, err = net.ResolveIPAddr(fallbackProtocol, target)
if err != nil {
return ip, err
}
}
probeDNSLookupTimeSeconds.Add(time.Since(resolveStart).Seconds())
if ip.IP.To4() == nil {
probeIPProtocolGauge.Set(6)
} else {
probeIPProtocolGauge.Set(4)
}
return ip, nil
}