Skip to content

Commit

Permalink
2527 Allow collecting of host stats within docker containers by enabl…
Browse files Browse the repository at this point in the history
…e procfs and sysfs location overrides
  • Loading branch information
Matthew Cheung committed Jun 22, 2017
1 parent 4c53443 commit 4218e24
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
5 changes: 5 additions & 0 deletions plugins/inputs/system/PROCESSES_README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion plugins/inputs/system/linux_sysctl_fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"),
}
})
}
14 changes: 12 additions & 2 deletions plugins/inputs/system/processes.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

type Processes struct {
execPS func() ([]byte, error)
getHostProc func() string
readProcFile func(filename string) ([]byte, error)

forcePS bool
Expand Down Expand Up @@ -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{}{
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -227,6 +236,7 @@ func init() {
inputs.Add("processes", func() telegraf.Input {
return &Processes{
execPS: execPS,
getHostProc: getHostProc,
readProcFile: readProcFile,
}
})
Expand Down
2 changes: 2 additions & 0 deletions plugins/inputs/system/processes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func TestFromProcFiles(t *testing.T) {
tester := tester{}
processes := &Processes{
readProcFile: tester.testProcFile,
getHostProc: getHostProc,
forceProc: true,
}

Expand All @@ -89,6 +90,7 @@ func TestFromProcFilesWithSpaceInCmd(t *testing.T) {
tester := tester{}
processes := &Processes{
readProcFile: tester.testProcFile2,
getHostProc: getHostProc,
forceProc: true,
}

Expand Down

0 comments on commit 4218e24

Please sign in to comment.