Log permission error and ignore in filecount input (#5483)

This commit is contained in:
Greg 2019-02-26 15:03:25 -07:00 committed by Daniel Nelson
parent c9597a2463
commit 9740e956ca
2 changed files with 19 additions and 7 deletions

View File

@ -1,6 +1,6 @@
# Filecount Input Plugin # Filecount Input Plugin
Counts files in directories that match certain criteria. Reports the number and total size of files in specified directories.
### Configuration: ### Configuration:
@ -8,7 +8,7 @@ Counts files in directories that match certain criteria.
[[inputs.filecount]] [[inputs.filecount]]
## Directory to gather stats about. ## Directory to gather stats about.
## deprecated in 1.9; use the directories option ## deprecated in 1.9; use the directories option
directory = "/var/cache/apt/archives" # directory = "/var/cache/apt/archives"
## Directories to gather stats about. ## Directories to gather stats about.
## This accept standard unit glob matching rules, but with the addition of ## 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/** -> 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/*/* -> 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 ## /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 "*". ## Only count files that match the name pattern. Defaults to "*".
name = "*.deb" name = "*"
## Count files in subdirectories. Defaults to true. ## Count files in subdirectories. Defaults to true.
recursive = false recursive = true
## Only count regular files. Defaults to true. ## Only count regular files. Defaults to true.
regular_only = true regular_only = true

View File

@ -1,22 +1,25 @@
package filecount package filecount
import ( import (
"log"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
"time" "time"
"github.com/karrick/godirwalk"
"github.com/pkg/errors"
"github.com/influxdata/telegraf" "github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/internal" "github.com/influxdata/telegraf/internal"
"github.com/influxdata/telegraf/internal/globpath" "github.com/influxdata/telegraf/internal/globpath"
"github.com/influxdata/telegraf/plugins/inputs" "github.com/influxdata/telegraf/plugins/inputs"
"github.com/karrick/godirwalk"
) )
const sampleConfig = ` const sampleConfig = `
## Directory to gather stats about. ## Directory to gather stats about.
## deprecated in 1.9; use the directories option ## deprecated in 1.9; use the directories option
directory = "/var/cache/apt/archives" # directory = "/var/cache/apt/archives"
## Directories to gather stats about. ## Directories to gather stats about.
## This accept standard unit glob matching rules, but with the addition of ## 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) { func (fc *FileCount) count(acc telegraf.Accumulator, basedir string, glob globpath.GlobPath) {
childCount := make(map[string]int64) childCount := make(map[string]int64)
childSize := make(map[string]int64) childSize := make(map[string]int64)
walkFn := func(path string, de *godirwalk.Dirent) error { walkFn := func(path string, de *godirwalk.Dirent) error {
if path == basedir { if path == basedir {
return nil return nil
@ -178,6 +182,7 @@ func (fc *FileCount) count(acc telegraf.Accumulator, basedir string, glob globpa
} }
return nil return nil
} }
postChildrenFn := func(path string, de *godirwalk.Dirent) error { postChildrenFn := func(path string, de *godirwalk.Dirent) error {
if glob.MatchString(path) { if glob.MatchString(path) {
gauge := map[string]interface{}{ gauge := map[string]interface{}{
@ -203,6 +208,13 @@ func (fc *FileCount) count(acc telegraf.Accumulator, basedir string, glob globpa
Callback: walkFn, Callback: walkFn,
PostChildrenCallback: postChildrenFn, PostChildrenCallback: postChildrenFn,
Unsorted: true, 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 { if err != nil {
acc.AddError(err) acc.AddError(err)