Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add modification_time field to filestat input plugin #3305

Merged
merged 8 commits into from
Nov 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions plugins/inputs/filestat/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ The filestat plugin gathers metrics about file existence, size, and other stats.
- filestat
- exists (int, 0 | 1)
- size_bytes (int, bytes)
- modification_time (int, unixtime)
- md5 (optional, string)

### Tags:
Expand All @@ -32,6 +33,6 @@ The filestat plugin gathers metrics about file existence, size, and other stats.
```
$ telegraf --config /etc/telegraf/telegraf.conf --input-filter filestat --test
* Plugin: filestat, Collection 1
> filestat,file=/tmp/foo/bar,host=tyrion exists=0i 1461203374493128216
> filestat,file=/Users/sparrc/ws/telegraf.conf,host=tyrion exists=1i,size=47894i 1461203374493199335
> filestat,file=/tmp/foo/bar,host=tyrion exists=0i 1507218518192154351
> filestat,file=/Users/sparrc/ws/telegraf.conf,host=tyrion exists=1i,size=47894i,modification_time=1507152973123456789i 1507218518192154351
```
1 change: 1 addition & 0 deletions plugins/inputs/filestat/filestat.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ func (f *FileStat) Gather(acc telegraf.Accumulator) error {
fileName)
} else {
fields["size_bytes"] = fileInfo.Size()
fields["modification_time"] = fileInfo.ModTime().UnixNano()
}

if f.Md5 {
Expand Down
115 changes: 61 additions & 54 deletions plugins/inputs/filestat/filestat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,19 @@ func TestGatherNoMd5(t *testing.T) {
tags1 := map[string]string{
"file": dir + "log1.log",
}
require.True(t, acc.HasPoint("filestat", tags1,
"size_bytes", int64(0)))
require.True(t, acc.HasPoint("filestat", tags1,
"exists", int64(1)))
require.True(t, acc.HasPoint("filestat", tags1, "size_bytes", int64(0)))
require.True(t, acc.HasPoint("filestat", tags1, "exists", int64(1)))

tags2 := map[string]string{
"file": dir + "log2.log",
}
require.True(t, acc.HasPoint("filestat", tags2,
"size_bytes", int64(0)))
require.True(t, acc.HasPoint("filestat", tags2,
"exists", int64(1)))
require.True(t, acc.HasPoint("filestat", tags2, "size_bytes", int64(0)))
require.True(t, acc.HasPoint("filestat", tags2, "exists", int64(1)))

tags3 := map[string]string{
"file": "/non/existant/file",
}
require.True(t, acc.HasPoint("filestat", tags3,
"exists", int64(0)))
require.True(t, acc.HasPoint("filestat", tags3, "exists", int64(0)))
}

func TestGatherExplicitFiles(t *testing.T) {
Expand All @@ -62,28 +57,21 @@ func TestGatherExplicitFiles(t *testing.T) {
tags1 := map[string]string{
"file": dir + "log1.log",
}
require.True(t, acc.HasPoint("filestat", tags1,
"size_bytes", int64(0)))
require.True(t, acc.HasPoint("filestat", tags1,
"exists", int64(1)))
require.True(t, acc.HasPoint("filestat", tags1,
"md5_sum", "d41d8cd98f00b204e9800998ecf8427e"))
require.True(t, acc.HasPoint("filestat", tags1, "size_bytes", int64(0)))
require.True(t, acc.HasPoint("filestat", tags1, "exists", int64(1)))
require.True(t, acc.HasPoint("filestat", tags1, "md5_sum", "d41d8cd98f00b204e9800998ecf8427e"))

tags2 := map[string]string{
"file": dir + "log2.log",
}
require.True(t, acc.HasPoint("filestat", tags2,
"size_bytes", int64(0)))
require.True(t, acc.HasPoint("filestat", tags2,
"exists", int64(1)))
require.True(t, acc.HasPoint("filestat", tags2,
"md5_sum", "d41d8cd98f00b204e9800998ecf8427e"))
require.True(t, acc.HasPoint("filestat", tags2, "size_bytes", int64(0)))
require.True(t, acc.HasPoint("filestat", tags2, "exists", int64(1)))
require.True(t, acc.HasPoint("filestat", tags2, "md5_sum", "d41d8cd98f00b204e9800998ecf8427e"))

tags3 := map[string]string{
"file": "/non/existant/file",
}
require.True(t, acc.HasPoint("filestat", tags3,
"exists", int64(0)))
require.True(t, acc.HasPoint("filestat", tags3, "exists", int64(0)))
}

func TestGatherGlob(t *testing.T) {
Expand All @@ -100,22 +88,16 @@ func TestGatherGlob(t *testing.T) {
tags1 := map[string]string{
"file": dir + "log1.log",
}
fields1 := map[string]interface{}{
"size_bytes": int64(0),
"exists": int64(1),
"md5_sum": "d41d8cd98f00b204e9800998ecf8427e",
}
acc.AssertContainsTaggedFields(t, "filestat", fields1, tags1)
require.True(t, acc.HasPoint("filestat", tags1, "size_bytes", int64(0)))
require.True(t, acc.HasPoint("filestat", tags1, "exists", int64(1)))
require.True(t, acc.HasPoint("filestat", tags1, "md5_sum", "d41d8cd98f00b204e9800998ecf8427e"))

tags2 := map[string]string{
"file": dir + "log2.log",
}
fields2 := map[string]interface{}{
"size_bytes": int64(0),
"exists": int64(1),
"md5_sum": "d41d8cd98f00b204e9800998ecf8427e",
}
acc.AssertContainsTaggedFields(t, "filestat", fields2, tags2)
require.True(t, acc.HasPoint("filestat", tags2, "size_bytes", int64(0)))
require.True(t, acc.HasPoint("filestat", tags2, "exists", int64(1)))
require.True(t, acc.HasPoint("filestat", tags2, "md5_sum", "d41d8cd98f00b204e9800998ecf8427e"))
}

func TestGatherSuperAsterisk(t *testing.T) {
Expand All @@ -132,32 +114,57 @@ func TestGatherSuperAsterisk(t *testing.T) {
tags1 := map[string]string{
"file": dir + "log1.log",
}
require.True(t, acc.HasPoint("filestat", tags1,
"size_bytes", int64(0)))
require.True(t, acc.HasPoint("filestat", tags1,
"exists", int64(1)))
require.True(t, acc.HasPoint("filestat", tags1,
"md5_sum", "d41d8cd98f00b204e9800998ecf8427e"))
require.True(t, acc.HasPoint("filestat", tags1, "size_bytes", int64(0)))
require.True(t, acc.HasPoint("filestat", tags1, "exists", int64(1)))
require.True(t, acc.HasPoint("filestat", tags1, "md5_sum", "d41d8cd98f00b204e9800998ecf8427e"))

tags2 := map[string]string{
"file": dir + "log2.log",
}
require.True(t, acc.HasPoint("filestat", tags2,
"size_bytes", int64(0)))
require.True(t, acc.HasPoint("filestat", tags2,
"exists", int64(1)))
require.True(t, acc.HasPoint("filestat", tags2,
"md5_sum", "d41d8cd98f00b204e9800998ecf8427e"))
require.True(t, acc.HasPoint("filestat", tags2, "size_bytes", int64(0)))
require.True(t, acc.HasPoint("filestat", tags2, "exists", int64(1)))
require.True(t, acc.HasPoint("filestat", tags2, "md5_sum", "d41d8cd98f00b204e9800998ecf8427e"))

tags3 := map[string]string{
"file": dir + "test.conf",
}
require.True(t, acc.HasPoint("filestat", tags3,
"size_bytes", int64(104)))
require.True(t, acc.HasPoint("filestat", tags3,
"exists", int64(1)))
require.True(t, acc.HasPoint("filestat", tags3,
"md5_sum", "5a7e9b77fa25e7bb411dbd17cf403c1f"))
require.True(t, acc.HasPoint("filestat", tags3, "size_bytes", int64(104)))
require.True(t, acc.HasPoint("filestat", tags3, "exists", int64(1)))
require.True(t, acc.HasPoint("filestat", tags3, "md5_sum", "5a7e9b77fa25e7bb411dbd17cf403c1f"))
}

func TestModificationTime(t *testing.T) {
dir := getTestdataDir()
fs := NewFileStat()
fs.Files = []string{
dir + "log1.log",
}

acc := testutil.Accumulator{}
acc.GatherError(fs.Gather)

tags1 := map[string]string{
"file": dir + "log1.log",
}
require.True(t, acc.HasPoint("filestat", tags1, "size_bytes", int64(0)))
require.True(t, acc.HasPoint("filestat", tags1, "exists", int64(1)))
require.True(t, acc.HasInt64Field("filestat", "modification_time"))
}

func TestNoModificationTime(t *testing.T) {
fs := NewFileStat()
fs.Files = []string{
"/non/existant/file",
}

acc := testutil.Accumulator{}
acc.GatherError(fs.Gather)

tags1 := map[string]string{
"file": "/non/existant/file",
}
require.True(t, acc.HasPoint("filestat", tags1, "exists", int64(0)))
require.False(t, acc.HasInt64Field("filestat", "modification_time"))
}

func TestGetMd5(t *testing.T) {
Expand Down