diff --git a/plugins/inputs/filecount/README.md b/plugins/inputs/filecount/README.md index cf33475c0..32c2d89ec 100644 --- a/plugins/inputs/filecount/README.md +++ b/plugins/inputs/filecount/README.md @@ -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 ``` diff --git a/plugins/inputs/filecount/filecount.go b/plugins/inputs/filecount/filecount.go index de19a487c..02b22f5af 100644 --- a/plugins/inputs/filecount/filecount.go +++ b/plugins/inputs/filecount/filecount.go @@ -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 } diff --git a/plugins/inputs/filecount/filecount_test.go b/plugins/inputs/filecount/filecount_test.go index 1da8c0989..02739bb9b 100644 --- a/plugins/inputs/filecount/filecount_test.go +++ b/plugins/inputs/filecount/filecount_test.go @@ -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,