Skip to content

Commit

Permalink
influxdata#2527 Allow collecting of host stats within docker containe…
Browse files Browse the repository at this point in the history
…rs by enable procfs and sysfs location overrides
  • Loading branch information
Matthew Cheung committed Jun 14, 2017
1 parent 4c53443 commit a045d08
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 6 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.
`SYS_PATH_PREFIX` the plugin will retrieve process information from the specified location.

`docker run -v /proc:/rootfs/proc:ro -e SYS_PATH_PREFIX=/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 `PROC_PATH_PREFIX` the plugin will retrieve process information from the specified location.

`docker run -v /proc:/rootfs/proc:ro -e PROC_PATH_PREFIX=/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("SYS_PATH_PREFIX") + "/proc/sys/fs",
}
})
}
19 changes: 14 additions & 5 deletions plugins/inputs/system/processes.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ import (

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

forcePS bool
forceProc bool
forcePS bool
forceProc bool
}

func (p *Processes) Description() string {
Expand All @@ -37,7 +38,7 @@ func (p *Processes) Gather(acc telegraf.Accumulator) error {
fields := getEmptyFields()

// Decide if we will use 'ps' to get stats (use procfs otherwise)
usePS := true
usePS := false
if runtime.GOOS == "linux" {
usePS = false
}
Expand Down Expand Up @@ -132,13 +133,16 @@ 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("PROC_PATH_PREFIX") + "/proc/[0-9]*/stat")
filenames, err := filepath.Glob(p.osGetEnv("/proc/[0-9]*/stat"))
log.Printf("Env var value: " + p.osGetEnv("PROC_PATH_PREFIX"))
if err != nil {
return err
}

for _, filename := range filenames {
_, err := os.Stat(filename)
log.Printf("Num of files: " + strconv.Itoa(len(filenames)))

data, err := p.readProcFile(filename)
if err != nil {
Expand Down Expand Up @@ -209,6 +213,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,7 +235,8 @@ func init() {
inputs.Add("processes", func() telegraf.Input {
return &Processes{
execPS: execPS,
osGetEnv: osGetEnv,
readProcFile: readProcFile,
}
})
}
}

0 comments on commit a045d08

Please sign in to comment.