diff --git a/internal/globpath/globpath.go b/internal/globpath/globpath.go index fc6a436189500..b21d93520841a 100644 --- a/internal/globpath/globpath.go +++ b/internal/globpath/globpath.go @@ -6,6 +6,7 @@ import ( "strings" "github.com/gobwas/glob" + "github.com/karrick/godirwalk" ) type GlobPath struct { @@ -41,38 +42,42 @@ func Compile(path string) (*GlobPath, error) { } // Match returns all files matching the expression -func (g *GlobPath) Match() map[string]os.FileInfo { - out := make(map[string]os.FileInfo) +// If it's a static path, returns path +func (g *GlobPath) Match() []string { if !g.hasMeta { - info, err := os.Stat(g.path) - if err == nil { - out[g.path] = info - } - return out + return []string{g.path} } if !g.HasSuperMeta { files, _ := filepath.Glob(g.path) - for _, file := range files { - info, err := os.Stat(file) - if err == nil { - out[file] = info - } - } - return out + return files } roots, err := filepath.Glob(g.rootGlob) if err != nil { - return out + return []string{} } - walkfn := func(path string, info os.FileInfo, _ error) error { + out := []string{} + walkfn := func(path string, _ *godirwalk.Dirent) error { if g.g.Match(path) { - out[path] = info + out = append(out, path) } return nil } for _, root := range roots { - filepath.Walk(root, walkfn) + fileinfo, err := os.Stat(root) + if err != nil { + continue + } + if !fileinfo.IsDir() { + if g.MatchString(root) { + out = append(out, root) + } + continue + } + godirwalk.Walk(root, &godirwalk.Options{ + Callback: walkfn, + Unsorted: true, + }) } return out } diff --git a/internal/globpath/globpath_test.go b/internal/globpath/globpath_test.go index e67474fa962d5..476ba924346e0 100644 --- a/internal/globpath/globpath_test.go +++ b/internal/globpath/globpath_test.go @@ -1,7 +1,6 @@ package globpath import ( - "os" "runtime" "strings" "testing" @@ -34,7 +33,7 @@ func TestCompileAndMatch(t *testing.T) { matches = g3.Match() require.Len(t, matches, 1) matches = g4.Match() - require.Len(t, matches, 0) + require.Len(t, matches, 1) matches = g5.Match() require.Len(t, matches, 0) } @@ -75,10 +74,10 @@ func getTestdataDir() string { func TestMatch_ErrPermission(t *testing.T) { tests := []struct { input string - expected map[string]os.FileInfo + expected []string }{ - {"/root/foo", map[string]os.FileInfo{}}, - {"/root/f*", map[string]os.FileInfo{}}, + {"/root/foo", []string{"/root/foo"}}, + {"/root/f*", []string(nil)}, } for _, test := range tests { diff --git a/plugins/inputs/file/file.go b/plugins/inputs/file/file.go index d6714301eaed2..b93a7ba9925d0 100644 --- a/plugins/inputs/file/file.go +++ b/plugins/inputs/file/file.go @@ -75,10 +75,7 @@ func (f *File) refreshFilePaths() error { if len(files) <= 0 { return fmt.Errorf("could not find file: %v", file) } - - for k := range files { - allFiles = append(allFiles, k) - } + allFiles = append(allFiles, files...) } f.filenames = allFiles diff --git a/plugins/inputs/filestat/filestat.go b/plugins/inputs/filestat/filestat.go index 762eaa42033c9..692e58c53e946 100644 --- a/plugins/inputs/filestat/filestat.go +++ b/plugins/inputs/filestat/filestat.go @@ -73,13 +73,17 @@ func (f *FileStat) Gather(acc telegraf.Accumulator) error { continue } - for fileName, fileInfo := range files { + for _, fileName := range files { tags := map[string]string{ "file": fileName, } fields := map[string]interface{}{ "exists": int64(1), } + fileInfo, err := os.Stat(fileName) + if os.IsNotExist(err) { + fields["exists"] = int64(0) + } if fileInfo == nil { log.Printf("E! Unable to get info for file [%s], possible permissions issue", diff --git a/plugins/inputs/logparser/logparser.go b/plugins/inputs/logparser/logparser.go index 163436a3cc52c..eb23e2b742e4d 100644 --- a/plugins/inputs/logparser/logparser.go +++ b/plugins/inputs/logparser/logparser.go @@ -182,7 +182,7 @@ func (l *LogParserPlugin) tailNewfiles(fromBeginning bool) error { } files := g.Match() - for file := range files { + for _, file := range files { if _, ok := l.tailers[file]; ok { // we're already tailing this file continue diff --git a/plugins/inputs/tail/tail.go b/plugins/inputs/tail/tail.go index 598287963f564..bdfa2de444031 100644 --- a/plugins/inputs/tail/tail.go +++ b/plugins/inputs/tail/tail.go @@ -111,7 +111,7 @@ func (t *Tail) tailNewFiles(fromBeginning bool) error { if err != nil { t.acc.AddError(fmt.Errorf("E! Error Glob %s failed to compile, %s", filepath, err)) } - for file := range g.Match() { + for _, file := range g.Match() { if _, ok := t.tailers[file]; ok { // we're already tailing this file continue