From 3bdd53ff81819b57e523604a2dfad652d4401386 Mon Sep 17 00:00:00 2001 From: Matthew Cheung Date: Wed, 14 Jun 2017 14:00:20 -0400 Subject: [PATCH] 2527 Allow collecting of host stats within docker containers by enable procfs and sysfs location overrides --- plugins/inputs/system/LINUX_SYSCTL_FS_README.md | 2 +- plugins/inputs/system/PROCESSES_README.md | 5 +++++ plugins/inputs/system/linux_sysctl_fs.go | 5 ++++- plugins/inputs/system/processes.go | 14 ++++++++++++-- plugins/inputs/system/processes_test.go | 2 ++ 5 files changed, 24 insertions(+), 4 deletions(-) diff --git a/plugins/inputs/system/LINUX_SYSCTL_FS_README.md b/plugins/inputs/system/LINUX_SYSCTL_FS_README.md index e9341c322d05d..2b5a66d667270 100644 --- a/plugins/inputs/system/LINUX_SYSCTL_FS_README.md +++ b/plugins/inputs/system/LINUX_SYSCTL_FS_README.md @@ -6,4 +6,4 @@ 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 -``` +``` \ No newline at end of file diff --git a/plugins/inputs/system/PROCESSES_README.md b/plugins/inputs/system/PROCESSES_README.md index 431e280ff4c9f..508e1fa81015f 100644 --- a/plugins/inputs/system/PROCESSES_README.md +++ b/plugins/inputs/system/PROCESSES_README.md @@ -14,6 +14,11 @@ it requires access to execute `ps`. # no configuration ``` +Another possible configuration is to define an alternative path for resolving the /proc location. +Using the environment variable `HOST_PROC` the plugin will retrieve process information from the specified location. + +`docker run -v /proc:/rootfs/proc:ro -e HOST_PROC=/rootfs/proc` + ### Measurements & Fields: - processes diff --git a/plugins/inputs/system/linux_sysctl_fs.go b/plugins/inputs/system/linux_sysctl_fs.go index 93e426e759af6..1983e316c5717 100644 --- a/plugins/inputs/system/linux_sysctl_fs.go +++ b/plugins/inputs/system/linux_sysctl_fs.go @@ -3,10 +3,12 @@ package system import ( "bytes" "io/ioutil" + "os" "strconv" "github.com/influxdata/telegraf" "github.com/influxdata/telegraf/plugins/inputs" + "path" ) // https://www.kernel.org/doc/Documentation/sysctl/fs.txt @@ -80,9 +82,10 @@ func (sfs *SysctlFS) Gather(acc telegraf.Accumulator) error { } func init() { + inputs.Add("linux_sysctl_fs", func() telegraf.Input { return &SysctlFS{ - path: "/proc/sys/fs", + path: path.Join(GetHostProc(), "/sys/fs"), } }) } diff --git a/plugins/inputs/system/processes.go b/plugins/inputs/system/processes.go index 09aa37afbe60c..b48711e4cbb96 100644 --- a/plugins/inputs/system/processes.go +++ b/plugins/inputs/system/processes.go @@ -20,6 +20,7 @@ import ( type Processes struct { execPS func() ([]byte, error) + getHostProc func() string readProcFile func(filename string) ([]byte, error) forcePS bool @@ -62,6 +63,14 @@ func (p *Processes) Gather(acc telegraf.Accumulator) error { return nil } +func GetHostProc() string { + procPath := "/proc" + if os.Getenv("HOST_PROC") != "" { + procPath = os.Getenv("HOST_PROC") + } + return procPath +} + // Gets empty fields of metrics based on the OS func getEmptyFields() map[string]interface{} { fields := map[string]interface{}{ @@ -132,14 +141,14 @@ func (p *Processes) gatherFromPS(fields map[string]interface{}) error { // get process states from /proc/(pid)/stat files func (p *Processes) gatherFromProc(fields map[string]interface{}) error { - filenames, err := filepath.Glob("/proc/[0-9]*/stat") + filenames, err := filepath.Glob(GetHostProc() + "/[0-9]*/stat") + if err != nil { return err } for _, filename := range filenames { _, err := os.Stat(filename) - data, err := p.readProcFile(filename) if err != nil { return err @@ -227,6 +236,7 @@ func init() { inputs.Add("processes", func() telegraf.Input { return &Processes{ execPS: execPS, + getHostProc: getHostProc, readProcFile: readProcFile, } }) diff --git a/plugins/inputs/system/processes_test.go b/plugins/inputs/system/processes_test.go index 8a9b0fb6b794c..95bd5dd4487aa 100644 --- a/plugins/inputs/system/processes_test.go +++ b/plugins/inputs/system/processes_test.go @@ -67,6 +67,7 @@ func TestFromProcFiles(t *testing.T) { tester := tester{} processes := &Processes{ readProcFile: tester.testProcFile, + getHostProc: getHostProc, forceProc: true, } @@ -89,6 +90,7 @@ func TestFromProcFilesWithSpaceInCmd(t *testing.T) { tester := tester{} processes := &Processes{ readProcFile: tester.testProcFile2, + getHostProc: getHostProc, forceProc: true, }