github.com/netdata/go.d.plugin@v0.58.1/modules/filecheck/filecheck.go (about)

     1  // SPDX-License-Identifier: GPL-3.0-or-later
     2  
     3  package filecheck
     4  
     5  import (
     6  	_ "embed"
     7  	"time"
     8  
     9  	"github.com/netdata/go.d.plugin/agent/module"
    10  	"github.com/netdata/go.d.plugin/pkg/web"
    11  )
    12  
    13  //go:embed "config_schema.json"
    14  var configSchema string
    15  
    16  func init() {
    17  	module.Register("filecheck", module.Creator{
    18  		JobConfigSchema: configSchema,
    19  		Defaults: module.Defaults{
    20  			UpdateEvery: 10,
    21  		},
    22  		Create: func() module.Module { return New() },
    23  	})
    24  }
    25  
    26  func New() *Filecheck {
    27  	return &Filecheck{
    28  		Config: Config{
    29  			DiscoveryEvery: web.Duration{Duration: time.Second * 30},
    30  			Files:          filesConfig{},
    31  			Dirs: dirsConfig{
    32  				CollectDirSize: true,
    33  			},
    34  		},
    35  		collectedFiles: make(map[string]bool),
    36  		collectedDirs:  make(map[string]bool),
    37  	}
    38  }
    39  
    40  type (
    41  	Config struct {
    42  		DiscoveryEvery web.Duration `yaml:"discovery_every"`
    43  		Files          filesConfig  `yaml:"files"`
    44  		Dirs           dirsConfig   `yaml:"dirs"`
    45  	}
    46  	filesConfig struct {
    47  		Include []string `yaml:"include"`
    48  		Exclude []string `yaml:"exclude"`
    49  	}
    50  	dirsConfig struct {
    51  		Include        []string `yaml:"include"`
    52  		Exclude        []string `yaml:"exclude"`
    53  		CollectDirSize bool     `yaml:"collect_dir_size"`
    54  	}
    55  )
    56  
    57  type Filecheck struct {
    58  	module.Base
    59  	Config `yaml:",inline"`
    60  
    61  	lastDiscoveryFiles time.Time
    62  	curFiles           []string
    63  	collectedFiles     map[string]bool
    64  
    65  	lastDiscoveryDirs time.Time
    66  	curDirs           []string
    67  	collectedDirs     map[string]bool
    68  
    69  	charts *module.Charts
    70  }
    71  
    72  func (Filecheck) Cleanup() {
    73  }
    74  
    75  func (fc *Filecheck) Init() bool {
    76  	err := fc.validateConfig()
    77  	if err != nil {
    78  		fc.Errorf("error on validating config: %v", err)
    79  		return false
    80  	}
    81  
    82  	charts, err := fc.initCharts()
    83  	if err != nil {
    84  		fc.Errorf("error on charts initialization: %v", err)
    85  		return false
    86  	}
    87  	fc.charts = charts
    88  
    89  	fc.Debugf("monitored files: %v", fc.Files.Include)
    90  	fc.Debugf("monitored dirs: %v", fc.Dirs.Include)
    91  	return true
    92  }
    93  
    94  func (fc Filecheck) Check() bool {
    95  	return true
    96  }
    97  
    98  func (fc *Filecheck) Charts() *module.Charts {
    99  	return fc.charts
   100  }
   101  
   102  func (fc *Filecheck) Collect() map[string]int64 {
   103  	ms, err := fc.collect()
   104  	if err != nil {
   105  		fc.Error(err)
   106  	}
   107  
   108  	if len(ms) == 0 {
   109  		return nil
   110  	}
   111  	return ms
   112  }