Use glob match for finding /proc/<pid>/stat files

closes #1323
This commit is contained in:
Cameron Sparr 2016-06-03 12:22:08 +01:00
parent 94f952787f
commit a362352587
2 changed files with 15 additions and 23 deletions

View File

@ -48,6 +48,7 @@ time before a new metric is included by the plugin.
- [#1283](https://github.com/influxdata/telegraf/pull/1283): Still send processes metrics if a process exited during metric collection. - [#1283](https://github.com/influxdata/telegraf/pull/1283): Still send processes metrics if a process exited during metric collection.
- [#1297](https://github.com/influxdata/telegraf/issues/1297): disk plugin panic when usage grab fails. - [#1297](https://github.com/influxdata/telegraf/issues/1297): disk plugin panic when usage grab fails.
- [#1316](https://github.com/influxdata/telegraf/pull/1316): Removed leaked "database" tag on redis metrics. Thanks @PierreF! - [#1316](https://github.com/influxdata/telegraf/pull/1316): Removed leaked "database" tag on redis metrics. Thanks @PierreF!
- [#1323](https://github.com/influxdata/telegraf/issues/1323): Processes plugin: fix potential error with /proc/net/stat directory.
## v0.13.1 [2016-05-24] ## v0.13.1 [2016-05-24]

View File

@ -9,7 +9,7 @@ import (
"log" "log"
"os" "os"
"os/exec" "os/exec"
"path" "path/filepath"
"runtime" "runtime"
"strconv" "strconv"
@ -19,7 +19,7 @@ import (
type Processes struct { type Processes struct {
execPS func() ([]byte, error) execPS func() ([]byte, error)
readProcFile func(statFile string) ([]byte, error) readProcFile func(filename string) ([]byte, error)
forcePS bool forcePS bool
forceProc bool forceProc bool
@ -128,22 +128,16 @@ 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 {
files, err := ioutil.ReadDir("/proc") filenames, err := filepath.Glob("/proc/[0-9]*/stat")
if err != nil { if err != nil {
return err return err
} }
for _, file := range files { for _, filename := range filenames {
if !file.IsDir() { _, err := os.Stat(filename)
continue
}
statFile := path.Join("/proc", file.Name(), "stat") data, err := p.readProcFile(filename)
data, err := p.readProcFile(statFile)
if err != nil { if err != nil {
if !file.IsDir() {
continue
}
return err return err
} }
if data == nil { if data == nil {
@ -159,7 +153,7 @@ func (p *Processes) gatherFromProc(fields map[string]interface{}) error {
stats := bytes.Fields(data) stats := bytes.Fields(data)
if len(stats) < 3 { if len(stats) < 3 {
return fmt.Errorf("Something is terribly wrong with %s", statFile) return fmt.Errorf("Something is terribly wrong with %s", filename)
} }
switch stats[0][0] { switch stats[0][0] {
case 'R': case 'R':
@ -176,7 +170,7 @@ func (p *Processes) gatherFromProc(fields map[string]interface{}) error {
fields["paging"] = fields["paging"].(int64) + int64(1) fields["paging"] = fields["paging"].(int64) + int64(1)
default: default:
log.Printf("processes: Unknown state [ %s ] in file %s", log.Printf("processes: Unknown state [ %s ] in file %s",
string(stats[0][0]), statFile) string(stats[0][0]), filename)
} }
fields["total"] = fields["total"].(int64) + int64(1) fields["total"] = fields["total"].(int64) + int64(1)
@ -190,15 +184,12 @@ func (p *Processes) gatherFromProc(fields map[string]interface{}) error {
return nil return nil
} }
func readProcFile(statFile string) ([]byte, error) { func readProcFile(filename string) ([]byte, error) {
if _, err := os.Stat(statFile); os.IsNotExist(err) { data, err := ioutil.ReadFile(filename)
return nil, nil
} else if err != nil {
return nil, err
}
data, err := ioutil.ReadFile(statFile)
if err != nil { if err != nil {
if os.IsNotExist(err) {
return nil, nil
}
return nil, err return nil, err
} }