Support resolution of symlinks in filecount input (#6735)

This commit is contained in:
Kevin Lin 2019-12-02 11:06:36 -08:00 committed by Daniel Nelson
parent 03a6910689
commit 906027c39b
3 changed files with 42 additions and 21 deletions

View File

@ -27,6 +27,9 @@ Reports the number and total size of files in specified directories.
## Only count regular files. Defaults to true. ## Only count regular files. Defaults to true.
regular_only = true regular_only = true
## Follow all symlinks while walking the directory tree. Defaults to false.
follow_symlinks = false
## Only count files that are at least this size. If size is ## Only count files that are at least this size. If size is
## a negative number, only count files that are smaller than the ## a negative number, only count files that are smaller than the
## absolute value of size. Acceptable units are B, KiB, MiB, KB, ... ## absolute value of size. Acceptable units are B, KiB, MiB, KB, ...

View File

@ -35,6 +35,9 @@ const sampleConfig = `
## Only count regular files. Defaults to true. ## Only count regular files. Defaults to true.
regular_only = true regular_only = true
## Follow all symlinks while walking the directory tree. Defaults to false.
follow_symlinks = false
## Only count files that are at least this size. If size is ## Only count files that are at least this size. If size is
## a negative number, only count files that are smaller than the ## a negative number, only count files that are smaller than the
## absolute value of size. Acceptable units are B, KiB, MiB, KB, ... ## absolute value of size. Acceptable units are B, KiB, MiB, KB, ...
@ -48,17 +51,18 @@ const sampleConfig = `
` `
type FileCount struct { type FileCount struct {
Directory string // deprecated in 1.9 Directory string // deprecated in 1.9
Directories []string Directories []string
Name string Name string
Recursive bool Recursive bool
RegularOnly bool RegularOnly bool
Size internal.Size FollowSymlinks bool
MTime internal.Duration `toml:"mtime"` Size internal.Size
fileFilters []fileFilterFunc MTime internal.Duration `toml:"mtime"`
globPaths []globpath.GlobPath fileFilters []fileFilterFunc
Fs fileSystem globPaths []globpath.GlobPath
Log telegraf.Logger Fs fileSystem
Log telegraf.Logger
} }
func (_ *FileCount) Description() string { func (_ *FileCount) Description() string {
@ -208,6 +212,7 @@ func (fc *FileCount) count(acc telegraf.Accumulator, basedir string, glob globpa
Callback: walkFn, Callback: walkFn,
PostChildrenCallback: postChildrenFn, PostChildrenCallback: postChildrenFn,
Unsorted: true, Unsorted: true,
FollowSymbolicLinks: fc.FollowSymlinks,
ErrorCallback: func(osPathname string, err error) godirwalk.ErrorAction { ErrorCallback: func(osPathname string, err error) godirwalk.ErrorAction {
if os.IsPermission(errors.Cause(err)) { if os.IsPermission(errors.Cause(err)) {
fc.Log.Debug(err) fc.Log.Debug(err)
@ -292,15 +297,16 @@ func (fc *FileCount) initGlobPaths(acc telegraf.Accumulator) {
func NewFileCount() *FileCount { func NewFileCount() *FileCount {
return &FileCount{ return &FileCount{
Directory: "", Directory: "",
Directories: []string{}, Directories: []string{},
Name: "*", Name: "*",
Recursive: true, Recursive: true,
RegularOnly: true, RegularOnly: true,
Size: internal.Size{Size: 0}, FollowSymlinks: false,
MTime: internal.Duration{Duration: 0}, Size: internal.Size{Size: 0},
fileFilters: nil, MTime: internal.Duration{Duration: 0},
Fs: osFS{}, fileFilters: nil,
Fs: osFS{},
} }
} }

View File

@ -102,7 +102,6 @@ func TestSizeFilter(t *testing.T) {
} }
func TestMTimeFilter(t *testing.T) { func TestMTimeFilter(t *testing.T) {
mtime := time.Date(2011, time.December, 14, 18, 25, 5, 0, time.UTC) mtime := time.Date(2011, time.December, 14, 18, 25, 5, 0, time.UTC)
fileAge := time.Since(mtime) - (60 * time.Second) fileAge := time.Since(mtime) - (60 * time.Second)
@ -119,6 +118,19 @@ func TestMTimeFilter(t *testing.T) {
fileCountEquals(t, fc, len(matches), 0) fileCountEquals(t, fc, len(matches), 0)
} }
// The library dependency karrick/godirwalk completely abstracts out the
// behavior of the FollowSymlinks plugin input option. However, it should at
// least behave identically when enabled on a filesystem with no symlinks.
func TestFollowSymlinks(t *testing.T) {
fc := getNoFilterFileCount()
fc.FollowSymlinks = true
matches := []string{"foo", "bar", "baz", "qux",
"subdir/", "subdir/quux", "subdir/quuz",
"subdir/nested2", "subdir/nested2/qux"}
fileCountEquals(t, fc, len(matches), 5096)
}
// Paths with a trailing slash will not exactly match paths produced during the // Paths with a trailing slash will not exactly match paths produced during the
// walk as these paths are cleaned before being returned from godirwalk. #6329 // walk as these paths are cleaned before being returned from godirwalk. #6329
func TestDirectoryWithTrailingSlash(t *testing.T) { func TestDirectoryWithTrailingSlash(t *testing.T) {