Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[add_session_metadata processor] Keep exited processes in the process DB #39173

Merged
merged 12 commits into from
Apr 25, 2024
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ https://github.com/elastic/beats/compare/v8.8.1\...main[Check the HEAD diff]

*Auditbeat*
- Set field types to correctly match ECS in sessionmd processor {issue}38955[38955] {pull}38994[38994]
- Keep process info on exited processes, to avoid failing to enrich events in sessionmd processor {pull}39173[39173]

*Filebeat*

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func New(cfg *cfg.C) (beat.Processor, error) {
}

backfilledPIDs := db.ScrapeProcfs()
logger.Debugf("backfilled %d processes", len(backfilledPIDs))
logger.Infof("backfilled %d processes", len(backfilledPIDs))

var p provider.Provider

Expand All @@ -70,6 +70,9 @@ func New(cfg *cfg.C) (beat.Processor, error) {
if err != nil {
return nil, fmt.Errorf("failed to create provider: %w", err)
}
logger.Info("backend=auto using procfs")
} else {
logger.Info("backend=auto using ebpf")
}
case "ebpf":
p, err = ebpf_provider.NewProvider(ctx, logger, db)
Expand Down
10 changes: 9 additions & 1 deletion x-pack/auditbeat/processors/sessionmd/processdb/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ type Process struct {
Cwd string
Env map[string]string
Filename string
ExitCode int32
}

var (
Expand Down Expand Up @@ -406,9 +407,15 @@ func (db *DB) InsertExit(exit types.ProcessExitEvent) {
defer db.mutex.Unlock()

pid := exit.PIDs.Tgid
delete(db.processes, pid)
mjwolf marked this conversation as resolved.
Show resolved Hide resolved
delete(db.entryLeaders, pid)
delete(db.entryLeaderRelationships, pid)
process, ok := db.processes[pid]
if !ok {
db.logger.Errorf("could not insert exit, pid %v not found in db", pid)
return
}
process.ExitCode = exit.ExitCode
db.processes[pid] = process
}

func interactiveFromTTY(tty types.TTYDev) bool {
Expand Down Expand Up @@ -437,6 +444,7 @@ func fullProcessFromDBProcess(p Process) types.Process {
ret.Thread.Capabilities.Effective, _ = capabilities.FromUint64(p.Creds.CapEffective)
ret.TTY.CharDevice.Major = p.CTTY.Major
ret.TTY.CharDevice.Minor = p.CTTY.Minor
ret.ExitCode = p.ExitCode

return ret
}
Expand Down
2 changes: 1 addition & 1 deletion x-pack/auditbeat/processors/sessionmd/types/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type Process struct {

// The exit code of the process, if this is a termination event.
// The field should be absent if there is no exit code for the event (e.g. process start).
ExitCode *int64 `json:"exit_code,omitempty"`
ExitCode int32 `json:"exit_code,omitempty"`

// Whether the process is connected to an interactive shell.
// Process interactivity is inferred from the processes file descriptors. If the character device for the controlling tty is the same as stdin and stderr for the process, the process is considered interactive.
Expand Down
Loading