Skip to content

Fix Agent failing to find NGINX process when NGINX is in debug #1162

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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 33 additions & 24 deletions pkg/nginxprocess/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ type Process struct {
func (p *Process) IsWorker() bool { return strings.HasPrefix(p.Cmd, "nginx: worker") }

// IsMaster returns true if the process is a NGINX master process.
func (p *Process) IsMaster() bool { return strings.HasPrefix(p.Cmd, "nginx: master") }
func (p *Process) IsMaster() bool {
return strings.HasPrefix(p.Cmd, "nginx: master") ||
strings.HasPrefix(p.Cmd, "{nginx-debug} nginx: master")
}

// IsShuttingDown returns true if the process is shutting down. This can identify workers that are in the process of a
// graceful shutdown. See [changing NGINX configuration] for more details.
Expand All @@ -53,6 +56,7 @@ type Option interface{ apply(opts *options) }

type optionFunc func(*options)

//nolint:ireturn
func (f optionFunc) apply(o *options) { f(o) }

// WithStatus runs an additional lookup to load the process status.
Expand All @@ -66,39 +70,44 @@ func convert(ctx context.Context, p *process.Process, o options) (*Process, erro
}

name, _ := p.NameWithContext(ctx) // slow: shells out to ps
if name != "nginx" {
if name != "nginx" && name != "nginx-debug" {
return nil, errNotAnNginxProcess
}

cmdLine, _ := p.CmdlineWithContext(ctx) // slow: shells out to ps
// ignore nginx processes in the middle of an upgrade
if !strings.HasPrefix(cmdLine, "nginx:") || strings.Contains(cmdLine, "upgrade") {

if strings.Contains(cmdLine, "upgrade") {
return nil, errNotAnNginxProcess
}

var status string
if o.loadStatus {
flags, _ := p.StatusWithContext(ctx) // slow: shells out to ps
status = strings.Join(flags, " ")
}
if strings.HasPrefix(cmdLine, "nginx:") || strings.HasPrefix(cmdLine, "{nginx-debug} nginx:") {
var status string
if o.loadStatus {
flags, _ := p.StatusWithContext(ctx) // slow: shells out to ps
status = strings.Join(flags, " ")
}

// unconditionally run fast lookups
var created time.Time
if millisSinceEpoch, err := p.CreateTimeWithContext(ctx); err == nil {
created = time.UnixMilli(millisSinceEpoch)
// unconditionally run fast lookups
var created time.Time
if millisSinceEpoch, err := p.CreateTimeWithContext(ctx); err == nil {
created = time.UnixMilli(millisSinceEpoch)
}
ppid, _ := p.PpidWithContext(ctx)
exe, _ := p.ExeWithContext(ctx)

return &Process{
PID: p.Pid,
PPID: ppid,
Name: name,
Cmd: cmdLine,
Created: created,
Status: status,
Exe: exe,
}, ctx.Err()
}
ppid, _ := p.PpidWithContext(ctx)
exe, _ := p.ExeWithContext(ctx)

return &Process{
PID: p.Pid,
PPID: ppid,
Name: name,
Cmd: cmdLine,
Created: created,
Status: status,
Exe: exe,
}, ctx.Err()

return nil, errNotAnNginxProcess
}

// List returns a slice of all NGINX processes. Returns a zero-length slice if no NGINX processes are found.
Expand Down
8 changes: 8 additions & 0 deletions pkg/nginxprocess/process_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@ func TestProcess_IsNginxMaster(t *testing.T) {
cmd: "nginx: cache manager process",
want: false,
},
"Test 5: nginx debug master": {
cmd: "{nginx-debug} nginx: master process /usr/sbin/nginx-debug -g daemon off;",
want: true,
},
"Test 6: nginx debug worker": {
cmd: "{nginx-debug} nginx: worker process;",
want: false,
},
}
for name, tc := range testcases {
t.Run(name, func(t *testing.T) {
Expand Down