-
Notifications
You must be signed in to change notification settings - Fork 50
/
registry.go
440 lines (412 loc) · 11.9 KB
/
registry.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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
package containers
import (
"bytes"
"fmt"
"os"
"regexp"
"strings"
"sync"
"time"
"github.com/coroot/coroot-node-agent/cgroup"
"github.com/coroot/coroot-node-agent/common"
"github.com/coroot/coroot-node-agent/ebpftracer"
"github.com/coroot/coroot-node-agent/flags"
"github.com/coroot/coroot-node-agent/proc"
"github.com/prometheus/client_golang/prometheus"
"github.com/vishvananda/netns"
"inet.af/netaddr"
"k8s.io/klog/v2"
)
var (
selfNetNs = netns.None()
hostNetNsId = netns.None().UniqueId()
agentPid = uint32(os.Getpid())
containerIdRegexp = regexp.MustCompile(`[a-z0-9]{64}`)
)
type ProcessInfo struct {
Pid uint32
ContainerId ContainerID
StartedAt time.Time
}
type Registry struct {
reg prometheus.Registerer
tracer *ebpftracer.Tracer
events chan ebpftracer.Event
hostConntrack *Conntrack
containersById map[ContainerID]*Container
containersByCgroupId map[string]*Container
containersByPid map[uint32]*Container
ip2fqdn map[netaddr.IP]string
ip2fqdnLock sync.Mutex
processInfoCh chan<- ProcessInfo
}
func NewRegistry(reg prometheus.Registerer, kernelVersion string, processInfoCh chan<- ProcessInfo) (*Registry, error) {
ns, err := proc.GetSelfNetNs()
if err != nil {
return nil, err
}
selfNetNs = ns
hostNetNs, err := proc.GetHostNetNs()
if err != nil {
return nil, err
}
defer hostNetNs.Close()
hostNetNsId = hostNetNs.UniqueId()
err = proc.ExecuteInNetNs(hostNetNs, selfNetNs, func() error {
if err := TaskstatsInit(); err != nil {
return err
}
return nil
})
if err != nil {
return nil, err
}
if err := cgroup.Init(); err != nil {
return nil, err
}
if err := DockerdInit(); err != nil {
klog.Warningln(err)
}
if err := ContainerdInit(); err != nil {
klog.Warningln(err)
}
if err := CrioInit(); err != nil {
klog.Warningln(err)
}
if err := JournaldInit(); err != nil {
klog.Warningln(err)
}
ct, err := NewConntrack(hostNetNs)
if err != nil {
return nil, err
}
r := &Registry{
reg: reg,
events: make(chan ebpftracer.Event, 10000),
hostConntrack: ct,
containersById: map[ContainerID]*Container{},
containersByCgroupId: map[string]*Container{},
containersByPid: map[uint32]*Container{},
ip2fqdn: map[netaddr.IP]string{},
processInfoCh: processInfoCh,
tracer: ebpftracer.NewTracer(kernelVersion, *flags.DisableL7Tracing),
}
if err = reg.Register(r); err != nil {
return nil, err
}
go r.handleEvents(r.events)
if err = r.tracer.Run(r.events); err != nil {
close(r.events)
return nil, err
}
return r, nil
}
func (r *Registry) Describe(ch chan<- *prometheus.Desc) {
ch <- metrics.Ip2Fqdn
}
func (r *Registry) Collect(ch chan<- prometheus.Metric) {
r.ip2fqdnLock.Lock()
defer r.ip2fqdnLock.Unlock()
for ip, fqdn := range r.ip2fqdn {
ch <- gauge(metrics.Ip2Fqdn, 1, ip.String(), fqdn)
}
}
func (r *Registry) Close() {
r.tracer.Close()
close(r.events)
}
func (r *Registry) handleEvents(ch <-chan ebpftracer.Event) {
gcTicker := time.NewTicker(gcInterval)
defer gcTicker.Stop()
for {
select {
case now := <-gcTicker.C:
for pid, c := range r.containersByPid {
cg, err := proc.ReadCgroup(pid)
if err != nil {
delete(r.containersByPid, pid)
if c != nil {
c.onProcessExit(pid, false)
}
continue
}
if c != nil && cg.Id != c.cgroup.Id {
delete(r.containersByPid, pid)
c.onProcessExit(pid, false)
}
}
activeIPs := map[netaddr.IP]struct{}{}
for id, c := range r.containersById {
for dst := range c.connectLastAttempt {
activeIPs[dst.IP()] = struct{}{}
}
if !c.Dead(now) {
continue
}
klog.Infoln("deleting dead container:", id)
for cg, cc := range r.containersByCgroupId {
if cc == c {
delete(r.containersByCgroupId, cg)
}
}
for pid, cc := range r.containersByPid {
if cc == c {
delete(r.containersByPid, pid)
}
}
if ok := prometheus.WrapRegistererWith(prometheus.Labels{"container_id": string(id)}, r.reg).Unregister(c); !ok {
klog.Warningln("failed to unregister container:", id)
}
delete(r.containersById, id)
c.Close()
}
r.ip2fqdnLock.Lock()
for ip := range r.ip2fqdn {
if _, ok := activeIPs[ip]; !ok {
delete(r.ip2fqdn, ip)
}
}
r.ip2fqdnLock.Unlock()
case e, more := <-ch:
if !more {
return
}
switch e.Type {
case ebpftracer.EventTypeProcessStart:
c, seen := r.containersByPid[e.Pid]
switch { // possible pids wraparound + missed `process-exit` event
case c == nil && seen: // ignored
delete(r.containersByPid, e.Pid)
case c != nil: // revalidating by cgroup
cg, err := proc.ReadCgroup(e.Pid)
if err != nil || cg.Id != c.cgroup.Id {
delete(r.containersByPid, e.Pid)
c.onProcessExit(e.Pid, false)
}
}
if c := r.getOrCreateContainer(e.Pid); c != nil {
p := c.onProcessStart(e.Pid)
if r.processInfoCh != nil && p != nil {
r.processInfoCh <- ProcessInfo{Pid: p.Pid, ContainerId: c.id, StartedAt: p.StartedAt}
}
}
case ebpftracer.EventTypeProcessExit:
if c := r.containersByPid[e.Pid]; c != nil {
c.onProcessExit(e.Pid, e.Reason == ebpftracer.EventReasonOOMKill)
}
delete(r.containersByPid, e.Pid)
case ebpftracer.EventTypeFileOpen:
if c := r.getOrCreateContainer(e.Pid); c != nil {
c.onFileOpen(e.Pid, e.Fd)
}
case ebpftracer.EventTypeListenOpen:
if c := r.getOrCreateContainer(e.Pid); c != nil {
c.onListenOpen(e.Pid, e.SrcAddr, false)
} else {
klog.Infoln("TCP listen open from unknown container", e)
}
case ebpftracer.EventTypeListenClose:
if c := r.containersByPid[e.Pid]; c != nil {
c.onListenClose(e.Pid, e.SrcAddr)
}
case ebpftracer.EventTypeConnectionOpen:
if c := r.getOrCreateContainer(e.Pid); c != nil {
c.onConnectionOpen(e.Pid, e.Fd, e.SrcAddr, e.DstAddr, e.Timestamp, false)
c.attachTlsUprobes(r.tracer, e.Pid)
} else {
klog.Infoln("TCP connection from unknown container", e)
}
case ebpftracer.EventTypeConnectionError:
if c := r.getOrCreateContainer(e.Pid); c != nil {
c.onConnectionOpen(e.Pid, e.Fd, e.SrcAddr, e.DstAddr, 0, true)
} else {
klog.Infoln("TCP connection error from unknown container", e)
}
case ebpftracer.EventTypeConnectionClose:
srcDst := AddrPair{src: e.SrcAddr, dst: e.DstAddr}
for _, c := range r.containersById {
if c.onConnectionClose(srcDst) {
break
}
}
case ebpftracer.EventTypeTCPRetransmit:
srcDst := AddrPair{src: e.SrcAddr, dst: e.DstAddr}
for _, c := range r.containersById {
if c.onRetransmit(srcDst) {
break
}
}
case ebpftracer.EventTypeL7Request:
if e.L7Request == nil {
continue
}
if c := r.containersByPid[e.Pid]; c != nil {
ip2fqdn := c.onL7Request(e.Pid, e.Fd, e.Timestamp, e.L7Request)
r.ip2fqdnLock.Lock()
for ip, fqdn := range ip2fqdn {
r.ip2fqdn[ip] = fqdn
}
r.ip2fqdnLock.Unlock()
}
}
}
}
}
func (r *Registry) getOrCreateContainer(pid uint32) *Container {
if c, seen := r.containersByPid[pid]; c != nil {
return c
} else if seen { // ignored
return nil
}
cg, err := proc.ReadCgroup(pid)
if err != nil {
if !common.IsNotExist(err) {
klog.Warningln("failed to read proc cgroup:", err)
}
return nil
}
if c := r.containersByCgroupId[cg.Id]; c != nil {
r.containersByPid[pid] = c
return c
}
if cg.ContainerType == cgroup.ContainerTypeSandbox {
cmdline := proc.GetCmdline(pid)
parts := bytes.Split(cmdline, []byte{0})
if len(parts) > 0 {
cmd := parts[0]
lastArg := parts[len(parts)-1]
if (bytes.HasSuffix(cmd, []byte("runsc-sandbox")) || bytes.HasSuffix(cmd, []byte("runsc"))) && containerIdRegexp.Match(lastArg) {
cg.ContainerId = string(lastArg)
}
}
}
md, err := getContainerMetadata(cg)
if err != nil {
klog.Warningf("failed to get container metadata for pid %d -> %s: %s", pid, cg.Id, err)
return nil
}
id := calcId(cg, md)
klog.Infof("calculated container id %d -> %s -> %s", pid, cg.Id, id)
if id == "" {
if cg.Id == "/init.scope" && pid != 1 {
klog.InfoS("ignoring without persisting", "cg", cg.Id, "pid", pid)
} else {
klog.InfoS("ignoring", "cg", cg.Id, "pid", pid)
r.containersByPid[pid] = nil
}
return nil
}
if c := r.containersById[id]; c != nil {
klog.Warningln("id conflict:", id)
if cg.CreatedAt().After(c.cgroup.CreatedAt()) {
c.cgroup = cg
c.metadata = md
c.runLogParser("")
if c.nsConntrack != nil {
_ = c.nsConntrack.Close()
c.nsConntrack = nil
}
}
r.containersByPid[pid] = c
r.containersByCgroupId[cg.Id] = c
return c
}
c, err := NewContainer(id, cg, md, r.hostConntrack, pid)
if err != nil {
klog.Warningf("failed to create container pid=%d cg=%s id=%s: %s", pid, cg.Id, id, err)
return nil
}
klog.InfoS("detected a new container", "pid", pid, "cg", cg.Id, "id", id)
if err := prometheus.WrapRegistererWith(prometheus.Labels{"container_id": string(id)}, r.reg).Register(c); err != nil {
klog.Warningln("failed to register container:", err)
return nil
}
r.containersByPid[pid] = c
r.containersByCgroupId[cg.Id] = c
r.containersById[id] = c
return c
}
func calcId(cg *cgroup.Cgroup, md *ContainerMetadata) ContainerID {
if cg.ContainerType == cgroup.ContainerTypeSystemdService {
if strings.HasPrefix(cg.ContainerId, "/system.slice/crio-conmon-") {
return ""
}
return ContainerID(cg.ContainerId)
}
switch cg.ContainerType {
case cgroup.ContainerTypeDocker, cgroup.ContainerTypeContainerd, cgroup.ContainerTypeSandbox, cgroup.ContainerTypeCrio:
default:
return ""
}
if cg.ContainerId == "" {
return ""
}
if md.labels["io.kubernetes.pod.name"] != "" {
pod := md.labels["io.kubernetes.pod.name"]
namespace := md.labels["io.kubernetes.pod.namespace"]
name := md.labels["io.kubernetes.container.name"]
if cg.ContainerType == cgroup.ContainerTypeSandbox {
name = "sandbox"
}
if name == "" || name == "POD" { // skip pause containers
return ""
}
return ContainerID(fmt.Sprintf("/k8s/%s/%s/%s", namespace, pod, name))
}
if taskNameParts := strings.SplitN(md.labels["com.docker.swarm.task.name"], ".", 3); len(taskNameParts) == 3 {
namespace := md.labels["com.docker.stack.namespace"]
service := md.labels["com.docker.swarm.service.name"]
if namespace != "" {
service = strings.TrimPrefix(service, namespace+"_")
}
if namespace == "" {
namespace = "_"
}
return ContainerID(fmt.Sprintf("/swarm/%s/%s/%s", namespace, service, taskNameParts[1]))
}
if md.env != nil {
allocId := md.env["NOMAD_ALLOC_ID"]
group := md.env["NOMAD_GROUP_NAME"]
job := md.env["NOMAD_JOB_NAME"]
namespace := md.env["NOMAD_NAMESPACE"]
task := md.env["NOMAD_TASK_NAME"]
if allocId != "" && group != "" && job != "" && namespace != "" && task != "" {
return ContainerID(fmt.Sprintf("/nomad/%s/%s/%s/%s/%s", namespace, job, group, allocId, task))
}
}
if md.name == "" { // should be "pure" dockerd container here
klog.Warningln("empty dockerd container name for:", cg.ContainerId)
return ""
}
return ContainerID("/docker/" + md.name)
}
func getContainerMetadata(cg *cgroup.Cgroup) (*ContainerMetadata, error) {
switch cg.ContainerType {
case cgroup.ContainerTypeDocker, cgroup.ContainerTypeContainerd, cgroup.ContainerTypeSandbox, cgroup.ContainerTypeCrio:
default:
return &ContainerMetadata{}, nil
}
if cg.ContainerId == "" {
return &ContainerMetadata{}, nil
}
if cg.ContainerType == cgroup.ContainerTypeCrio {
return CrioInspect(cg.ContainerId)
}
var dockerdErr error
if dockerdClient != nil {
md, err := DockerdInspect(cg.ContainerId)
if err == nil {
return md, nil
}
dockerdErr = err
}
var containerdErr error
if containerdClient != nil {
md, err := ContainerdInspect(cg.ContainerId)
if err == nil {
return md, nil
}
containerdErr = err
}
return nil, fmt.Errorf("failed to interact with dockerd (%s) or with containerd (%s)", dockerdErr, containerdErr)
}