Skip to content

Commit b29108e

Browse files
authored
Fix Agent failing to find NGINX process when NGINX is in debug (#1162)
1 parent 240cf41 commit b29108e

File tree

2 files changed

+41
-24
lines changed

2 files changed

+41
-24
lines changed

pkg/nginxprocess/process.go

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ type Process struct {
3131
func (p *Process) IsWorker() bool { return strings.HasPrefix(p.Cmd, "nginx: worker") }
3232

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

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

5457
type optionFunc func(*options)
5558

59+
//nolint:ireturn
5660
func (f optionFunc) apply(o *options) { f(o) }
5761

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

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

7377
cmdLine, _ := p.CmdlineWithContext(ctx) // slow: shells out to ps
7478
// ignore nginx processes in the middle of an upgrade
75-
if !strings.HasPrefix(cmdLine, "nginx:") || strings.Contains(cmdLine, "upgrade") {
79+
80+
if strings.Contains(cmdLine, "upgrade") {
7681
return nil, errNotAnNginxProcess
7782
}
7883

79-
var status string
80-
if o.loadStatus {
81-
flags, _ := p.StatusWithContext(ctx) // slow: shells out to ps
82-
status = strings.Join(flags, " ")
83-
}
84+
if strings.HasPrefix(cmdLine, "nginx:") || strings.HasPrefix(cmdLine, "{nginx-debug} nginx:") {
85+
var status string
86+
if o.loadStatus {
87+
flags, _ := p.StatusWithContext(ctx) // slow: shells out to ps
88+
status = strings.Join(flags, " ")
89+
}
8490

85-
// unconditionally run fast lookups
86-
var created time.Time
87-
if millisSinceEpoch, err := p.CreateTimeWithContext(ctx); err == nil {
88-
created = time.UnixMilli(millisSinceEpoch)
91+
// unconditionally run fast lookups
92+
var created time.Time
93+
if millisSinceEpoch, err := p.CreateTimeWithContext(ctx); err == nil {
94+
created = time.UnixMilli(millisSinceEpoch)
95+
}
96+
ppid, _ := p.PpidWithContext(ctx)
97+
exe, _ := p.ExeWithContext(ctx)
98+
99+
return &Process{
100+
PID: p.Pid,
101+
PPID: ppid,
102+
Name: name,
103+
Cmd: cmdLine,
104+
Created: created,
105+
Status: status,
106+
Exe: exe,
107+
}, ctx.Err()
89108
}
90-
ppid, _ := p.PpidWithContext(ctx)
91-
exe, _ := p.ExeWithContext(ctx)
92-
93-
return &Process{
94-
PID: p.Pid,
95-
PPID: ppid,
96-
Name: name,
97-
Cmd: cmdLine,
98-
Created: created,
99-
Status: status,
100-
Exe: exe,
101-
}, ctx.Err()
109+
110+
return nil, errNotAnNginxProcess
102111
}
103112

104113
// List returns a slice of all NGINX processes. Returns a zero-length slice if no NGINX processes are found.

pkg/nginxprocess/process_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,14 @@ func TestProcess_IsNginxMaster(t *testing.T) {
8787
cmd: "nginx: cache manager process",
8888
want: false,
8989
},
90+
"Test 5: nginx debug master": {
91+
cmd: "{nginx-debug} nginx: master process /usr/sbin/nginx-debug -g daemon off;",
92+
want: true,
93+
},
94+
"Test 6: nginx debug worker": {
95+
cmd: "{nginx-debug} nginx: worker process;",
96+
want: false,
97+
},
9098
}
9199
for name, tc := range testcases {
92100
t.Run(name, func(t *testing.T) {

0 commit comments

Comments
 (0)