fix: Improve slab testing without sudo. (#11151)
This commit is contained in:
parent
49137dd267
commit
df17df3a8f
|
|
@ -20,18 +20,11 @@ import (
|
||||||
"github.com/influxdata/telegraf/plugins/inputs"
|
"github.com/influxdata/telegraf/plugins/inputs"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
|
||||||
inputs.Add("slab", func() telegraf.Input {
|
|
||||||
return &SlabStats{
|
|
||||||
statFile: path.Join(getHostProc(), "/slabinfo"),
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
type SlabStats struct {
|
type SlabStats struct {
|
||||||
Log telegraf.Logger `toml:"-"`
|
Log telegraf.Logger `toml:"-"`
|
||||||
|
|
||||||
statFile string
|
statFile string
|
||||||
|
useSudo bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ss *SlabStats) Init() error {
|
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) {
|
func (ss *SlabStats) runCmd(cmd string, args []string) ([]byte, error) {
|
||||||
execCmd := exec.Command(cmd, args...)
|
execCmd := exec.Command(cmd, args...)
|
||||||
if os.Geteuid() != 0 {
|
if os.Geteuid() != 0 && ss.useSudo {
|
||||||
execCmd = exec.Command("sudo", append([]string{"-n", cmd}, args...)...)
|
execCmd = exec.Command("sudo", append([]string{"-n", cmd}, args...)...)
|
||||||
}
|
}
|
||||||
|
|
||||||
out, err := internal.StdOutputTimeout(execCmd, 5*time.Second)
|
out, err := internal.StdOutputTimeout(execCmd, 5*time.Second)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf(
|
return nil, fmt.Errorf("failed to run command %s: %s - %v", execCmd.Args, err, out)
|
||||||
"failed to run command %s: %s - %s",
|
|
||||||
strings.Join(execCmd.Args, " "), err, string(out),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return out, nil
|
return out, nil
|
||||||
|
|
@ -117,3 +107,12 @@ func getHostProc() string {
|
||||||
func normalizeName(name string) string {
|
func normalizeName(name string) string {
|
||||||
return strings.ReplaceAll(strings.ToLower(name), "-", "_") + "_size"
|
return strings.ReplaceAll(strings.ToLower(name), "-", "_") + "_size"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
inputs.Add("slab", func() telegraf.Input {
|
||||||
|
return &SlabStats{
|
||||||
|
statFile: path.Join(getHostProc(), "slabinfo"),
|
||||||
|
useSudo: true,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,37 +4,22 @@
|
||||||
package slab
|
package slab
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"path"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/influxdata/telegraf/testutil"
|
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
|
"github.com/influxdata/telegraf/testutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
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()
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestSlab(t *testing.T) {
|
func TestSlab(t *testing.T) {
|
||||||
slabStats := SlabStats{
|
slabStats := SlabStats{
|
||||||
statFile: makeFakeStatFile([]byte(procSlabInfo)),
|
statFile: path.Join("testdata", "slabinfo"),
|
||||||
|
useSudo: false,
|
||||||
}
|
}
|
||||||
|
|
||||||
var acc testutil.Accumulator
|
var acc testutil.Accumulator
|
||||||
err := acc.GatherError(slabStats.Gather)
|
require.NoError(t, slabStats.Gather(&acc))
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
fields := map[string]interface{}{
|
fields := map[string]interface{}{
|
||||||
"ext4_allocation_context_size": int(16384),
|
"ext4_allocation_context_size": int(16384),
|
||||||
|
|
@ -62,28 +47,3 @@ func TestSlab(t *testing.T) {
|
||||||
|
|
||||||
acc.AssertContainsFields(t, "slab", fields)
|
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
|
|
||||||
`
|
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
Loading…
Reference in New Issue