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 21, 2017
1 parent 4c53443 commit f57ff57
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 5 deletions.
5 changes: 5 additions & 0 deletions plugins/inputs/system/LINUX_SYSCTL_FS_README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,8 @@ 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
```
### Configuration:
The sysctl path can be redefined by specifying the prefix of the path as an 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`
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 a prefix for resolving the procfs (/proc) location.
Using the environment variable `HOST_SYS` the plugin will retrieve process information from the specified location.

`docker run -v /proc:/rootfs/proc:ro -e HOST_SYS=/rootfs`

### Measurements & Fields:

- processes
Expand Down
6 changes: 5 additions & 1 deletion plugins/inputs/system/linux_sysctl_fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package system
import (
"bytes"
"io/ioutil"
"os"
"strconv"

"github.com/influxdata/telegraf"
Expand All @@ -23,6 +24,9 @@ func (_ SysctlFS) Description() string {
func (_ SysctlFS) SampleConfig() string {
return sysctlFSSampleConfig
}
func OSGetEnv(key string) string {
return os.Getenv(key)
}

func (sfs *SysctlFS) gatherList(file string, fields map[string]interface{}, fieldNames ...string) error {
bs, err := ioutil.ReadFile(sfs.path + "/" + file)
Expand Down Expand Up @@ -82,7 +86,7 @@ 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: OSGetEnv("HOST_SYS") + "/proc/sys/fs",
}
})
}
10 changes: 8 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)
osGetEnv func(key string) string
readProcFile func(filename string) ([]byte, error)

forcePS bool
Expand Down Expand Up @@ -132,14 +133,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(p.osGetEnv("HOST_PROC") + "/proc/[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 @@ -209,6 +210,10 @@ func readProcFile(filename string) ([]byte, error) {
return data, nil
}

func osGetEnv(key string) string {
return os.Getenv(key)
}

func execPS() ([]byte, error) {
bin, err := exec.LookPath("ps")
if err != nil {
Expand All @@ -227,6 +232,7 @@ func init() {
inputs.Add("processes", func() telegraf.Input {
return &Processes{
execPS: execPS,
osGetEnv: osGetEnv,
readProcFile: readProcFile,
}
})
Expand Down
8 changes: 6 additions & 2 deletions plugins/inputs/system/processes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
func TestProcesses(t *testing.T) {
processes := &Processes{
execPS: execPS,
osGetEnv: osGetEnv,
readProcFile: readProcFile,
}
var acc testutil.Accumulator
Expand All @@ -31,8 +32,9 @@ func TestProcesses(t *testing.T) {

func TestFromPS(t *testing.T) {
processes := &Processes{
execPS: testExecPS,
forcePS: true,
execPS: testExecPS,
osGetEnv: osGetEnv,
forcePS: true,
}

var acc testutil.Accumulator
Expand Down Expand Up @@ -67,6 +69,7 @@ func TestFromProcFiles(t *testing.T) {
tester := tester{}
processes := &Processes{
readProcFile: tester.testProcFile,
osGetEnv: osGetEnv,
forceProc: true,
}

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

Expand Down

0 comments on commit f57ff57

Please sign in to comment.