forked from gridfuse/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
154 lines (128 loc) · 3.6 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
package main
import (
"bytes"
"errors"
"fmt"
"os"
"path/filepath"
"github.com/influxdata/toml"
"github.com/influxdata/toml/ast"
"github.com/influxdata/telegraf/config"
)
type pluginState map[string]bool
type selection map[string]pluginState
func ImportConfigurations(files, dirs []string) (*selection, int, error) {
sel := selection(make(map[string]pluginState))
// Initialize the categories
for _, category := range categories {
sel[category] = make(map[string]bool)
}
// 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 {
enabled := packageCollection{
packages: map[string][]packageInfo{},
}
for category, pkgs := range p.packages {
var categoryEnabledPackages []packageInfo
settings := (*s)[category]
for _, pkg := range pkgs {
if _, found := settings[pkg.Plugin]; found {
categoryEnabledPackages = append(categoryEnabledPackages, pkg)
}
}
enabled.packages[category] = categoryEnabledPackages
}
// Make sure we update the list of default parsers used by
// the remaining packages
enabled.FillDefaultParsers()
// If the user did not configure any parser, we want to include
// the default parsers if any to preserve a functional set of
// plugins.
if len(enabled.packages["parsers"]) == 0 && len(enabled.defaultParsers) > 0 {
var parsers []packageInfo
for _, pkg := range p.packages["parsers"] {
for _, name := range enabled.defaultParsers {
if pkg.Plugin == name {
parsers = append(parsers, pkg)
break
}
}
}
enabled.packages["parsers"] = parsers
}
return &enabled
}
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: %v", cfg, err)
}
if err := s.extractPluginsFromConfig(buf); err != nil {
return fmt.Errorf("extracting plugins from %q failed: %v", 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 {
categoryTbl, ok := subtbl.(*ast.Table)
if !ok {
continue
}
if _, found := (*s)[category]; !found {
continue
}
for name, data := range categoryTbl.Fields {
(*s)[category][name] = true
// We need to check the data_format field to get all required parsers
switch category {
case "inputs", "processors":
pluginTables, ok := data.([]*ast.Table)
if !ok {
continue
}
for _, subsubtbl := range pluginTables {
for field, fieldData := range subsubtbl.Fields {
if field != "data_format" {
continue
}
kv := fieldData.(*ast.KeyValue)
name := kv.Value.(*ast.String)
(*s)["parsers"][name.Value] = true
}
}
}
}
}
return nil
}
func trimBOM(f []byte) []byte {
return bytes.TrimPrefix(f, []byte("\xef\xbb\xbf"))
}