From c95c8a248459ebc7aa5d1c7708449f4848ebfc8b Mon Sep 17 00:00:00 2001 From: Sven Rebhan Date: Thu, 19 May 2022 18:07:35 +0200 Subject: [PATCH] fix: Improve slab testing without sudo. --- plugins/inputs/slab/slab.go | 25 ++++++++------- plugins/inputs/slab/slab_test.go | 52 ++++---------------------------- 2 files changed, 18 insertions(+), 59 deletions(-) diff --git a/plugins/inputs/slab/slab.go b/plugins/inputs/slab/slab.go index a18513df98789..6ad0e52b9cee0 100644 --- a/plugins/inputs/slab/slab.go +++ b/plugins/inputs/slab/slab.go @@ -20,18 +20,11 @@ import ( "github.com/influxdata/telegraf/plugins/inputs" ) -func init() { - inputs.Add("slab", func() telegraf.Input { - return &SlabStats{ - statFile: path.Join(getHostProc(), "/slabinfo"), - } - }) -} - type SlabStats struct { Log telegraf.Logger `toml:"-"` statFile string + useSudo bool } func (ss *SlabStats) Init() error { @@ -91,16 +84,13 @@ func (ss *SlabStats) getSlabStats() (map[string]interface{}, error) { func (ss *SlabStats) runCmd(cmd string, args []string) ([]byte, error) { execCmd := exec.Command(cmd, args...) - if os.Geteuid() != 0 { + if ss.useSudo { execCmd = exec.Command("sudo", append([]string{"-n", cmd}, args...)...) } out, err := internal.StdOutputTimeout(execCmd, 5*time.Second) if err != nil { - return nil, fmt.Errorf( - "failed to run command %s: %s - %s", - strings.Join(execCmd.Args, " "), err, string(out), - ) + return nil, fmt.Errorf("failed to run command %s: %s - %v", execCmd.Args, err, out) } return out, nil @@ -117,3 +107,12 @@ func getHostProc() string { func normalizeName(name string) string { return strings.ReplaceAll(strings.ToLower(name), "-", "_") + "_size" } + +func init() { + inputs.Add("slab", func() telegraf.Input { + return &SlabStats{ + statFile: path.Join(getHostProc(), "slabinfo"), + useSudo: true, + } + }) +} diff --git a/plugins/inputs/slab/slab_test.go b/plugins/inputs/slab/slab_test.go index e5eda8f13666b..a4b26e6e63162 100644 --- a/plugins/inputs/slab/slab_test.go +++ b/plugins/inputs/slab/slab_test.go @@ -4,37 +4,22 @@ package slab import ( - "os" + "path" "testing" - "github.com/influxdata/telegraf/testutil" "github.com/stretchr/testify/require" -) - -func makeFakeStatFile(content []byte) string { - tmpfile, err := os.CreateTemp("", "slab_test") - if err != nil { - panic(err) - } - if _, err := tmpfile.Write(content); err != nil { - panic(err) - } - if err := tmpfile.Close(); err != nil { - panic(err) - } - - return tmpfile.Name() -} + "github.com/influxdata/telegraf/testutil" +) func TestSlab(t *testing.T) { slabStats := SlabStats{ - statFile: makeFakeStatFile([]byte(procSlabInfo)), + statFile: path.Join("testdata", "slabinfo"), + useSudo: false, } var acc testutil.Accumulator - err := acc.GatherError(slabStats.Gather) - require.NoError(t, err) + require.NoError(t, slabStats.Gather(&acc)) fields := map[string]interface{}{ "ext4_allocation_context_size": int(16384), @@ -62,28 +47,3 @@ func TestSlab(t *testing.T) { acc.AssertContainsFields(t, "slab", fields) } - -var procSlabInfo = `slabinfo - version: 2.1 -# name : tunables : slabdata -ext4_inode_cache 480 480 1024 32 8 : tunables 0 0 0 : slabdata 15 15 0 -ext4_xattr 0 0 88 46 1 : tunables 0 0 0 : slabdata 0 0 0 -ext4_free_data 0 0 64 64 1 : tunables 0 0 0 : slabdata 0 0 0 -ext4_allocation_context 128 128 128 32 1 : tunables 0 0 0 : slabdata 4 4 0 -ext4_io_end 56 56 72 56 1 : tunables 0 0 0 : slabdata 1 1 0 -ext4_extent_status 204 204 40 102 1 : tunables 0 0 0 : slabdata 2 2 0 -kmalloc-8192 106 128 8192 4 8 : tunables 0 0 0 : slabdata 32 32 0 -kmalloc-4096 486 576 4096 8 8 : tunables 0 0 0 : slabdata 72 72 0 -kmalloc-2048 1338 1616 2048 16 8 : tunables 0 0 0 : slabdata 101 101 0 -kmalloc-1024 155845 234304 1024 32 8 : tunables 0 0 0 : slabdata 7329 7329 0 -kmalloc-512 18995 80928 512 32 4 : tunables 0 0 0 : slabdata 2529 2529 0 -kmalloc-256 16366 21184 256 32 2 : tunables 0 0 0 : slabdata 662 662 0 -kmalloc-192 18835 20916 192 21 1 : tunables 0 0 0 : slabdata 996 996 0 -kmalloc-128 23600 43648 128 32 1 : tunables 0 0 0 : slabdata 1364 1364 0 -kmalloc-96 95106 128940 96 42 1 : tunables 0 0 0 : slabdata 3070 3070 0 -kmalloc-64 82432 133376 64 64 1 : tunables 0 0 0 : slabdata 2084 2084 0 -kmalloc-32 78477 114304 32 128 1 : tunables 0 0 0 : slabdata 893 893 0 -kmalloc-16 885605 1062656 16 256 1 : tunables 0 0 0 : slabdata 4151 4151 0 -kmalloc-8 28672 28672 8 512 1 : tunables 0 0 0 : slabdata 56 56 0 -kmem_cache_node 576 576 64 64 1 : tunables 0 0 0 : slabdata 9 9 0 -kmem_cache 320 320 256 32 2 : tunables 0 0 0 : slabdata 10 10 0 -`