diff --git a/libbeat/cmd/instance/metrics.go b/libbeat/cmd/instance/metrics.go index 910e08a8b67..d2d0f53df4a 100644 --- a/libbeat/cmd/instance/metrics.go +++ b/libbeat/cmd/instance/metrics.go @@ -4,7 +4,6 @@ package instance import ( "fmt" - "os" "runtime" "github.com/elastic/beats/libbeat/logp" @@ -65,8 +64,12 @@ func reportMemStats(m monitoring.Mode, V monitoring.Visitor) { } func getRSSSize() (uint64, error) { - beatPID := os.Getpid() - state, err := beatProcessStats.GetOne(beatPID) + pid, err := process.GetSelfPid() + if err != nil { + return 0, fmt.Errorf("error getting PID for self process: %v", err) + } + + state, err := beatProcessStats.GetOne(pid) if err != nil { return 0, fmt.Errorf("error retrieving process stats: %v", err) } @@ -121,8 +124,12 @@ func reportBeatCPU(_ monitoring.Mode, V monitoring.Visitor) { } func getCPUUsage() (float64, *process.Ticks, error) { - beatPID := os.Getpid() - state, err := beatProcessStats.GetOne(beatPID) + pid, err := process.GetSelfPid() + if err != nil { + return 0.0, nil, fmt.Errorf("error getting PID for self process: %v", err) + } + + state, err := beatProcessStats.GetOne(pid) if err != nil { return 0.0, nil, fmt.Errorf("error retrieving process stats: %v", err) } diff --git a/libbeat/metric/system/process/process.go b/libbeat/metric/system/process/process.go index 7d89125b942..66f3387c586 100644 --- a/libbeat/metric/system/process/process.go +++ b/libbeat/metric/system/process/process.go @@ -46,7 +46,7 @@ type Process struct { cpuTotalPctNorm float64 } -// Stats stores the stats of preocesses on the host. +// Stats stores the stats of processes on the host. type Stats struct { Procs []string ProcsMap ProcsMap diff --git a/libbeat/metric/system/process/process_linux.go b/libbeat/metric/system/process/process_linux.go new file mode 100644 index 00000000000..9400a43d834 --- /dev/null +++ b/libbeat/metric/system/process/process_linux.go @@ -0,0 +1,20 @@ +package process + +import ( + "os" + "path" + "strconv" + + "github.com/elastic/gosigar" +) + +// GetSelfPid returns the PID for this process +func GetSelfPid() (int, error) { + pid, err := os.Readlink(path.Join(gosigar.Procd, "self")) + + if err != nil { + return 0, err + } + + return strconv.Atoi(pid) +} diff --git a/libbeat/metric/system/process/process_other.go b/libbeat/metric/system/process/process_other.go new file mode 100644 index 00000000000..f829eac1442 --- /dev/null +++ b/libbeat/metric/system/process/process_other.go @@ -0,0 +1,10 @@ +// +build !linux + +package process + +import "os" + +// GetSelfPid returns the PID for this process +func GetSelfPid() (int, error) { + return os.Getpid(), nil +} diff --git a/libbeat/metric/system/process/process_test.go b/libbeat/metric/system/process/process_test.go index fe989d46a89..4bd6bcf9c6b 100644 --- a/libbeat/metric/system/process/process_test.go +++ b/libbeat/metric/system/process/process_test.go @@ -66,6 +66,13 @@ func TestGetProcess(t *testing.T) { } } +// See https://github.com/elastic/beats/issues/6620 +func TestGetSelfPid(t *testing.T) { + pid, err := GetSelfPid() + assert.NoError(t, err) + assert.Equal(t, os.Getpid(), pid) +} + func TestProcState(t *testing.T) { assert.Equal(t, getProcState('R'), "running") assert.Equal(t, getProcState('S'), "sleeping")