-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathprocess.go
423 lines (357 loc) · 13.5 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
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
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//go:build (darwin && cgo) || freebsd || linux || windows || aix
package process
import (
"context"
"errors"
"fmt"
"sort"
"strings"
"time"
psutil "github.com/shirou/gopsutil/v3/process"
"github.com/elastic/elastic-agent-libs/mapstr"
"github.com/elastic/elastic-agent-libs/opt"
"github.com/elastic/elastic-agent-libs/transform/typeconv"
"github.com/elastic/elastic-agent-system-metrics/metric"
"github.com/elastic/elastic-agent-system-metrics/metric/system/network"
"github.com/elastic/elastic-agent-system-metrics/metric/system/resolve"
"github.com/elastic/go-sysinfo"
sysinfotypes "github.com/elastic/go-sysinfo/types"
)
// ListStates is a wrapper that returns a list of processess with only the basic PID info filled out.
func ListStates(hostfs resolve.Resolver) ([]ProcState, error) {
init := Stats{
Hostfs: hostfs,
Procs: []string{".*"},
EnableCgroups: false,
skipExtended: true,
}
err := init.Init()
if err != nil {
return nil, fmt.Errorf("error initializing process collectors: %w", err)
}
// actually fetch the PIDs from the OS-specific code
_, plist, err := init.FetchPids()
if err != nil {
return nil, fmt.Errorf("error gathering PIDs: %w", err)
}
return plist, nil
}
// GetPIDState returns the state of a given PID
// It will return ErrProcNotExist if the process was not found.
func GetPIDState(hostfs resolve.Resolver, pid int) (PidState, error) {
// This library still doesn't have a good cross-platform way to distinguish between "does not eixst" and other process errors.
// This is a fairly difficult problem to solve in a cross-platform way
exists, err := psutil.PidExistsWithContext(context.Background(), int32(pid))
if err != nil {
return "", fmt.Errorf("Error trying to find process: %d: %w", pid, err)
}
if !exists {
return "", ErrProcNotExist
}
// GetInfoForPid will return the smallest possible dataset for a PID
procState, err := GetInfoForPid(hostfs, pid)
if err != nil {
return "", fmt.Errorf("error getting state info for pid %d: %w", pid, err)
}
return procState.State, nil
}
// Get fetches the configured processes and returns a list of formatted events and root ECS fields
func (procStats *Stats) Get() ([]mapstr.M, []mapstr.M, error) {
// If the user hasn't configured any kind of process glob, return
if len(procStats.Procs) == 0 {
return nil, nil, nil
}
// actually fetch the PIDs from the OS-specific code
pidMap, plist, err := procStats.FetchPids()
if err != nil {
return nil, nil, fmt.Errorf("error gathering PIDs: %w", err)
}
// We use this to track processes over time.
procStats.ProcsMap.SetMap(pidMap)
// filter the process list that will be passed down to users
plist = procStats.includeTopProcesses(plist)
// This is a holdover until we migrate this library to metricbeat/internal
// At which point we'll use the memory code there.
var totalPhyMem uint64
if procStats.host != nil {
memStats, err := procStats.host.Memory()
if err != nil {
procStats.logger.Warnf("Getting memory details: %v", err)
} else {
totalPhyMem = memStats.Total
}
}
// Format the list to the MapStr type used by the outputs
procs := make([]mapstr.M, 0, len(plist))
rootEvents := make([]mapstr.M, 0, len(plist))
for _, process := range plist {
process := process
// Add the RSS pct memory first
process.Memory.Rss.Pct = GetProcMemPercentage(process, totalPhyMem)
// Create the root event
rootMap := processRootEvent(&process)
proc, err := procStats.getProcessEvent(&process)
if err != nil {
return nil, nil, fmt.Errorf("error converting process for pid %d: %w", process.Pid.ValueOr(0), err)
}
procs = append(procs, proc)
rootEvents = append(rootEvents, rootMap)
}
return procs, rootEvents, nil
}
// GetOne fetches process data for a given PID if its name matches the regexes provided from the host.
func (procStats *Stats) GetOne(pid int) (mapstr.M, error) {
pidStat, _, err := procStats.pidFill(pid, false)
if err != nil {
return nil, fmt.Errorf("error fetching PID %d: %w", pid, err)
}
procStats.ProcsMap.SetPid(pid, pidStat)
return procStats.getProcessEvent(&pidStat)
}
// GetOneRootEvent is the same as `GetOne()` but it returns an
// event formatted as expected by ECS
func (procStats *Stats) GetOneRootEvent(pid int) (mapstr.M, mapstr.M, error) {
pidStat, _, err := procStats.pidFill(pid, false)
if err != nil {
return nil, nil, fmt.Errorf("error fetching PID %d: %w", pid, err)
}
procStats.ProcsMap.SetPid(pid, pidStat)
procMap, err := procStats.getProcessEvent(&pidStat)
if err != nil {
return nil, nil, fmt.Errorf("error formatting process %d: %w", pid, err)
}
rootMap := processRootEvent(&pidStat)
return procMap, rootMap, err
}
// GetSelf gets process info for the beat itself
// Be advised that if you call this method on a Stats object that was created with an alternate
// `Hostfs` setting, this method will return data for that pid as it exists on that hostfs.
// For example, if called from inside a container with a `hostfs` path for the container host,
// the PID in the ProcState object will be the PID as the host sees it.
func (procStats *Stats) GetSelf() (ProcState, error) {
self, err := GetSelfPid(procStats.Hostfs)
if err != nil {
return ProcState{}, fmt.Errorf("error finding PID: %w", err)
}
pidStat, _, err := procStats.pidFill(self, false)
if err != nil {
return ProcState{}, fmt.Errorf("error fetching PID %d: %w", self, err)
}
procStats.ProcsMap.SetPid(self, pidStat)
return pidStat, nil
}
// pidIter wraps a few lines of generic code that all OS-specific FetchPids() functions must call.
// this also handles the process of adding to the maps/lists in order to limit the code duplication in all the OS implementations
func (procStats *Stats) pidIter(pid int, procMap ProcsMap, proclist []ProcState) (ProcsMap, []ProcState) {
status, saved, err := procStats.pidFill(pid, true)
if err != nil {
if !errors.Is(err, NonFatalErr{}) {
procStats.logger.Debugf("Error fetching PID info for %d, skipping: %s", pid, err)
return procMap, proclist
}
procStats.logger.Debugf("Non fatal error fetching PID some info for %d, metrics are valid, but partial: %s", pid, err)
}
if !saved {
procStats.logger.Debugf("Process name does not match the provided regex; PID=%d; name=%s", pid, status.Name)
return procMap, proclist
}
procMap[pid] = status
proclist = append(proclist, status)
return procMap, proclist
}
// NonFatalErr is returned when there was an error
// collecting metrics, however the metrics already
// gathered and returned are still valid.
// This error can be safely ignored, this will result
// in having partial metrics for a process rather than
// no metrics at all.
//
// It was introduced to allow for partial metrics collection
// on privileged process on Windows.
type NonFatalErr struct {
Err error
}
func (c NonFatalErr) Error() string {
return "Not enough privileges to fetch information: " + c.Err.Error()
}
func (c NonFatalErr) Is(other error) bool {
_, is := other.(NonFatalErr)
return is
}
// pidFill is an entrypoint used by OS-specific code to fill out a pid.
// This in turn calls various OS-specific code to fill out the various bits of PID data
// This is done to minimize the code duplication between different OS implementations
// The second return value will only be false if an event has been filtered out.
func (procStats *Stats) pidFill(pid int, filter bool) (ProcState, bool, error) {
// Fetch proc state so we can get the name for filtering based on user's filter.
// OS-specific entrypoint, get basic info so we can at least run matchProcess
status, err := GetInfoForPid(procStats.Hostfs, pid)
if err != nil {
return status, true, fmt.Errorf("GetInfoForPid: %w", err)
}
if procStats.skipExtended {
return status, true, nil
}
// Some OSes use the cache to avoid expensive system calls,
// cacheCmdLine reads from the cache.
status = procStats.cacheCmdLine(status)
// Filter based on user-supplied func
if filter {
if !procStats.matchProcess(status.Name) {
return status, false, nil
}
}
// If we've passed the filter, continue to fill out the rest of the metrics
status, err = FillPidMetrics(procStats.Hostfs, pid, status, procStats.isWhitelistedEnvVar)
if err != nil {
if !errors.Is(err, NonFatalErr{}) {
return status, true, fmt.Errorf("FillPidMetrics: %w", err)
}
procStats.logger.Debugf("Non-fatal error fetching PID metrics for %d, metrics are valid, but partial: %s", pid, err)
}
if status.CPU.Total.Ticks.Exists() {
status.CPU.Total.Value = opt.FloatWith(metric.Round(float64(status.CPU.Total.Ticks.ValueOr(0))))
}
// postprocess with cgroups and percentages
last, ok := procStats.ProcsMap.GetPid(status.Pid.ValueOr(0))
status.SampleTime = time.Now()
if ok {
status = GetProcCPUPercentage(last, status)
}
if procStats.EnableCgroups {
cgStats, err := procStats.cgroups.GetStatsForPid(status.Pid.ValueOr(0))
if err != nil {
procStats.logger.Debugf("Non-fatal error fetching cgroups metrics for pid %d, metrics are valid but partial: %s", pid, err)
} else {
status.Cgroup = cgStats
if ok {
status.Cgroup.FillPercentages(last.Cgroup, status.SampleTime, last.SampleTime)
}
}
} // end cgroups processor
status, err = FillMetricsRequiringMoreAccess(pid, status)
if err != nil {
return status, true, fmt.Errorf("FillMetricsRequiringMoreAccess: %w", err)
}
// Generate `status.Cmdline` here for compatibility because on Windows
// `status.Args` is set by `FillMetricsRequiringMoreAccess`.
if len(status.Args) > 0 && status.Cmdline == "" {
status.Cmdline = strings.Join(status.Args, " ")
}
// network data
if procStats.EnableNetwork {
procHandle, err := sysinfo.Process(pid)
// treat this as a soft error
if err != nil {
procStats.logger.Debugf("error initializing process handler for pid %d while trying to fetch network data: %w", pid, err)
} else {
procNet, ok := procHandle.(sysinfotypes.NetworkCounters)
if ok {
status.Network, err = procNet.NetworkCounters()
if err != nil {
procStats.logger.Debugf("error fetching network counters for process %d: %w", pid, err)
}
}
}
}
return status, true, nil
}
// cacheCmdLine fills out Env and arg metrics from any stored previous metrics for the pid
func (procStats *Stats) cacheCmdLine(in ProcState) ProcState {
if previousProc, ok := procStats.ProcsMap.GetPid(in.Pid.ValueOr(0)); ok {
if procStats.CacheCmdLine {
in.Args = previousProc.Args
in.Cmdline = previousProc.Cmdline
}
env := previousProc.Env
in.Env = env
}
return in
}
// return a formatted MapStr of the process metrics
func (procStats *Stats) getProcessEvent(process *ProcState) (mapstr.M, error) {
// Remove CPUTicks if needed
if !procStats.CPUTicks {
process.CPU.User.Ticks = opt.NewUintNone()
process.CPU.System.Ticks = opt.NewUintNone()
process.CPU.Total.Ticks = opt.NewUintNone()
}
proc := mapstr.M{}
err := typeconv.Convert(&proc, process)
if procStats.EnableNetwork && process.Network != nil {
proc["network"] = network.MapProcNetCountersWithFilter(process.Network, procStats.NetworkMetrics)
}
return proc, err
}
// matchProcess checks if the provided process name matches any of the process regexes
func (procStats *Stats) matchProcess(name string) bool {
for _, reg := range procStats.procRegexps {
if reg.MatchString(name) {
return true
}
}
return false
}
// includeTopProcesses filters down the metrics based on top CPU or top Memory settings
func (procStats *Stats) includeTopProcesses(processes []ProcState) []ProcState {
if !procStats.IncludeTop.Enabled ||
(procStats.IncludeTop.ByCPU == 0 && procStats.IncludeTop.ByMemory == 0) {
return processes
}
var result []ProcState
if procStats.IncludeTop.ByCPU > 0 {
numProcs := procStats.IncludeTop.ByCPU
if len(processes) < procStats.IncludeTop.ByCPU {
numProcs = len(processes)
}
sort.Slice(processes, func(i, j int) bool {
return processes[i].CPU.Total.Pct.ValueOr(0) > processes[j].CPU.Total.Pct.ValueOr(0)
})
result = append(result, processes[:numProcs]...)
}
if procStats.IncludeTop.ByMemory > 0 {
numProcs := procStats.IncludeTop.ByMemory
if len(processes) < procStats.IncludeTop.ByMemory {
numProcs = len(processes)
}
sort.Slice(processes, func(i, j int) bool {
return processes[i].Memory.Rss.Bytes.ValueOr(0) > processes[j].Memory.Rss.Bytes.ValueOr(0)
})
for _, proc := range processes[:numProcs] {
proc := proc
if !isProcessInSlice(result, &proc) {
result = append(result, proc)
}
}
}
return result
}
// isWhitelistedEnvVar returns true if the given variable name is a match for
// the whitelist. If the whitelist is empty it returns false.
func (procStats *Stats) isWhitelistedEnvVar(varName string) bool {
if len(procStats.envRegexps) == 0 {
return false
}
for _, p := range procStats.envRegexps {
if p.MatchString(varName) {
return true
}
}
return false
}