summaryrefslogtreecommitdiffstats
path: root/src/go/collectors/go.d.plugin/agent/discovery/file/read.go
blob: 1b45b37678bf8b4293583ad88c8c0530f94c6568 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// SPDX-License-Identifier: GPL-3.0-or-later

package file

import (
	"context"
	"fmt"
	"os"
	"path/filepath"
	"strings"

	"github.com/netdata/netdata/go/go.d.plugin/agent/confgroup"
	"github.com/netdata/netdata/go/go.d.plugin/logger"
)

type (
	staticConfig struct {
		confgroup.Default `yaml:",inline"`
		Jobs              []confgroup.Config `yaml:"jobs"`
	}
	sdConfig []confgroup.Config
)

func NewReader(reg confgroup.Registry, paths []string) *Reader {
	return &Reader{
		Logger: log,
		reg:    reg,
		paths:  paths,
	}
}

type Reader struct {
	*logger.Logger

	reg   confgroup.Registry
	paths []string
}

func (r *Reader) String() string {
	return r.Name()
}

func (r *Reader) Name() string {
	return "file reader"
}

func (r *Reader) Run(ctx context.Context, in chan<- []*confgroup.Group) {
	r.Info("instance is started")
	defer func() { r.Info("instance is stopped") }()

	select {
	case <-ctx.Done():
	case in <- r.groups():
	}

	close(in)
}

func (r *Reader) groups() (groups []*confgroup.Group) {
	for _, pattern := range r.paths {
		matches, err := filepath.Glob(pattern)
		if err != nil {
			continue
		}

		for _, path := range matches {
			if fi, err := os.Stat(path); err != nil || !fi.Mode().IsRegular() {
				continue
			}

			group, err := parse(r.reg, path)
			if err != nil {
				r.Warningf("parse '%s': %v", path, err)
				continue
			}

			if group == nil {
				group = &confgroup.Group{Source: path}
			} else {
				for _, cfg := range group.Configs {
					cfg.SetProvider("file reader")
					cfg.SetSourceType(configSourceType(path))
					cfg.SetSource(fmt.Sprintf("discoverer=file_reader,file=%s", path))
				}
			}
			groups = append(groups, group)
		}
	}

	return groups
}

func configSourceType(path string) string {
	if strings.Contains(path, "/etc/netdata") {
		return "user"
	}
	return "stock"
}