This repository was archived by the owner on Jan 15, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
142 lines (123 loc) · 4.39 KB
/
main.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
package main
import (
"flag"
"fmt"
"log"
"net"
"net/http"
"sort"
"strings"
"time"
"github.com/docker/libnetwork/resolvconf"
"github.com/docker/libnetwork/types"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var (
promNameserver = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: "node_dns",
Name: "nameserver",
},
[]string{"server", "node"},
)
promSearchdomain = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: "node_dns",
Name: "searchdomain",
},
[]string{"host", "node"},
)
promHostTest = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: "node_dns",
Name: "test_result",
},
[]string{"host", "result", "status", "node"},
)
)
var addr = flag.String("listen-address", "127.0.0.1:8080", "The address to listen on for HTTP requests.")
var test = flag.String("test-hosts", "nrk.no,vg.no,example.com", "Comma separated list of hosts to test DNS resolution")
var requiredSearchdomains = flag.String("req-searchdomains","", "Comma separated list of required searchdomains")
var testInterval = flag.Int("test-interval-seconds", 10, "Interval in seconds for running test DNS resolution")
var nodeName = flag.String("node-name", "localhost", "The name of the node running the container")
func init() {
prometheus.MustRegister(promNameserver)
prometheus.MustRegister(promSearchdomain)
prometheus.MustRegister(promHostTest)
}
func main() {
flag.Parse()
conf, err := resolvconf.Get()
if err != nil {
panic(err)
}
nameservers := resolvconf.GetNameservers(conf.Content, types.IPv4)
for _, server := range nameservers {
promNameserver.With(prometheus.Labels{"server": server, "node": *nodeName}).Inc()
}
searchdomains := resolvconf.GetSearchDomains(conf.Content)
if *requiredSearchdomains != "" {
sort.Strings(searchdomains)
reqSearchdomains := strings.Split(strings.ReplaceAll(*requiredSearchdomains," ",""), ",")
for _, host := range reqSearchdomains{
i := sort.SearchStrings(searchdomains,host)
if !(i < len(searchdomains) && searchdomains[i] == host){
promSearchdomain.With(prometheus.Labels{"host": host, "node": *nodeName}).Desc()
}
}
}
for _, host := range searchdomains {
promSearchdomain.With(prometheus.Labels{"host": host, "node": *nodeName}).Inc()
}
testhosts := strings.Split(strings.ReplaceAll(*test," ",""), ",")
for _, host := range testhosts {
ticker := time.NewTicker(time.Duration(float64(*testInterval)) * time.Second)
quit := make(chan struct{})
go func(host string, metric *prometheus.GaugeVec) {
resultPrev := ""
resultPrevErr := ""
for {
select {
case <-ticker.C:
result, err := net.LookupHost(host)
if err != nil {
resultErr := err.Error()
resultErrSlice := strings.Split(resultErr, ":")
resultErr = strings.TrimPrefix(resultErrSlice[len(resultErrSlice)-1]," ")
fmt.Printf("Error: %s\n", err.Error())
metric.With(prometheus.Labels{"host": host, "status": "failed", "result": resultErr, "node": *nodeName}).Set(1)
if resultPrev != "" {
metric.With(prometheus.Labels{"host": host, "status": "success", "result": resultPrev, "node": *nodeName}).Set(0)
resultPrev = ""
}
// Reset previous error result if it has changed
if resultPrevErr != "" && resultErr != resultPrevErr {
metric.With(prometheus.Labels{"host": host, "status": "failed", "result": resultPrevErr, "node": *nodeName}).Set(0)
}
resultPrevErr = resultErr
} else {
sort.Strings(result)
fmt.Printf("Resolved %s to %s\n", host, result)
resultString := strings.Join(result, ",")
metric.With(prometheus.Labels{"host": host, "status": "success", "result": resultString, "node": *nodeName}).Set(1)
if resultPrevErr != "" {
metric.With(prometheus.Labels{"host": host, "status": "failed", "result": resultPrevErr, "node": *nodeName}).Set(0)
resultPrevErr = ""
}
// Reset previous success result if it has changed
if resultPrev != "" && resultString != resultPrev {
metric.With(prometheus.Labels{"host": host, "status": "success", "result": resultPrev, "node": *nodeName}).Set(0)
}
resultPrev = resultString
}
case <-quit:
ticker.Stop()
return
}
}
}(host, promHostTest)
}
http.Handle("/metrics", promhttp.Handler())
log.Fatal(http.ListenAndServe(*addr, nil))
}