plugins/filestat: Skip missing files (#7316)
This commit is contained in:
parent
0860487321
commit
b991aab75d
|
|
@ -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()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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{}
|
||||
|
|
|
|||
Loading…
Reference in New Issue