forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
232 lines (201 loc) · 5.7 KB
/
config.go
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package main
import (
"bytes"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/influxdata/toml"
"github.com/influxdata/toml/ast"
"github.com/influxdata/telegraf/config"
)
type instance struct {
category string
name string
enabled bool
dataformat []string
}
type selection struct {
plugins map[string][]instance
}
func ImportConfigurations(files, dirs []string) (*selection, int, error) {
sel := &selection{
plugins: make(map[string][]instance),
}
// Gather all configuration files
var filenames []string
filenames = append(filenames, files...)
for _, dir := range dirs {
// Walk the directory and get the packages
elements, err := os.ReadDir(dir)
if err != nil {
return nil, 0, fmt.Errorf("reading directory %q failed: %w", dir, err)
}
for _, element := range elements {
if element.IsDir() || filepath.Ext(element.Name()) != ".conf" {
continue
}
filenames = append(filenames, filepath.Join(dir, element.Name()))
}
}
if len(filenames) == 0 {
return sel, 0, errors.New("no configuration files given or found")
}
// Do the actual import
err := sel.importFiles(filenames)
return sel, len(filenames), err
}
func (s *selection) Filter(p packageCollection) (*packageCollection, error) {
enabled := packageCollection{
packages: make(map[string][]packageInfo),
}
implicitlyConfigured := make(map[string]bool)
for category, pkgs := range p.packages {
for _, pkg := range pkgs {
key := category + "." + pkg.Plugin
instances, found := s.plugins[key]
if !found {
continue
}
// The package was configured so add it to the enabled list
enabled.packages[category] = append(enabled.packages[category], pkg)
// Check if the instances configured a data-format and decide if it
// is a parser or serializer depending on the plugin type.
// If no data-format was configured, check the default settings in
// case this plugin supports a data-format setting but the user
// didn't set it.
for _, instance := range instances {
for _, dataformat := range instance.dataformat {
switch category {
case "inputs":
implicitlyConfigured["parsers."+dataformat] = true
case "processors":
implicitlyConfigured["parsers."+dataformat] = true
// The execd processor requires both a parser and serializer
if pkg.Plugin == "execd" {
implicitlyConfigured["serializers."+dataformat] = true
}
case "outputs":
implicitlyConfigured["serializers."+dataformat] = true
}
}
if len(instance.dataformat) == 0 {
if pkg.DefaultParser != "" {
implicitlyConfigured["parsers."+pkg.DefaultParser] = true
}
if pkg.DefaultSerializer != "" {
implicitlyConfigured["serializers."+pkg.DefaultSerializer] = true
}
}
}
}
}
// Iterate over all plugins AGAIN to add the implicitly configured packages
// such as parsers and serializers
for category, pkgs := range p.packages {
for _, pkg := range pkgs {
key := category + "." + pkg.Plugin
// Skip the plugins that were explicitly configured as we already
// added them above.
if _, found := s.plugins[key]; found {
continue
}
// Add the package if it was implicitly configured e.g. by a
// 'data_format' setting or by a default value for the data-format
if _, implicit := implicitlyConfigured[key]; implicit {
enabled.packages[category] = append(enabled.packages[category], pkg)
}
}
}
// Check if all packages in the config were covered
available := make(map[string]bool)
for category, pkgs := range p.packages {
for _, pkg := range pkgs {
available[category+"."+pkg.Plugin] = true
}
}
var unknown []string
for pkg := range s.plugins {
if !available[pkg] {
unknown = append(unknown, pkg)
}
}
for pkg := range implicitlyConfigured {
if !available[pkg] {
unknown = append(unknown, pkg)
}
}
if len(unknown) > 0 {
return nil, fmt.Errorf("configured but unknown packages %q", strings.Join(unknown, ","))
}
return &enabled, nil
}
func (s *selection) importFiles(configurations []string) error {
for _, cfg := range configurations {
buf, _, err := config.LoadConfigFile(cfg)
if err != nil {
return fmt.Errorf("reading %q failed: %w", cfg, err)
}
if err := s.extractPluginsFromConfig(buf); err != nil {
return fmt.Errorf("extracting plugins from %q failed: %w", cfg, err)
}
}
return nil
}
func (s *selection) extractPluginsFromConfig(buf []byte) error {
table, err := toml.Parse(trimBOM(buf))
if err != nil {
return fmt.Errorf("parsing TOML failed: %w", err)
}
for category, subtbl := range table.Fields {
// Check if we should handle the category, i.e. it contains plugins
// to configure.
var valid bool
for _, c := range categories {
if c == category {
valid = true
break
}
}
if !valid {
continue
}
categoryTbl, ok := subtbl.(*ast.Table)
if !ok {
continue
}
for name, data := range categoryTbl.Fields {
key := category + "." + name
cfg := instance{
category: category,
name: name,
enabled: true,
}
// We need to check the data_format field to get all required
// parsers and serializers
pluginTables, ok := data.([]*ast.Table)
if ok {
for _, subsubtbl := range pluginTables {
var dataformat string
for field, fieldData := range subsubtbl.Fields {
if field != "data_format" {
continue
}
kv := fieldData.(*ast.KeyValue)
option := kv.Value.(*ast.String)
dataformat = option.Value
}
if dataformat != "" {
cfg.dataformat = append(cfg.dataformat, dataformat)
}
}
}
s.plugins[key] = append(s.plugins[key], cfg)
}
}
return nil
}
func trimBOM(f []byte) []byte {
return bytes.TrimPrefix(f, []byte("\xef\xbb\xbf"))
}