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
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

View File

@ -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)