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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
// SPDX-License-Identifier: GPL-3.0-or-later
package file
import (
"fmt"
"os"
"path/filepath"
"github.com/netdata/netdata/go/plugins/plugin/go.d/agent/confgroup"
"gopkg.in/yaml.v2"
)
type format int
const (
unknownFormat format = iota
unknownEmptyFormat
staticFormat
sdFormat
)
func parse(req confgroup.Registry, path string) (*confgroup.Group, error) {
bs, err := os.ReadFile(path)
if err != nil {
return nil, err
}
if len(bs) == 0 {
return nil, nil
}
switch cfgFormat(bs) {
case staticFormat:
return parseStaticFormat(req, path, bs)
case sdFormat:
return parseSDFormat(req, path, bs)
case unknownEmptyFormat:
return nil, nil
default:
return nil, fmt.Errorf("unknown file format: '%s'", path)
}
}
func parseStaticFormat(reg confgroup.Registry, path string, bs []byte) (*confgroup.Group, error) {
name := fileName(path)
// TODO: properly handle module renaming
// See agent/setup.go buildDiscoveryConf() for details
if name == "wmi" {
name = "windows"
}
modDef, ok := reg.Lookup(name)
if !ok {
return nil, nil
}
var modCfg staticConfig
if err := yaml.Unmarshal(bs, &modCfg); err != nil {
return nil, err
}
for _, cfg := range modCfg.Jobs {
cfg.SetModule(name)
def := mergeDef(modCfg.Default, modDef)
cfg.ApplyDefaults(def)
}
group := &confgroup.Group{
Configs: modCfg.Jobs,
Source: path,
}
return group, nil
}
func parseSDFormat(reg confgroup.Registry, path string, bs []byte) (*confgroup.Group, error) {
var cfgs sdConfig
if err := yaml.Unmarshal(bs, &cfgs); err != nil {
return nil, err
}
var i int
for _, cfg := range cfgs {
if def, ok := reg.Lookup(cfg.Module()); ok && cfg.Module() != "" {
cfg.ApplyDefaults(def)
cfgs[i] = cfg
i++
}
}
group := &confgroup.Group{
Configs: cfgs[:i],
Source: path,
}
return group, nil
}
func cfgFormat(bs []byte) format {
var data interface{}
if err := yaml.Unmarshal(bs, &data); err != nil {
return unknownFormat
}
if data == nil {
return unknownEmptyFormat
}
type (
static = map[any]any
sd = []any
)
switch data.(type) {
case static:
return staticFormat
case sd:
return sdFormat
default:
return unknownFormat
}
}
func mergeDef(a, b confgroup.Default) confgroup.Default {
return confgroup.Default{
MinUpdateEvery: firstPositive(a.MinUpdateEvery, b.MinUpdateEvery),
UpdateEvery: firstPositive(a.UpdateEvery, b.UpdateEvery),
AutoDetectionRetry: firstPositive(a.AutoDetectionRetry, b.AutoDetectionRetry),
Priority: firstPositive(a.Priority, b.Priority),
}
}
func firstPositive(value int, others ...int) int {
if value > 0 || len(others) == 0 {
return value
}
return firstPositive(others[0], others[1:]...)
}
func fileName(path string) string {
_, file := filepath.Split(path)
ext := filepath.Ext(path)
return file[:len(file)-len(ext)]
}
|