-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
process.go
144 lines (120 loc) · 4.17 KB
/
process.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
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
//go:build linux
package modules
import (
"errors"
"io"
"net/http"
"os"
"path/filepath"
"time"
"go.uber.org/atomic"
"github.com/DataDog/datadog-agent/cmd/system-probe/api/module"
"github.com/DataDog/datadog-agent/cmd/system-probe/config"
sysconfigtypes "github.com/DataDog/datadog-agent/cmd/system-probe/config/types"
"github.com/DataDog/datadog-agent/comp/core/workloadmeta"
//nolint:revive // TODO(PROC) Fix revive linter
sysconfig "github.com/DataDog/datadog-agent/cmd/system-probe/config"
"github.com/DataDog/datadog-agent/pkg/process/encoding"
reqEncoding "github.com/DataDog/datadog-agent/pkg/process/encoding/request"
"github.com/DataDog/datadog-agent/pkg/process/procutil"
"github.com/DataDog/datadog-agent/pkg/util/log"
"github.com/DataDog/datadog-agent/pkg/util/optional"
)
// ErrProcessUnsupported is an error type indicating that the process module is not support in the running environment
var ErrProcessUnsupported = errors.New("process module unsupported")
// Process is a module that fetches process level data
var Process = module.Factory{
Name: config.ProcessModule,
ConfigNamespaces: []string{},
Fn: func(cfg *sysconfigtypes.Config, _ optional.Option[workloadmeta.Component]) (module.Module, error) {
log.Infof("Creating process module for: %s", filepath.Base(os.Args[0]))
// we disable returning zero values for stats to reduce parsing work on process-agent side
p := procutil.NewProcessProbe(procutil.WithReturnZeroPermStats(false))
return &process{
probe: p,
lastCheck: atomic.NewInt64(0),
}, nil
},
NeedsEBPF: func() bool {
return false
},
}
var _ module.Module = &process{}
type process struct {
probe procutil.Probe
lastCheck *atomic.Int64
}
// GetStats returns stats for the module
func (t *process) GetStats() map[string]interface{} {
return map[string]interface{}{
"last_check": t.lastCheck.Load(),
}
}
// Register registers endpoints for the module to expose data
func (t *process) Register(httpMux *module.Router) error {
runCounter := atomic.NewUint64(0)
httpMux.HandleFunc("/stats", func(w http.ResponseWriter, req *http.Request) {
start := time.Now()
t.lastCheck.Store(start.Unix())
pids, err := getPids(req)
if err != nil {
log.Errorf("Unable to get PIDs from request: %s", err)
w.WriteHeader(http.StatusBadRequest)
}
stats, err := t.probe.StatsWithPermByPID(pids)
if err != nil {
log.Errorf("unable to retrieve process stats: %s", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
contentType := req.Header.Get("Accept")
marshaler := encoding.GetMarshaler(contentType)
writeStats(w, marshaler, stats)
count := runCounter.Inc()
logProcTracerRequests(count, len(stats), start)
}).Methods("POST")
return nil
}
// Close cleans up the underlying probe object
func (t *process) Close() {
if t.probe != nil {
t.probe.Close()
}
}
func logProcTracerRequests(count uint64, statsCount int, start time.Time) {
args := []interface{}{string(sysconfig.ProcessModule), count, statsCount, time.Since(start)}
msg := "Got request on /%s/stats (count: %d): retrieved %d stats in %s"
switch {
case count <= 5, count%20 == 0:
log.Infof(msg, args...)
default:
log.Debugf(msg, args...)
}
}
func writeStats(w http.ResponseWriter, marshaler encoding.Marshaler, stats map[int32]*procutil.StatsWithPerm) {
buf, err := marshaler.Marshal(stats)
if err != nil {
log.Errorf("unable to marshall stats with type %s: %s", marshaler.ContentType(), err)
w.WriteHeader(500)
return
}
w.Header().Set("Content-type", marshaler.ContentType())
w.Write(buf)
log.Tracef("/%s/stats: %d stats, %d bytes", string(sysconfig.ProcessModule), len(stats), len(buf))
}
func getPids(r *http.Request) ([]int32, error) {
contentType := r.Header.Get("Content-Type")
body, err := io.ReadAll(r.Body)
if err != nil {
return nil, err
}
procReq, err := reqEncoding.GetUnmarshaler(contentType).Unmarshal(body)
if err != nil {
return nil, err
}
return procReq.Pids, nil
}