forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add support for linux sysctl fs metrics (influxdata#2609)
- Loading branch information
1 parent
540f98e
commit 51c99d5
Showing
5 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Linux Sysctl FS Input | ||
|
||
The linux_sysctl_fs input provides Linux system level file metrics. The documentation on these fields can be found at https://www.kernel.org/doc/Documentation/sysctl/fs.txt. | ||
|
||
Example output: | ||
|
||
``` | ||
> linux_sysctl_fs,host=foo dentry-want-pages=0i,file-max=44222i,aio-max-nr=65536i,inode-preshrink-nr=0i,dentry-nr=64340i,dentry-unused-nr=55274i,file-nr=1568i,aio-nr=0i,inode-nr=35952i,inode-free-nr=12957i,dentry-age-limit=45i 1490982022000000000 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package system | ||
|
||
import ( | ||
"bytes" | ||
"io/ioutil" | ||
"strconv" | ||
|
||
"github.com/influxdata/telegraf" | ||
"github.com/influxdata/telegraf/plugins/inputs" | ||
) | ||
|
||
// https://www.kernel.org/doc/Documentation/sysctl/fs.txt | ||
type SysctlFS struct { | ||
path string | ||
} | ||
|
||
var sysctlFSDescription = `Provides Linux sysctl fs metrics` | ||
var sysctlFSSampleConfig = `` | ||
|
||
func (_ SysctlFS) Description() string { | ||
return sysctlFSDescription | ||
} | ||
func (_ SysctlFS) SampleConfig() string { | ||
return sysctlFSSampleConfig | ||
} | ||
|
||
func (sfs *SysctlFS) gatherList(file string, fields map[string]interface{}, fieldNames ...string) error { | ||
bs, err := ioutil.ReadFile(sfs.path + "/" + file) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
bsplit := bytes.Split(bytes.TrimRight(bs, "\n"), []byte{'\t'}) | ||
for i, name := range fieldNames { | ||
if i >= len(bsplit) { | ||
break | ||
} | ||
if name == "" { | ||
continue | ||
} | ||
|
||
v, err := strconv.ParseUint(string(bsplit[i]), 10, 64) | ||
if err != nil { | ||
return err | ||
} | ||
fields[name] = v | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (sfs *SysctlFS) gatherOne(name string, fields map[string]interface{}) error { | ||
bs, err := ioutil.ReadFile(sfs.path + "/" + name) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
v, err := strconv.ParseUint(string(bytes.TrimRight(bs, "\n")), 10, 64) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fields[name] = v | ||
return nil | ||
} | ||
|
||
func (sfs *SysctlFS) Gather(acc telegraf.Accumulator) error { | ||
fields := map[string]interface{}{} | ||
|
||
for _, n := range []string{"aio-nr", "aio-max-nr", "dquot-nr", "dquot-max", "super-nr", "super-max"} { | ||
sfs.gatherOne(n, fields) | ||
} | ||
|
||
sfs.gatherList("inode-state", fields, "inode-nr", "inode-free-nr", "inode-preshrink-nr") | ||
sfs.gatherList("dentry-state", fields, "dentry-nr", "dentry-unused-nr", "dentry-age-limit", "dentry-want-pages") | ||
sfs.gatherList("file-nr", fields, "file-nr", "", "file-max") | ||
|
||
acc.AddFields("linux_sysctl_fs", fields, nil) | ||
return nil | ||
} | ||
|
||
func init() { | ||
inputs.Add("linux_sysctl_fs", func() telegraf.Input { | ||
return &SysctlFS{ | ||
path: "/proc/sys/fs", | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package system | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"testing" | ||
|
||
"github.com/influxdata/telegraf/testutil" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestSysctlFSGather(t *testing.T) { | ||
td, err := ioutil.TempDir("", "") | ||
require.NoError(t, err) | ||
defer os.RemoveAll(td) | ||
|
||
require.NoError(t, ioutil.WriteFile(td+"/aio-nr", []byte("100\n"), 0644)) | ||
require.NoError(t, ioutil.WriteFile(td+"/aio-max-nr", []byte("101\n"), 0644)) | ||
require.NoError(t, ioutil.WriteFile(td+"/super-nr", []byte("102\n"), 0644)) | ||
require.NoError(t, ioutil.WriteFile(td+"/super-max", []byte("103\n"), 0644)) | ||
require.NoError(t, ioutil.WriteFile(td+"/file-nr", []byte("104\t0\t106\n"), 0644)) | ||
require.NoError(t, ioutil.WriteFile(td+"/inode-state", []byte("107\t108\t109\t0\t0\t0\t0\n"), 0644)) | ||
|
||
sfs := &SysctlFS{ | ||
path: td, | ||
} | ||
var acc testutil.Accumulator | ||
require.NoError(t, sfs.Gather(&acc)) | ||
|
||
acc.AssertContainsFields(t, "linux_sysctl_fs", map[string]interface{}{ | ||
"aio-nr": uint64(100), | ||
"aio-max-nr": uint64(101), | ||
"super-nr": uint64(102), | ||
"super-max": uint64(103), | ||
"file-nr": uint64(104), | ||
"file-max": uint64(106), | ||
"inode-nr": uint64(107), | ||
"inode-free-nr": uint64(108), | ||
"inode-preshrink-nr": uint64(109), | ||
}) | ||
} |