Skip to content

Commit

Permalink
fix: detect nginx upgrade (#145)
Browse files Browse the repository at this point in the history
* fix: detect nginx deletes and use the new binary path

* fix: change log entry to debug

Co-authored-by: Nick Chen <[email protected]>
  • Loading branch information
nickchen and nginx-nickc authored Dec 12, 2022
1 parent 5384bb8 commit b50facc
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 15 deletions.
33 changes: 26 additions & 7 deletions src/core/nginx.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,7 @@ func (n *NginxBinaryType) GetChildProcesses() map[string][]*proto.NginxDetails {
}

func (n *NginxBinaryType) GetNginxIDForProcess(nginxProcess Process) string {
defaulted := false
if nginxProcess.Path == "" {
nginxProcess.Path = defaultToNginxCommandForProcessPath()
defaulted = true
}

defaulted := n.sanitizeProcessPath(&nginxProcess)
info := n.getNginxInfoFrom(nginxProcess.Path)

// reset the process path from the default to what NGINX tells us
Expand All @@ -160,13 +155,21 @@ func (n *NginxBinaryType) GetNginxDetailsByID(nginxID string) *proto.NginxDetail
return n.nginxDetailsMap[nginxID]
}

func (n *NginxBinaryType) GetNginxDetailsFromProcess(nginxProcess Process) *proto.NginxDetails {
func (n *NginxBinaryType) sanitizeProcessPath(nginxProcess *Process) bool {
defaulted := false
if nginxProcess.Path == "" {
nginxProcess.Path = defaultToNginxCommandForProcessPath()
defaulted = true
}
if strings.Contains(nginxProcess.Path, execDeleted) {
log.Debugf("nginx was upgraded (process), using new info")
nginxProcess.Path = sanitizeExecDeletedPath(nginxProcess.Path)
}
return defaulted
}

func (n *NginxBinaryType) GetNginxDetailsFromProcess(nginxProcess Process) *proto.NginxDetails {
defaulted := n.sanitizeProcessPath(&nginxProcess)
info := n.getNginxInfoFrom(nginxProcess.Path)

// reset the process path from the default to what NGINX tells us
Expand Down Expand Up @@ -537,6 +540,10 @@ func (n *NginxBinaryType) getNginxInfoFrom(ngxExe string) *nginxInfo {
if ngxExe == "" {
return &nginxInfo{}
}
if strings.Contains(ngxExe, execDeleted) {
log.Infof("nginx was upgraded, using new info")
ngxExe = sanitizeExecDeletedPath(ngxExe)
}
if info, ok := n.nginxInfoMap[ngxExe]; ok {
stat, err := os.Stat(ngxExe)
if err == nil && stat.ModTime().Equal(info.mtime) {
Expand All @@ -554,6 +561,18 @@ func (n *NginxBinaryType) getNginxInfoFrom(ngxExe string) *nginxInfo {
return info
}

const (
execDeleted = "(deleted)"
)

func sanitizeExecDeletedPath(exe string) string {
firstSpace := strings.Index(exe, execDeleted)
if firstSpace != -1 {
return strings.TrimSpace(exe[0:firstSpace])
}
return strings.TrimSpace(exe)
}

// getNginxInfoFromBuffer -
func (n *NginxBinaryType) getNginxInfoFromBuffer(exePath string, buffer *bytes.Buffer) *nginxInfo {
info := &nginxInfo{}
Expand Down
27 changes: 27 additions & 0 deletions src/core/nginx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -744,3 +744,30 @@ func buildConfig(rootDirectory string) (*proto.NginxConfig, error) {

return nginxConfig, nil
}

// TestNginxBinaryType_sanitizeProcessPath validate correct parsing of the nginx path when nginx binary has been updated.
func TestNginxBinaryType_sanitizeProcessPath(t *testing.T) {
type testDef struct {
desc string
path string
expect string
defaulted bool
}

// no test case for process lookup, that would require running nginx or proc some where
for _, def := range []testDef{
{desc: "deleted path", path: "/usr/sbin/nginx (deleted)", expect: "/usr/sbin/nginx"},
{desc: "no change path", path: "/usr/sbin/nginx", expect: "/usr/sbin/nginx"},
} {
t.Run(def.desc, func(tt *testing.T) {
p := Process{
Path: def.path,
}
binary := NginxBinaryType{
env: &EnvironmentType{},
}
assert.Equal(tt, def.defaulted, binary.sanitizeProcessPath(&p))
assert.Equal(tt, def.expect, p.Path)
})
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b50facc

Please sign in to comment.