Support HOST_PROC in processes and linux_sysctl_fs inputs (#2924)

This commit is contained in:
MatthewCh 2017-06-23 14:11:33 -04:00 committed by Daniel Nelson
parent 793f12a0b1
commit cc72af94e6
3 changed files with 19 additions and 3 deletions

View File

@ -14,6 +14,11 @@ it requires access to execute `ps`.
# no configuration # 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: ### Measurements & Fields:
- processes - processes

View File

@ -7,6 +7,7 @@ import (
"github.com/influxdata/telegraf" "github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/inputs" "github.com/influxdata/telegraf/plugins/inputs"
"path"
) )
// https://www.kernel.org/doc/Documentation/sysctl/fs.txt // https://www.kernel.org/doc/Documentation/sysctl/fs.txt
@ -80,9 +81,10 @@ func (sfs *SysctlFS) Gather(acc telegraf.Accumulator) error {
} }
func init() { func init() {
inputs.Add("linux_sysctl_fs", func() telegraf.Input { inputs.Add("linux_sysctl_fs", func() telegraf.Input {
return &SysctlFS{ return &SysctlFS{
path: "/proc/sys/fs", path: path.Join(GetHostProc(), "/sys/fs"),
} }
}) })
} }

View File

@ -20,6 +20,7 @@ import (
type Processes struct { type Processes struct {
execPS func() ([]byte, error) execPS func() ([]byte, error)
getHostProc func() string
readProcFile func(filename string) ([]byte, error) readProcFile func(filename string) ([]byte, error)
forcePS bool forcePS bool
@ -62,6 +63,14 @@ func (p *Processes) Gather(acc telegraf.Accumulator) error {
return nil 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 // Gets empty fields of metrics based on the OS
func getEmptyFields() map[string]interface{} { func getEmptyFields() map[string]interface{} {
fields := 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 // get process states from /proc/(pid)/stat files
func (p *Processes) gatherFromProc(fields map[string]interface{}) error { 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 { if err != nil {
return err return err
} }
for _, filename := range filenames { for _, filename := range filenames {
_, err := os.Stat(filename) _, err := os.Stat(filename)
data, err := p.readProcFile(filename) data, err := p.readProcFile(filename)
if err != nil { if err != nil {
return err return err