2015-10-04 05:09:18 +00:00
|
|
|
package procstat
|
|
|
|
|
|
|
|
import (
|
2017-03-17 23:49:11 +00:00
|
|
|
"fmt"
|
2017-11-13 22:45:31 +00:00
|
|
|
"io/ioutil"
|
2015-10-04 05:09:18 +00:00
|
|
|
"os"
|
2017-11-13 22:45:31 +00:00
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
2018-02-01 23:14:27 +00:00
|
|
|
"runtime"
|
2017-11-13 22:45:31 +00:00
|
|
|
"strings"
|
2015-10-04 05:09:18 +00:00
|
|
|
"testing"
|
2017-03-17 23:49:11 +00:00
|
|
|
"time"
|
2015-10-05 22:45:40 +00:00
|
|
|
|
2017-03-17 23:49:11 +00:00
|
|
|
"github.com/influxdata/telegraf/testutil"
|
|
|
|
"github.com/shirou/gopsutil/cpu"
|
2017-03-06 15:59:36 +00:00
|
|
|
"github.com/shirou/gopsutil/process"
|
2015-10-05 22:45:40 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2015-10-04 05:09:18 +00:00
|
|
|
)
|
|
|
|
|
2017-11-13 22:45:31 +00:00
|
|
|
func init() {
|
|
|
|
execCommand = mockExecCommand
|
|
|
|
}
|
|
|
|
func mockExecCommand(arg0 string, args ...string) *exec.Cmd {
|
|
|
|
args = append([]string{"-test.run=TestMockExecCommand", "--", arg0}, args...)
|
|
|
|
cmd := exec.Command(os.Args[0], args...)
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
func TestMockExecCommand(t *testing.T) {
|
|
|
|
var cmd []string
|
|
|
|
for _, arg := range os.Args {
|
|
|
|
if string(arg) == "--" {
|
|
|
|
cmd = []string{}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if cmd == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
cmd = append(cmd, string(arg))
|
|
|
|
}
|
|
|
|
if cmd == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
cmdline := strings.Join(cmd, " ")
|
|
|
|
|
|
|
|
if cmdline == "systemctl show TestGather_systemdUnitPIDs" {
|
|
|
|
fmt.Printf(`PIDFile=
|
|
|
|
GuessMainPID=yes
|
|
|
|
MainPID=11408
|
|
|
|
ControlPID=0
|
|
|
|
ExecMainPID=11408
|
|
|
|
`)
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("command not found\n")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2017-03-17 23:49:11 +00:00
|
|
|
type testPgrep struct {
|
|
|
|
pids []PID
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func pidFinder(pids []PID, err error) func() (PIDFinder, error) {
|
|
|
|
return func() (PIDFinder, error) {
|
|
|
|
return &testPgrep{
|
|
|
|
pids: pids,
|
|
|
|
err: err,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pg *testPgrep) PidFile(path string) ([]PID, error) {
|
|
|
|
return pg.pids, pg.err
|
|
|
|
}
|
|
|
|
|
2019-04-11 01:42:38 +00:00
|
|
|
func (p *testProc) Cmdline() (string, error) {
|
|
|
|
return "test_proc", nil
|
|
|
|
}
|
|
|
|
|
2017-03-17 23:49:11 +00:00
|
|
|
func (pg *testPgrep) Pattern(pattern string) ([]PID, error) {
|
|
|
|
return pg.pids, pg.err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pg *testPgrep) Uid(user string) ([]PID, error) {
|
|
|
|
return pg.pids, pg.err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pg *testPgrep) FullPattern(pattern string) ([]PID, error) {
|
|
|
|
return pg.pids, pg.err
|
|
|
|
}
|
|
|
|
|
|
|
|
type testProc struct {
|
|
|
|
pid PID
|
|
|
|
tags map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
func newTestProc(pid PID) (Process, error) {
|
|
|
|
proc := &testProc{
|
|
|
|
tags: make(map[string]string),
|
|
|
|
}
|
|
|
|
return proc, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *testProc) PID() PID {
|
|
|
|
return p.pid
|
|
|
|
}
|
|
|
|
|
2018-07-05 20:56:41 +00:00
|
|
|
func (p *testProc) Username() (string, error) {
|
|
|
|
return "testuser", nil
|
|
|
|
}
|
|
|
|
|
2017-03-17 23:49:11 +00:00
|
|
|
func (p *testProc) Tags() map[string]string {
|
|
|
|
return p.tags
|
|
|
|
}
|
|
|
|
|
2019-04-26 00:21:02 +00:00
|
|
|
func (p *testProc) PageFaults() (*process.PageFaultsStat, error) {
|
|
|
|
return &process.PageFaultsStat{}, nil
|
|
|
|
}
|
|
|
|
|
2017-03-17 23:49:11 +00:00
|
|
|
func (p *testProc) IOCounters() (*process.IOCountersStat, error) {
|
|
|
|
return &process.IOCountersStat{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *testProc) MemoryInfo() (*process.MemoryInfoStat, error) {
|
|
|
|
return &process.MemoryInfoStat{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *testProc) Name() (string, error) {
|
|
|
|
return "test_proc", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *testProc) NumCtxSwitches() (*process.NumCtxSwitchesStat, error) {
|
|
|
|
return &process.NumCtxSwitchesStat{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *testProc) NumFDs() (int32, error) {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *testProc) NumThreads() (int32, error) {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *testProc) Percent(interval time.Duration) (float64, error) {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
2019-08-14 17:17:38 +00:00
|
|
|
func (p *testProc) MemoryPercent() (float32, error) {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
2017-03-17 23:49:11 +00:00
|
|
|
func (p *testProc) Times() (*cpu.TimesStat, error) {
|
|
|
|
return &cpu.TimesStat{}, nil
|
|
|
|
}
|
|
|
|
|
2017-09-15 18:16:44 +00:00
|
|
|
func (p *testProc) RlimitUsage(gatherUsage bool) ([]process.RlimitStat, error) {
|
|
|
|
return []process.RlimitStat{}, nil
|
|
|
|
}
|
|
|
|
|
2017-03-17 23:49:11 +00:00
|
|
|
var pid PID = PID(42)
|
|
|
|
var exe string = "foo"
|
|
|
|
|
|
|
|
func TestGather_CreateProcessErrorOk(t *testing.T) {
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
|
|
|
|
p := Procstat{
|
|
|
|
Exe: exe,
|
|
|
|
createPIDFinder: pidFinder([]PID{pid}, nil),
|
|
|
|
createProcess: func(PID) (Process, error) {
|
|
|
|
return nil, fmt.Errorf("createProcess error")
|
|
|
|
},
|
|
|
|
}
|
2017-04-24 18:13:26 +00:00
|
|
|
require.NoError(t, acc.GatherError(p.Gather))
|
2017-03-17 23:49:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGather_CreatePIDFinderError(t *testing.T) {
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
|
|
|
|
p := Procstat{
|
|
|
|
createPIDFinder: func() (PIDFinder, error) {
|
|
|
|
return nil, fmt.Errorf("createPIDFinder error")
|
|
|
|
},
|
|
|
|
createProcess: newTestProc,
|
|
|
|
}
|
2017-04-24 18:13:26 +00:00
|
|
|
require.Error(t, acc.GatherError(p.Gather))
|
2017-03-17 23:49:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGather_ProcessName(t *testing.T) {
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
|
|
|
|
p := Procstat{
|
|
|
|
Exe: exe,
|
|
|
|
ProcessName: "custom_name",
|
|
|
|
createPIDFinder: pidFinder([]PID{pid}, nil),
|
|
|
|
createProcess: newTestProc,
|
|
|
|
}
|
2017-04-24 18:13:26 +00:00
|
|
|
require.NoError(t, acc.GatherError(p.Gather))
|
2017-03-17 23:49:11 +00:00
|
|
|
|
|
|
|
assert.Equal(t, "custom_name", acc.TagValue("procstat", "process_name"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGather_NoProcessNameUsesReal(t *testing.T) {
|
2015-10-04 05:09:18 +00:00
|
|
|
var acc testutil.Accumulator
|
2017-03-17 23:49:11 +00:00
|
|
|
pid := PID(os.Getpid())
|
|
|
|
|
2015-10-04 05:09:18 +00:00
|
|
|
p := Procstat{
|
2017-03-17 23:49:11 +00:00
|
|
|
Exe: exe,
|
|
|
|
createPIDFinder: pidFinder([]PID{pid}, nil),
|
|
|
|
createProcess: newTestProc,
|
2015-10-04 05:09:18 +00:00
|
|
|
}
|
2017-04-24 18:13:26 +00:00
|
|
|
require.NoError(t, acc.GatherError(p.Gather))
|
2017-03-17 23:49:11 +00:00
|
|
|
|
|
|
|
assert.True(t, acc.HasTag("procstat", "process_name"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGather_NoPidTag(t *testing.T) {
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
|
|
|
|
p := Procstat{
|
|
|
|
Exe: exe,
|
|
|
|
createPIDFinder: pidFinder([]PID{pid}, nil),
|
|
|
|
createProcess: newTestProc,
|
|
|
|
}
|
2017-04-24 18:13:26 +00:00
|
|
|
require.NoError(t, acc.GatherError(p.Gather))
|
2017-03-17 23:49:11 +00:00
|
|
|
assert.True(t, acc.HasInt32Field("procstat", "pid"))
|
|
|
|
assert.False(t, acc.HasTag("procstat", "pid"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGather_PidTag(t *testing.T) {
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
|
|
|
|
p := Procstat{
|
|
|
|
Exe: exe,
|
|
|
|
PidTag: true,
|
|
|
|
createPIDFinder: pidFinder([]PID{pid}, nil),
|
|
|
|
createProcess: newTestProc,
|
|
|
|
}
|
2017-04-24 18:13:26 +00:00
|
|
|
require.NoError(t, acc.GatherError(p.Gather))
|
2017-03-17 23:49:11 +00:00
|
|
|
assert.Equal(t, "42", acc.TagValue("procstat", "pid"))
|
|
|
|
assert.False(t, acc.HasInt32Field("procstat", "pid"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGather_Prefix(t *testing.T) {
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
|
|
|
|
p := Procstat{
|
|
|
|
Exe: exe,
|
|
|
|
Prefix: "custom_prefix",
|
|
|
|
createPIDFinder: pidFinder([]PID{pid}, nil),
|
|
|
|
createProcess: newTestProc,
|
|
|
|
}
|
2017-04-24 18:13:26 +00:00
|
|
|
require.NoError(t, acc.GatherError(p.Gather))
|
2017-03-17 23:49:11 +00:00
|
|
|
assert.True(t, acc.HasInt32Field("procstat", "custom_prefix_num_fds"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGather_Exe(t *testing.T) {
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
|
|
|
|
p := Procstat{
|
|
|
|
Exe: exe,
|
|
|
|
createPIDFinder: pidFinder([]PID{pid}, nil),
|
|
|
|
createProcess: newTestProc,
|
|
|
|
}
|
2017-04-24 18:13:26 +00:00
|
|
|
require.NoError(t, acc.GatherError(p.Gather))
|
2017-03-17 23:49:11 +00:00
|
|
|
|
|
|
|
assert.Equal(t, exe, acc.TagValue("procstat", "exe"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGather_User(t *testing.T) {
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
user := "ada"
|
|
|
|
|
|
|
|
p := Procstat{
|
|
|
|
User: user,
|
|
|
|
createPIDFinder: pidFinder([]PID{pid}, nil),
|
|
|
|
createProcess: newTestProc,
|
|
|
|
}
|
2017-04-24 18:13:26 +00:00
|
|
|
require.NoError(t, acc.GatherError(p.Gather))
|
2017-03-17 23:49:11 +00:00
|
|
|
|
|
|
|
assert.Equal(t, user, acc.TagValue("procstat", "user"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGather_Pattern(t *testing.T) {
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
pattern := "foo"
|
|
|
|
|
|
|
|
p := Procstat{
|
|
|
|
Pattern: pattern,
|
|
|
|
createPIDFinder: pidFinder([]PID{pid}, nil),
|
|
|
|
createProcess: newTestProc,
|
|
|
|
}
|
2017-04-24 18:13:26 +00:00
|
|
|
require.NoError(t, acc.GatherError(p.Gather))
|
2017-03-17 23:49:11 +00:00
|
|
|
|
|
|
|
assert.Equal(t, pattern, acc.TagValue("procstat", "pattern"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGather_MissingPidMethod(t *testing.T) {
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
|
|
|
|
p := Procstat{
|
|
|
|
createPIDFinder: pidFinder([]PID{pid}, nil),
|
|
|
|
createProcess: newTestProc,
|
|
|
|
}
|
2017-04-24 18:13:26 +00:00
|
|
|
require.Error(t, acc.GatherError(p.Gather))
|
2017-03-17 23:49:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGather_PidFile(t *testing.T) {
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
pidfile := "/path/to/pidfile"
|
|
|
|
|
|
|
|
p := Procstat{
|
|
|
|
PidFile: pidfile,
|
|
|
|
createPIDFinder: pidFinder([]PID{pid}, nil),
|
|
|
|
createProcess: newTestProc,
|
|
|
|
}
|
2017-04-24 18:13:26 +00:00
|
|
|
require.NoError(t, acc.GatherError(p.Gather))
|
2017-03-17 23:49:11 +00:00
|
|
|
|
|
|
|
assert.Equal(t, pidfile, acc.TagValue("procstat", "pidfile"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGather_PercentFirstPass(t *testing.T) {
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
pid := PID(os.Getpid())
|
|
|
|
|
|
|
|
p := Procstat{
|
|
|
|
Pattern: "foo",
|
|
|
|
PidTag: true,
|
|
|
|
createPIDFinder: pidFinder([]PID{pid}, nil),
|
|
|
|
createProcess: NewProc,
|
|
|
|
}
|
2017-04-24 18:13:26 +00:00
|
|
|
require.NoError(t, acc.GatherError(p.Gather))
|
2017-03-17 23:49:11 +00:00
|
|
|
|
|
|
|
assert.True(t, acc.HasFloatField("procstat", "cpu_time_user"))
|
|
|
|
assert.False(t, acc.HasFloatField("procstat", "cpu_usage"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGather_PercentSecondPass(t *testing.T) {
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
pid := PID(os.Getpid())
|
|
|
|
|
|
|
|
p := Procstat{
|
|
|
|
Pattern: "foo",
|
|
|
|
PidTag: true,
|
|
|
|
createPIDFinder: pidFinder([]PID{pid}, nil),
|
|
|
|
createProcess: NewProc,
|
|
|
|
}
|
2017-04-24 18:13:26 +00:00
|
|
|
require.NoError(t, acc.GatherError(p.Gather))
|
|
|
|
require.NoError(t, acc.GatherError(p.Gather))
|
2017-03-17 23:49:11 +00:00
|
|
|
|
|
|
|
assert.True(t, acc.HasFloatField("procstat", "cpu_time_user"))
|
|
|
|
assert.True(t, acc.HasFloatField("procstat", "cpu_usage"))
|
2015-10-04 05:09:18 +00:00
|
|
|
}
|
2017-11-13 22:45:31 +00:00
|
|
|
|
|
|
|
func TestGather_systemdUnitPIDs(t *testing.T) {
|
|
|
|
p := Procstat{
|
|
|
|
createPIDFinder: pidFinder([]PID{}, nil),
|
|
|
|
SystemdUnit: "TestGather_systemdUnitPIDs",
|
|
|
|
}
|
2018-06-19 18:47:13 +00:00
|
|
|
var acc testutil.Accumulator
|
|
|
|
pids, tags, err := p.findPids(&acc)
|
2017-11-13 22:45:31 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, []PID{11408}, pids)
|
|
|
|
assert.Equal(t, "TestGather_systemdUnitPIDs", tags["systemd_unit"])
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGather_cgroupPIDs(t *testing.T) {
|
2018-02-01 23:14:27 +00:00
|
|
|
//no cgroups in windows
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
t.Skip("no cgroups in windows")
|
|
|
|
}
|
2017-11-13 22:45:31 +00:00
|
|
|
td, err := ioutil.TempDir("", "")
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
err = ioutil.WriteFile(filepath.Join(td, "cgroup.procs"), []byte("1234\n5678\n"), 0644)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
p := Procstat{
|
|
|
|
createPIDFinder: pidFinder([]PID{}, nil),
|
|
|
|
CGroup: td,
|
|
|
|
}
|
2018-06-19 18:47:13 +00:00
|
|
|
var acc testutil.Accumulator
|
|
|
|
pids, tags, err := p.findPids(&acc)
|
2017-11-13 22:45:31 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, []PID{1234, 5678}, pids)
|
|
|
|
assert.Equal(t, td, tags["cgroup"])
|
|
|
|
}
|
2018-06-19 18:47:13 +00:00
|
|
|
|
|
|
|
func TestProcstatLookupMetric(t *testing.T) {
|
|
|
|
p := Procstat{
|
|
|
|
createPIDFinder: pidFinder([]PID{543}, nil),
|
|
|
|
Exe: "-Gsys",
|
|
|
|
}
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
err := acc.GatherError(p.Gather)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, len(p.procs)+1, len(acc.Metrics))
|
|
|
|
}
|