master
go 98 lines 1.85 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package file
4
5 import (
6 "context"
7 "fmt"
8 "os"
9 "path/filepath"
10
11 "github.com/netdata/netdata/go/plugins/logger"
12 "github.com/netdata/netdata/go/plugins/pkg/pluginconfig"
13 "github.com/netdata/netdata/go/plugins/plugin/framework/confgroup"
14 )
15
16 type (
17 staticConfig struct {
18 confgroup.Default `yaml:",inline"`
19 Jobs []confgroup.Config `yaml:"jobs"`
20 }
21 sdConfig []confgroup.Config
22 )
23
24 func NewReader(reg confgroup.Registry, paths []string) *Reader {
25 return &Reader{
26 Logger: log,
27 reg: reg,
28 paths: paths,
29 }
30 }
31
32 type Reader struct {
33 *logger.Logger
34
35 reg confgroup.Registry
36 paths []string
37 }
38
39 func (r *Reader) String() string {
40 return r.Name()
41 }
42
43 func (r *Reader) Name() string {
44 return "file reader"
45 }
46
47 func (r *Reader) Run(ctx context.Context, in chan<- []*confgroup.Group) {
48 r.Info("instance is started")
49 defer func() { r.Info("instance is stopped") }()
50
51 select {
52 case <-ctx.Done():
53 case in <- r.groups():
54 }
55
56 close(in)
57 }
58
59 func (r *Reader) groups() (groups []*confgroup.Group) {
60 for _, pattern := range r.paths {
61 matches, err := filepath.Glob(pattern)
62 if err != nil {
63 continue
64 }
65
66 for _, path := range matches {
67 if fi, err := os.Stat(path); err != nil || !fi.Mode().IsRegular() {
68 continue
69 }
70
71 group, err := parse(r.reg, path)
72 if err != nil {
73 r.Warningf("parse '%s': %v", path, err)
74 continue
75 }
76
77 if group == nil {
78 group = &confgroup.Group{Source: path}
79 } else {
80 for _, cfg := range group.Configs {
81 cfg.SetProvider("file reader")
82 cfg.SetSourceType(configSourceType(path))
83 cfg.SetSource(fmt.Sprintf("discoverer=file_reader,file=%s", path))
84 }
85 }
86 groups = append(groups, group)
87 }
88 }
89
90 return groups
91 }
92
93 func configSourceType(path string) string {
94 if pluginconfig.IsStock(path) {
95 return confgroup.TypeStock
96 }
97 return confgroup.TypeUser
98 }