From b991aab75d815f1337ed56be7d9eb0e39a863c44 Mon Sep 17 00:00:00 2001 From: Andreas Fuchs Date: Wed, 17 Feb 2021 15:50:25 -0500 Subject: [PATCH] plugins/filestat: Skip missing files (#7316) --- plugins/inputs/filestat/filestat.go | 24 +++++++++++++++++++++--- plugins/inputs/filestat/filestat_test.go | 17 +++++++++++++++++ 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/plugins/inputs/filestat/filestat.go b/plugins/inputs/filestat/filestat.go index bf8ea6c16..9450f9a41 100644 --- a/plugins/inputs/filestat/filestat.go +++ b/plugins/inputs/filestat/filestat.go @@ -35,11 +35,18 @@ type FileStat struct { // maps full file paths to globmatch obj globs map[string]*globpath.GlobPath + + // files that were missing - we only log the first time it's not found. + missingFiles map[string]bool + // files that had an error in Stat - we only log the first error. + filesWithErrors map[string]bool } func NewFileStat() *FileStat { return &FileStat{ - globs: make(map[string]*globpath.GlobPath), + globs: make(map[string]*globpath.GlobPath), + missingFiles: make(map[string]bool), + filesWithErrors: make(map[string]bool), } } @@ -85,12 +92,23 @@ func (f *FileStat) Gather(acc telegraf.Accumulator) error { fileInfo, err := os.Stat(fileName) if os.IsNotExist(err) { fields["exists"] = int64(0) + acc.AddFields("filestat", fields, tags) + if !f.missingFiles[fileName] { + f.Log.Warnf("File %q not found", fileName) + f.missingFiles[fileName] = true + } + continue } + f.missingFiles[fileName] = false if fileInfo == nil { - f.Log.Errorf("Unable to get info for file %q, possible permissions issue", - fileName) + if !f.filesWithErrors[fileName] { + f.filesWithErrors[fileName] = true + f.Log.Errorf("Unable to get info for file %q: %v", + fileName, err) + } } else { + f.filesWithErrors[fileName] = false fields["size_bytes"] = fileInfo.Size() fields["modification_time"] = fileInfo.ModTime().UnixNano() } diff --git a/plugins/inputs/filestat/filestat_test.go b/plugins/inputs/filestat/filestat_test.go index 79a111ffb..f0b843dcb 100644 --- a/plugins/inputs/filestat/filestat_test.go +++ b/plugins/inputs/filestat/filestat_test.go @@ -83,6 +83,23 @@ func TestGatherExplicitFiles(t *testing.T) { require.True(t, acc.HasPoint("filestat", tags3, "exists", int64(0))) } +func TestNonExistentFile(t *testing.T) { + fs := NewFileStat() + fs.Log = testutil.Logger{} + fs.Md5 = true + fs.Files = []string{ + "/non/existant/file", + } + acc := testutil.Accumulator{} + require.NoError(t, acc.GatherError(fs.Gather)) + + acc.AssertContainsFields(t, "filestat", map[string]interface{}{"exists": int64(0)}) + assert.False(t, acc.HasField("filestat", "error")) + assert.False(t, acc.HasField("filestat", "md5_sum")) + assert.False(t, acc.HasField("filestat", "size_bytes")) + assert.False(t, acc.HasField("filestat", "modification_time")) +} + func TestGatherGlob(t *testing.T) { fs := NewFileStat() fs.Log = testutil.Logger{}