-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
128 lines (112 loc) · 2.83 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
package main
import (
"net"
"net/http"
"reflect"
"strings"
dnstap "github.com/dnstap/golang-dnstap"
"github.com/gopuff/morecontext"
"github.com/miekg/dns"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/sirupsen/logrus"
"google.golang.org/protobuf/proto"
corev1 "k8s.io/api/core/v1"
)
var (
query = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "dnstap_dns_queries_total",
Help: "The total number of dns queries",
}, []string{"name", "type", "ns", "pod"})
)
func main() {
cfg := setupConfig()
ctx := morecontext.ForSignals()
if !cfg.GetBool("metrics.disabled") {
http.Handle("/metrics", promhttp.Handler())
go http.ListenAndServe(cfg.GetString("metrics.addr"), nil)
}
l, err := net.Listen("tcp", cfg.GetString("listen.addr"))
if err != nil {
lg.WithError(err).Fatal("failed to listen")
}
lg.Info("listening")
m := k8sMap(ctx)
i := dnstap.NewFrameStreamSockInput(l)
bch := make(chan []byte)
go i.ReadInto(bch)
ignores := cfg.GetStringSlice("suffixes.ignore")
only := cfg.GetStringSlice("suffixes.only")
for {
select {
case <-ctx.Done():
return
case bs := <-bch:
var f dnstap.Dnstap
err := proto.Unmarshal(bs, &f)
if err != nil {
lg.WithError(err).Error("failed to decode dnstap message")
continue
}
addr := net.IP(f.Message.QueryAddress)
v, ok := m.Load(addr.String())
if !ok {
lg.WithFields(logrus.Fields{
"addr": addr,
}).Error("no pod found")
continue
}
pod, ok := v.(*corev1.Pod)
if !ok {
lg.WithField("addr", addr).WithField("type", reflect.TypeOf(v)).Error("cached value is not a pod")
continue
}
lg := lg.WithFields(logrus.Fields{
"pod": pod.Name,
"ns": pod.Namespace,
// "addr": addr,
// "tap": f,
})
lg.Debug("dnstap message received")
msgBs := f.Message.GetQueryMessage()
if len(msgBs) == 0 {
msgBs = f.Message.GetResponseMessage()
}
if len(msgBs) == 0 {
lg.Error("no dns message found")
continue
}
if *f.Message.Type != dnstap.Message_CLIENT_QUERY {
lg.WithField("type", f.Message.Type.String()).Debug("not a query")
continue
}
var msg dns.Msg
err = msg.Unpack(msgBs)
if err != nil {
lg.WithError(err).Error("failed to decode dns message")
continue
}
q:
for _, n := range msg.Question {
if len(only) > 0 {
for _, suff := range only {
if !strings.HasSuffix(n.Name, suff) {
continue q
}
}
}
for _, suff := range ignores {
if strings.HasSuffix(n.Name, suff) {
continue q
}
}
query.WithLabelValues(n.Name, dns.TypeToString[n.Qtype], pod.Namespace, pod.Name).Inc()
if cfg.GetBool("noLog") {
continue
}
lg.WithField("name", n.Name).Info()
}
}
}
}