From dd70722d57d4c4e3ecda1d6ae974b6dcb54709dd Mon Sep 17 00:00:00 2001 From: Evgeniy Sokolov Date: Thu, 22 Mar 2018 22:21:11 +0100 Subject: [PATCH] Fix self metrix when containerised #6620 --- libbeat/cmd/instance/metrics.go | 17 +++++++++++----- libbeat/metric/system/process/process.go | 2 +- .../metric/system/process/process_linux.go | 20 +++++++++++++++++++ .../metric/system/process/process_other.go | 10 ++++++++++ libbeat/metric/system/process/process_test.go | 7 +++++++ 5 files changed, 50 insertions(+), 6 deletions(-) create mode 100644 libbeat/metric/system/process/process_linux.go create mode 100644 libbeat/metric/system/process/process_other.go diff --git a/libbeat/cmd/instance/metrics.go b/libbeat/cmd/instance/metrics.go index 910e08a8b67f..d2d0f53df4a0 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 7d89125b9429..66f3387c5861 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 000000000000..9400a43d8341 --- /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 000000000000..f829eac1442b --- /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 fe989d46a895..4bd6bcf9c6bb 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")