Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Improve slab testing without sudo. #11151

Merged
merged 3 commits into from
May 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 12 additions & 13 deletions plugins/inputs/slab/slab.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 os.Geteuid() != 0 && 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
Expand All @@ -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,
}
})
}
52 changes: 6 additions & 46 deletions plugins/inputs/slab/slab_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -62,28 +47,3 @@ func TestSlab(t *testing.T) {

acc.AssertContainsFields(t, "slab", fields)
}

var procSlabInfo = `slabinfo - version: 2.1
# name <active_objs> <num_objs> <objsize> <objperslab> <pagesperslab> : tunables <limit> <batchcount> <sharedfactor> : slabdata <active_slabs> <num_slabs> <sharedavail>
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
`
23 changes: 23 additions & 0 deletions plugins/inputs/slab/testdata/slabinfo
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
slabinfo - version: 2.1
# name <active_objs> <num_objs> <objsize> <objperslab> <pagesperslab> : tunables <limit> <batchcount> <sharedfactor> : slabdata <active_slabs> <num_slabs> <sharedavail>
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