From 9740e956ca3176774ae23a312e5d498f4c20ef5a Mon Sep 17 00:00:00 2001 From: Greg <2653109+glinton@users.noreply.github.com> Date: Tue, 26 Feb 2019 15:03:25 -0700 Subject: [PATCH] Log permission error and ignore in filecount input (#5483) --- plugins/inputs/filecount/README.md | 10 +++++----- plugins/inputs/filecount/filecount.go | 16 ++++++++++++++-- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/plugins/inputs/filecount/README.md b/plugins/inputs/filecount/README.md index a6836ffc3..49e28caa6 100644 --- a/plugins/inputs/filecount/README.md +++ b/plugins/inputs/filecount/README.md @@ -1,6 +1,6 @@ # Filecount Input Plugin -Counts files in directories that match certain criteria. +Reports the number and total size of files in specified directories. ### Configuration: @@ -8,7 +8,7 @@ Counts files in directories that match certain criteria. [[inputs.filecount]] ## Directory to gather stats about. ## deprecated in 1.9; use the directories option - directory = "/var/cache/apt/archives" + # directory = "/var/cache/apt/archives" ## Directories to gather stats about. ## This accept standard unit glob matching rules, but with the addition of @@ -16,13 +16,13 @@ Counts files in directories that match certain criteria. ## /var/log/** -> recursively find all directories in /var/log and count files in each directories ## /var/log/*/* -> find all directories with a parent dir in /var/log and count files in each directories ## /var/log -> count all files in /var/log and all of its subdirectories - directories = ["/var/cache/apt/archives"] + directories = ["/var/cache/apt", "/tmp"] ## Only count files that match the name pattern. Defaults to "*". - name = "*.deb" + name = "*" ## Count files in subdirectories. Defaults to true. - recursive = false + recursive = true ## Only count regular files. Defaults to true. regular_only = true diff --git a/plugins/inputs/filecount/filecount.go b/plugins/inputs/filecount/filecount.go index f8840721b..1fd7041ff 100644 --- a/plugins/inputs/filecount/filecount.go +++ b/plugins/inputs/filecount/filecount.go @@ -1,22 +1,25 @@ package filecount import ( + "log" "os" "path/filepath" "strings" "time" + "github.com/karrick/godirwalk" + "github.com/pkg/errors" + "github.com/influxdata/telegraf" "github.com/influxdata/telegraf/internal" "github.com/influxdata/telegraf/internal/globpath" "github.com/influxdata/telegraf/plugins/inputs" - "github.com/karrick/godirwalk" ) const sampleConfig = ` ## Directory to gather stats about. ## deprecated in 1.9; use the directories option - directory = "/var/cache/apt/archives" + # directory = "/var/cache/apt/archives" ## Directories to gather stats about. ## This accept standard unit glob matching rules, but with the addition of @@ -152,6 +155,7 @@ 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) + walkFn := func(path string, de *godirwalk.Dirent) error { if path == basedir { return nil @@ -178,6 +182,7 @@ func (fc *FileCount) count(acc telegraf.Accumulator, basedir string, glob globpa } return nil } + postChildrenFn := func(path string, de *godirwalk.Dirent) error { if glob.MatchString(path) { gauge := map[string]interface{}{ @@ -203,6 +208,13 @@ func (fc *FileCount) count(acc telegraf.Accumulator, basedir string, glob globpa Callback: walkFn, PostChildrenCallback: postChildrenFn, Unsorted: true, + ErrorCallback: func(osPathname string, err error) godirwalk.ErrorAction { + if os.IsPermission(errors.Cause(err)) { + log.Println("D! [inputs.filecount]", err) + return godirwalk.SkipNode + } + return godirwalk.Halt + }, }) if err != nil { acc.AddError(err)