feat(inputs.filecount): Add oldestFileTimestamp and newestFileTimestamp (#13163)

This commit is contained in:
tguenneguez 2023-05-02 13:34:56 +02:00 committed by GitHub
parent 7436575ea7
commit 3d40659387
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 4 deletions

View File

@ -56,10 +56,12 @@ See the [CONFIGURATION.md][CONFIGURATION.md] for more details.
- fields:
- count (integer)
- size_bytes (integer)
- oldest_file_timestamp (int, unix time nanoseconds)
- newest_file_timestamp (int, unix time nanoseconds)
## Example Output
```text
filecount,directory=/var/cache/apt count=7i,size_bytes=7438336i 1530034445000000000
filecount,directory=/tmp count=17i,size_bytes=28934786i 1530034445000000000
filecount,directory=/var/cache/apt count=7i,size_bytes=7438336i,oldest_file_timestamp=1507152973123456789i,newest_file_timestamp=1507152973123456789i 1530034445000000000
filecount,directory=/tmp count=17i,size_bytes=28934786i,oldest_file_timestamp=1507152973123456789i,newest_file_timestamp=1507152973123456789i 1530034445000000000
```

View File

@ -122,6 +122,8 @@ func (fc *FileCount) initFileFilters() {
func (fc *FileCount) count(acc telegraf.Accumulator, basedir string, glob globpath.GlobPath) {
childCount := make(map[string]int64)
childSize := make(map[string]int64)
oldestFileTimestamp := make(map[string]int64)
newestFileTimestamp := make(map[string]int64)
walkFn := func(path string, de *godirwalk.Dirent) error {
rel, err := filepath.Rel(basedir, path)
@ -144,6 +146,12 @@ func (fc *FileCount) count(acc telegraf.Accumulator, basedir string, glob globpa
parent := filepath.Dir(path)
childCount[parent]++
childSize[parent] += file.Size()
if oldestFileTimestamp[parent] == 0 || oldestFileTimestamp[parent] > file.ModTime().UnixNano() {
oldestFileTimestamp[parent] = file.ModTime().UnixNano()
}
if newestFileTimestamp[parent] == 0 || newestFileTimestamp[parent] < file.ModTime().UnixNano() {
newestFileTimestamp[parent] = file.ModTime().UnixNano()
}
}
if file.IsDir() && !fc.Recursive && !glob.HasSuperMeta {
return filepath.SkipDir
@ -157,6 +165,8 @@ func (fc *FileCount) count(acc telegraf.Accumulator, basedir string, glob globpa
"count": childCount[path],
"size_bytes": childSize[path],
}
gauge["oldest_file_timestamp"] = oldestFileTimestamp[path]
gauge["newest_file_timestamp"] = newestFileTimestamp[path]
acc.AddGauge("filecount", gauge,
map[string]string{
"directory": path,
@ -166,9 +176,17 @@ func (fc *FileCount) count(acc telegraf.Accumulator, basedir string, glob globpa
if fc.Recursive {
childCount[parent] += childCount[path]
childSize[parent] += childSize[path]
if oldestFileTimestamp[parent] == 0 || oldestFileTimestamp[parent] > oldestFileTimestamp[path] {
oldestFileTimestamp[parent] = oldestFileTimestamp[path]
}
if newestFileTimestamp[parent] == 0 || newestFileTimestamp[parent] < newestFileTimestamp[path] {
newestFileTimestamp[parent] = newestFileTimestamp[path]
}
}
delete(childCount, path)
delete(childSize, path)
delete(oldestFileTimestamp, path)
delete(newestFileTimestamp, path)
return nil
}

View File

@ -38,6 +38,8 @@ func TestNoFiltersOnChildDir(t *testing.T) {
require.NoError(t, acc.GatherError(fc.Gather))
require.True(t, acc.HasPoint("filecount", tags, "count", int64(len(matches))))
require.True(t, acc.HasPoint("filecount", tags, "size_bytes", int64(600)))
require.True(t, acc.HasInt64Field("filecount", "oldest_file_timestamp"))
require.True(t, acc.HasInt64Field("filecount", "newest_file_timestamp"))
}
func TestNoRecursiveButSuperMeta(t *testing.T) {
@ -52,6 +54,8 @@ func TestNoRecursiveButSuperMeta(t *testing.T) {
require.True(t, acc.HasPoint("filecount", tags, "count", int64(len(matches))))
require.True(t, acc.HasPoint("filecount", tags, "size_bytes", int64(200)))
require.True(t, acc.HasInt64Field("filecount", "oldest_file_timestamp"))
require.True(t, acc.HasInt64Field("filecount", "newest_file_timestamp"))
}
func TestNameFilter(t *testing.T) {
@ -157,8 +161,10 @@ func TestDirectoryWithTrailingSlash(t *testing.T) {
"directory": getTestdataDir(),
},
map[string]interface{}{
"count": 9,
"size_bytes": 5096,
"count": 9,
"size_bytes": 5096,
"newest_file_timestamp": time.Unix(1450117505, 0).UnixNano(),
"oldest_file_timestamp": time.Unix(1292351105, 0).UnixNano(),
},
time.Unix(0, 0),
telegraf.Gauge,