2018-07-31 22:05:55 +00:00
|
|
|
package filecount
|
|
|
|
|
|
|
|
import (
|
2019-02-26 22:03:25 +00:00
|
|
|
"log"
|
2018-07-31 22:05:55 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/influxdata/telegraf"
|
|
|
|
"github.com/influxdata/telegraf/internal"
|
2018-10-05 19:55:23 +00:00
|
|
|
"github.com/influxdata/telegraf/internal/globpath"
|
2018-07-31 22:05:55 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/inputs"
|
2019-03-27 01:12:40 +00:00
|
|
|
"github.com/karrick/godirwalk"
|
|
|
|
"github.com/pkg/errors"
|
2018-07-31 22:05:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const sampleConfig = `
|
|
|
|
## Directory to gather stats about.
|
2018-10-05 19:55:23 +00:00
|
|
|
## deprecated in 1.9; use the directories option
|
2019-02-26 22:03:25 +00:00
|
|
|
# directory = "/var/cache/apt/archives"
|
2018-07-31 22:05:55 +00:00
|
|
|
|
2018-10-05 19:55:23 +00:00
|
|
|
## Directories to gather stats about.
|
|
|
|
## This accept standard unit glob matching rules, but with the addition of
|
|
|
|
## ** as a "super asterisk". ie:
|
|
|
|
## /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"]
|
|
|
|
|
2018-07-31 22:05:55 +00:00
|
|
|
## Only count files that match the name pattern. Defaults to "*".
|
|
|
|
name = "*.deb"
|
|
|
|
|
|
|
|
## Count files in subdirectories. Defaults to true.
|
|
|
|
recursive = false
|
|
|
|
|
|
|
|
## Only count regular files. Defaults to true.
|
|
|
|
regular_only = true
|
|
|
|
|
2018-10-19 18:17:18 +00:00
|
|
|
## Only count files that are at least this size. If size is
|
2018-07-31 22:05:55 +00:00
|
|
|
## a negative number, only count files that are smaller than the
|
2018-10-19 18:17:18 +00:00
|
|
|
## absolute value of size. Acceptable units are B, KiB, MiB, KB, ...
|
|
|
|
## Without quotes and units, interpreted as size in bytes.
|
|
|
|
size = "0B"
|
2018-07-31 22:05:55 +00:00
|
|
|
|
|
|
|
## Only count files that have not been touched for at least this
|
|
|
|
## duration. If mtime is negative, only count files that have been
|
|
|
|
## touched in this duration. Defaults to "0s".
|
|
|
|
mtime = "0s"
|
|
|
|
`
|
|
|
|
|
|
|
|
type FileCount struct {
|
2018-10-05 19:55:23 +00:00
|
|
|
Directory string // deprecated in 1.9
|
|
|
|
Directories []string
|
2018-07-31 22:05:55 +00:00
|
|
|
Name string
|
|
|
|
Recursive bool
|
|
|
|
RegularOnly bool
|
2018-10-19 18:17:18 +00:00
|
|
|
Size internal.Size
|
2018-07-31 22:05:55 +00:00
|
|
|
MTime internal.Duration `toml:"mtime"`
|
|
|
|
fileFilters []fileFilterFunc
|
2018-12-13 20:25:49 +00:00
|
|
|
globPaths []globpath.GlobPath
|
2019-06-24 18:03:05 +00:00
|
|
|
Fs fileSystem
|
2018-07-31 22:05:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (_ *FileCount) Description() string {
|
|
|
|
return "Count files in a directory"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (_ *FileCount) SampleConfig() string { return sampleConfig }
|
|
|
|
|
2018-12-13 20:25:49 +00:00
|
|
|
type fileFilterFunc func(os.FileInfo) (bool, error)
|
|
|
|
|
2018-07-31 22:05:55 +00:00
|
|
|
func rejectNilFilters(filters []fileFilterFunc) []fileFilterFunc {
|
|
|
|
filtered := make([]fileFilterFunc, 0, len(filters))
|
|
|
|
for _, f := range filters {
|
|
|
|
if f != nil {
|
|
|
|
filtered = append(filtered, f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return filtered
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fc *FileCount) nameFilter() fileFilterFunc {
|
|
|
|
if fc.Name == "*" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return func(f os.FileInfo) (bool, error) {
|
|
|
|
match, err := filepath.Match(fc.Name, f.Name())
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return match, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fc *FileCount) regularOnlyFilter() fileFilterFunc {
|
|
|
|
if !fc.RegularOnly {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return func(f os.FileInfo) (bool, error) {
|
|
|
|
return f.Mode().IsRegular(), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fc *FileCount) sizeFilter() fileFilterFunc {
|
2018-10-19 18:17:18 +00:00
|
|
|
if fc.Size.Size == 0 {
|
2018-07-31 22:05:55 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return func(f os.FileInfo) (bool, error) {
|
|
|
|
if !f.Mode().IsRegular() {
|
|
|
|
return false, nil
|
|
|
|
}
|
2018-10-19 18:17:18 +00:00
|
|
|
if fc.Size.Size < 0 {
|
|
|
|
return f.Size() < -fc.Size.Size, nil
|
2018-07-31 22:05:55 +00:00
|
|
|
}
|
2018-10-19 18:17:18 +00:00
|
|
|
return f.Size() >= fc.Size.Size, nil
|
2018-07-31 22:05:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fc *FileCount) mtimeFilter() fileFilterFunc {
|
|
|
|
if fc.MTime.Duration == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return func(f os.FileInfo) (bool, error) {
|
|
|
|
age := absDuration(fc.MTime.Duration)
|
|
|
|
mtime := time.Now().Add(-age)
|
|
|
|
if fc.MTime.Duration < 0 {
|
|
|
|
return f.ModTime().After(mtime), nil
|
|
|
|
}
|
|
|
|
return f.ModTime().Before(mtime), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func absDuration(x time.Duration) time.Duration {
|
|
|
|
if x < 0 {
|
|
|
|
return -x
|
|
|
|
}
|
|
|
|
return x
|
|
|
|
}
|
|
|
|
|
2018-12-13 20:25:49 +00:00
|
|
|
func (fc *FileCount) initFileFilters() {
|
|
|
|
filters := []fileFilterFunc{
|
|
|
|
fc.nameFilter(),
|
|
|
|
fc.regularOnlyFilter(),
|
|
|
|
fc.sizeFilter(),
|
|
|
|
fc.mtimeFilter(),
|
|
|
|
}
|
|
|
|
fc.fileFilters = rejectNilFilters(filters)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fc *FileCount) count(acc telegraf.Accumulator, basedir string, glob globpath.GlobPath) {
|
|
|
|
childCount := make(map[string]int64)
|
|
|
|
childSize := make(map[string]int64)
|
2019-02-26 22:03:25 +00:00
|
|
|
|
2018-12-13 20:25:49 +00:00
|
|
|
walkFn := func(path string, de *godirwalk.Dirent) error {
|
2019-03-27 01:12:40 +00:00
|
|
|
rel, err := filepath.Rel(basedir, path)
|
|
|
|
if err == nil && rel == "." {
|
2018-12-13 20:25:49 +00:00
|
|
|
return nil
|
|
|
|
}
|
2019-06-24 18:03:05 +00:00
|
|
|
file, err := fc.Fs.Stat(path)
|
2018-10-12 21:43:06 +00:00
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2018-10-05 19:55:23 +00:00
|
|
|
match, err := fc.filter(file)
|
|
|
|
if err != nil {
|
|
|
|
acc.AddError(err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if match {
|
2019-03-27 01:12:40 +00:00
|
|
|
parent := filepath.Dir(path)
|
2018-12-13 20:25:49 +00:00
|
|
|
childCount[parent]++
|
|
|
|
childSize[parent] += file.Size()
|
2018-10-05 19:55:23 +00:00
|
|
|
}
|
2018-12-13 20:25:49 +00:00
|
|
|
if file.IsDir() && !fc.Recursive && !glob.HasSuperMeta {
|
2018-07-31 22:05:55 +00:00
|
|
|
return filepath.SkipDir
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2019-02-26 22:03:25 +00:00
|
|
|
|
2018-12-13 20:25:49 +00:00
|
|
|
postChildrenFn := func(path string, de *godirwalk.Dirent) error {
|
|
|
|
if glob.MatchString(path) {
|
|
|
|
gauge := map[string]interface{}{
|
|
|
|
"count": childCount[path],
|
|
|
|
"size_bytes": childSize[path],
|
|
|
|
}
|
|
|
|
acc.AddGauge("filecount", gauge,
|
|
|
|
map[string]string{
|
|
|
|
"directory": path,
|
|
|
|
})
|
|
|
|
}
|
2019-03-27 01:12:40 +00:00
|
|
|
parent := filepath.Dir(path)
|
2018-12-13 20:25:49 +00:00
|
|
|
if fc.Recursive {
|
|
|
|
childCount[parent] += childCount[path]
|
|
|
|
childSize[parent] += childSize[path]
|
|
|
|
}
|
|
|
|
delete(childCount, path)
|
|
|
|
delete(childSize, path)
|
|
|
|
return nil
|
|
|
|
}
|
2018-10-05 19:55:23 +00:00
|
|
|
|
2018-12-13 20:25:49 +00:00
|
|
|
err := godirwalk.Walk(basedir, &godirwalk.Options{
|
|
|
|
Callback: walkFn,
|
|
|
|
PostChildrenCallback: postChildrenFn,
|
|
|
|
Unsorted: true,
|
2019-02-26 22:03:25 +00:00
|
|
|
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
|
|
|
|
},
|
2018-12-13 20:25:49 +00:00
|
|
|
})
|
2018-10-05 19:55:23 +00:00
|
|
|
if err != nil {
|
|
|
|
acc.AddError(err)
|
|
|
|
}
|
2018-07-31 22:05:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (fc *FileCount) filter(file os.FileInfo) (bool, error) {
|
|
|
|
if fc.fileFilters == nil {
|
|
|
|
fc.initFileFilters()
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, fileFilter := range fc.fileFilters {
|
|
|
|
match, err := fileFilter(file)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if !match {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fc *FileCount) Gather(acc telegraf.Accumulator) error {
|
2018-12-13 20:25:49 +00:00
|
|
|
if fc.globPaths == nil {
|
|
|
|
fc.initGlobPaths(acc)
|
2018-07-31 22:05:55 +00:00
|
|
|
}
|
|
|
|
|
2018-12-13 20:25:49 +00:00
|
|
|
for _, glob := range fc.globPaths {
|
2019-06-24 18:03:05 +00:00
|
|
|
for _, dir := range fc.onlyDirectories(glob.GetRoots()) {
|
2018-12-13 20:25:49 +00:00
|
|
|
fc.count(acc, dir, glob)
|
|
|
|
}
|
2018-10-05 19:55:23 +00:00
|
|
|
}
|
2018-07-31 22:05:55 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-24 18:03:05 +00:00
|
|
|
func (fc *FileCount) onlyDirectories(directories []string) []string {
|
2018-12-13 20:25:49 +00:00
|
|
|
out := make([]string, 0)
|
|
|
|
for _, path := range directories {
|
2019-06-24 18:03:05 +00:00
|
|
|
info, err := fc.Fs.Stat(path)
|
2018-12-13 20:25:49 +00:00
|
|
|
if err == nil && info.IsDir() {
|
|
|
|
out = append(out, path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2018-10-05 19:55:23 +00:00
|
|
|
func (fc *FileCount) getDirs() []string {
|
|
|
|
dirs := make([]string, len(fc.Directories))
|
|
|
|
for i, dir := range fc.Directories {
|
|
|
|
dirs[i] = dir
|
|
|
|
}
|
|
|
|
|
|
|
|
if fc.Directory != "" {
|
|
|
|
dirs = append(dirs, fc.Directory)
|
|
|
|
}
|
|
|
|
|
|
|
|
return dirs
|
|
|
|
}
|
|
|
|
|
2018-12-13 20:25:49 +00:00
|
|
|
func (fc *FileCount) initGlobPaths(acc telegraf.Accumulator) {
|
|
|
|
fc.globPaths = []globpath.GlobPath{}
|
|
|
|
for _, directory := range fc.getDirs() {
|
|
|
|
glob, err := globpath.Compile(directory)
|
2018-10-05 19:55:23 +00:00
|
|
|
if err != nil {
|
2018-12-13 20:25:49 +00:00
|
|
|
acc.AddError(err)
|
|
|
|
} else {
|
|
|
|
fc.globPaths = append(fc.globPaths, *glob)
|
2018-10-05 19:55:23 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-24 18:03:05 +00:00
|
|
|
|
2018-10-05 19:55:23 +00:00
|
|
|
}
|
|
|
|
|
2018-07-31 22:05:55 +00:00
|
|
|
func NewFileCount() *FileCount {
|
|
|
|
return &FileCount{
|
|
|
|
Directory: "",
|
2018-10-05 19:55:23 +00:00
|
|
|
Directories: []string{},
|
2018-07-31 22:05:55 +00:00
|
|
|
Name: "*",
|
|
|
|
Recursive: true,
|
|
|
|
RegularOnly: true,
|
2018-10-19 18:17:18 +00:00
|
|
|
Size: internal.Size{Size: 0},
|
2018-07-31 22:05:55 +00:00
|
|
|
MTime: internal.Duration{Duration: 0},
|
|
|
|
fileFilters: nil,
|
2019-06-24 18:03:05 +00:00
|
|
|
Fs: osFS{},
|
2018-07-31 22:05:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
inputs.Add("filecount", func() telegraf.Input {
|
|
|
|
return NewFileCount()
|
|
|
|
})
|
|
|
|
}
|