diff --git a/plugins/inputs/filecount/filecount.go b/plugins/inputs/filecount/filecount.go index 02b22f5af..d9654bb5e 100644 --- a/plugins/inputs/filecount/filecount.go +++ b/plugins/inputs/filecount/filecount.go @@ -130,7 +130,7 @@ func (fc *FileCount) count(acc telegraf.Accumulator, basedir string, glob globpa if err == nil && rel == "." { return nil } - file, err := fc.Fs.Stat(path) + file, err := fc.resolveLink(path) if err != nil { if os.IsNotExist(err) { return nil @@ -244,6 +244,21 @@ func (fc *FileCount) Gather(acc telegraf.Accumulator) error { return nil } +func (fc *FileCount) resolveLink(path string) (os.FileInfo, error) { + if fc.FollowSymlinks { + return fc.Fs.Stat(path) + } + fi, err := fc.Fs.Lstat(path) + if err != nil { + return fi, err + } + if fi.Mode()&os.ModeSymlink != 0 { + // if this file is a symlink, skip it + return nil, godirwalk.SkipThis + } + return fi, nil +} + func (fc *FileCount) onlyDirectories(directories []string) []string { out := make([]string, 0) for _, path := range directories { diff --git a/plugins/inputs/filecount/filesystem_helpers.go b/plugins/inputs/filecount/filesystem_helpers.go index b39bccaa7..cd55afba9 100644 --- a/plugins/inputs/filecount/filesystem_helpers.go +++ b/plugins/inputs/filecount/filesystem_helpers.go @@ -14,6 +14,7 @@ import ( type fileSystem interface { Open(name string) (file, error) Stat(name string) (os.FileInfo, error) + Lstat(name string) (os.FileInfo, error) } type file interface { @@ -27,5 +28,6 @@ type file interface { // osFS implements fileSystem using the local disk type osFS struct{} -func (osFS) Open(name string) (file, error) { return os.Open(name) } -func (osFS) Stat(name string) (os.FileInfo, error) { return os.Stat(name) } +func (osFS) Open(name string) (file, error) { return os.Open(name) } +func (osFS) Stat(name string) (os.FileInfo, error) { return os.Stat(name) } +func (osFS) Lstat(name string) (os.FileInfo, error) { return os.Lstat(name) } diff --git a/plugins/inputs/filecount/filesystem_helpers_notwindows.go b/plugins/inputs/filecount/filesystem_helpers_notwindows.go index 9a187d1a8..ca2dbc074 100644 --- a/plugins/inputs/filecount/filesystem_helpers_notwindows.go +++ b/plugins/inputs/filecount/filesystem_helpers_notwindows.go @@ -50,3 +50,8 @@ func (f fakeFileSystem) Stat(name string) (os.FileInfo, error) { } return nil, &os.PathError{Op: "Stat", Path: name, Err: errors.New("no such file or directory")} } + +func (f fakeFileSystem) Lstat(name string) (os.FileInfo, error) { + // not able to test with symlinks currently + return f.Stat(name) +}