Skip to content

Commit b991aab

Browse files
authored
plugins/filestat: Skip missing files (#7316)
1 parent 0860487 commit b991aab

2 files changed

Lines changed: 38 additions & 3 deletions

File tree

plugins/inputs/filestat/filestat.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,18 @@ type FileStat struct {
3535

3636
// maps full file paths to globmatch obj
3737
globs map[string]*globpath.GlobPath
38+
39+
// files that were missing - we only log the first time it's not found.
40+
missingFiles map[string]bool
41+
// files that had an error in Stat - we only log the first error.
42+
filesWithErrors map[string]bool
3843
}
3944

4045
func NewFileStat() *FileStat {
4146
return &FileStat{
42-
globs: make(map[string]*globpath.GlobPath),
47+
globs: make(map[string]*globpath.GlobPath),
48+
missingFiles: make(map[string]bool),
49+
filesWithErrors: make(map[string]bool),
4350
}
4451
}
4552

@@ -85,12 +92,23 @@ func (f *FileStat) Gather(acc telegraf.Accumulator) error {
8592
fileInfo, err := os.Stat(fileName)
8693
if os.IsNotExist(err) {
8794
fields["exists"] = int64(0)
95+
acc.AddFields("filestat", fields, tags)
96+
if !f.missingFiles[fileName] {
97+
f.Log.Warnf("File %q not found", fileName)
98+
f.missingFiles[fileName] = true
99+
}
100+
continue
88101
}
102+
f.missingFiles[fileName] = false
89103

90104
if fileInfo == nil {
91-
f.Log.Errorf("Unable to get info for file %q, possible permissions issue",
92-
fileName)
105+
if !f.filesWithErrors[fileName] {
106+
f.filesWithErrors[fileName] = true
107+
f.Log.Errorf("Unable to get info for file %q: %v",
108+
fileName, err)
109+
}
93110
} else {
111+
f.filesWithErrors[fileName] = false
94112
fields["size_bytes"] = fileInfo.Size()
95113
fields["modification_time"] = fileInfo.ModTime().UnixNano()
96114
}

plugins/inputs/filestat/filestat_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,23 @@ func TestGatherExplicitFiles(t *testing.T) {
8383
require.True(t, acc.HasPoint("filestat", tags3, "exists", int64(0)))
8484
}
8585

86+
func TestNonExistentFile(t *testing.T) {
87+
fs := NewFileStat()
88+
fs.Log = testutil.Logger{}
89+
fs.Md5 = true
90+
fs.Files = []string{
91+
"/non/existant/file",
92+
}
93+
acc := testutil.Accumulator{}
94+
require.NoError(t, acc.GatherError(fs.Gather))
95+
96+
acc.AssertContainsFields(t, "filestat", map[string]interface{}{"exists": int64(0)})
97+
assert.False(t, acc.HasField("filestat", "error"))
98+
assert.False(t, acc.HasField("filestat", "md5_sum"))
99+
assert.False(t, acc.HasField("filestat", "size_bytes"))
100+
assert.False(t, acc.HasField("filestat", "modification_time"))
101+
}
102+
86103
func TestGatherGlob(t *testing.T) {
87104
fs := NewFileStat()
88105
fs.Log = testutil.Logger{}

0 commit comments

Comments
 (0)