Fix basedir check and parent dir extraction in filecount input (#5630)

This commit is contained in:
Daniel Nelson 2019-03-26 18:12:40 -07:00 committed by GitHub
parent 22ab649261
commit 5f74c0da0d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 6 additions and 7 deletions

View File

@ -4,16 +4,14 @@ import (
"log" "log"
"os" "os"
"path/filepath" "path/filepath"
"strings"
"time" "time"
"github.com/karrick/godirwalk"
"github.com/pkg/errors"
"github.com/influxdata/telegraf" "github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/internal" "github.com/influxdata/telegraf/internal"
"github.com/influxdata/telegraf/internal/globpath" "github.com/influxdata/telegraf/internal/globpath"
"github.com/influxdata/telegraf/plugins/inputs" "github.com/influxdata/telegraf/plugins/inputs"
"github.com/karrick/godirwalk"
"github.com/pkg/errors"
) )
const sampleConfig = ` const sampleConfig = `
@ -157,7 +155,8 @@ func (fc *FileCount) count(acc telegraf.Accumulator, basedir string, glob globpa
childSize := make(map[string]int64) childSize := make(map[string]int64)
walkFn := func(path string, de *godirwalk.Dirent) error { walkFn := func(path string, de *godirwalk.Dirent) error {
if path == basedir { rel, err := filepath.Rel(basedir, path)
if err == nil && rel == "." {
return nil return nil
} }
file, err := os.Stat(path) file, err := os.Stat(path)
@ -173,7 +172,7 @@ func (fc *FileCount) count(acc telegraf.Accumulator, basedir string, glob globpa
return nil return nil
} }
if match { if match {
parent := path[:strings.LastIndex(path, "/")] parent := filepath.Dir(path)
childCount[parent]++ childCount[parent]++
childSize[parent] += file.Size() childSize[parent] += file.Size()
} }
@ -194,7 +193,7 @@ func (fc *FileCount) count(acc telegraf.Accumulator, basedir string, glob globpa
"directory": path, "directory": path,
}) })
} }
parent := path[:strings.LastIndex(path, "/")] parent := filepath.Dir(path)
if fc.Recursive { if fc.Recursive {
childCount[parent] += childCount[path] childCount[parent] += childCount[path]
childSize[parent] += childSize[path] childSize[parent] += childSize[path]