-
Notifications
You must be signed in to change notification settings - Fork 478
/
config.go
317 lines (261 loc) · 10.8 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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package node_exporter //nolint:golint
import (
"fmt"
"runtime"
"strings"
"time"
"github.com/cortexproject/cortex/pkg/util/flagext"
"github.com/go-kit/kit/log"
"github.com/grafana/agent/pkg/integrations"
"github.com/grafana/agent/pkg/integrations/config"
"github.com/prometheus/procfs"
"github.com/prometheus/procfs/sysfs"
"gopkg.in/alecthomas/kingpin.v2"
)
var (
// DefaultConfig holds non-zero default options for the Config when it is
// unmarshaled from YAML.
DefaultConfig = Config{
ProcFSPath: procfs.DefaultMountPoint,
SysFSPath: sysfs.DefaultMountPoint,
RootFSPath: "/",
DiskStatsIgnoredDevices: "^(ram|loop|fd|(h|s|v|xv)d[a-z]|nvme\\d+n\\d+p)\\d+$",
NetclassIgnoredDevices: "^$",
NetstatFields: "^(.*_(InErrors|InErrs)|Ip_Forwarding|Ip(6|Ext)_(InOctets|OutOctets)|Icmp6?_(InMsgs|OutMsgs)|TcpExt_(Listen.*|Syncookies.*|TCPSynRetrans)|Tcp_(ActiveOpens|InSegs|OutSegs|PassiveOpens|RetransSegs|CurrEstab)|Udp6?_(InDatagrams|OutDatagrams|NoPorts|RcvbufErrors|SndbufErrors))$",
NTPServer: "127.0.0.1",
NTPProtocolVersion: 4,
NTPIPTTL: 1,
NTPMaxDistance: time.Microsecond * 3466080,
NTPLocalOffsetTolerance: time.Millisecond,
PowersupplyIgnoredSupplies: "^$",
RunitServiceDir: "/etc/service",
SupervisordURL: "http://localhost:9001/RPC2",
SystemdUnitWhitelist: ".+",
SystemdUnitBlacklist: ".+\\.(automount|device|mount|scope|slice)",
VMStatFields: "^(oom_kill|pgpg|pswp|pg.*fault).*",
}
)
func init() {
// The default values for the filesystem collector are to ignore everything,
// but some platforms have specific defaults. We'll fill these in below at
// initialization time, but the values can still be overridden via the config
// file.
switch runtime.GOOS {
case "linux":
DefaultConfig.FilesystemIgnoredMountPoints = "^/(dev|proc|sys|var/lib/docker/.+)($|/)"
DefaultConfig.FilesystemIgnoredFSTypes = "^(autofs|binfmt_misc|bpf|cgroup2?|configfs|debugfs|devpts|devtmpfs|fusectl|hugetlbfs|iso9660|mqueue|nsfs|overlay|proc|procfs|pstore|rpc_pipefs|securityfs|selinuxfs|squashfs|sysfs|tracefs)$"
case "freebsd", "netbsd", "openbsd", "darwin":
DefaultConfig.FilesystemIgnoredMountPoints = "^/(dev)($|/)"
DefaultConfig.FilesystemIgnoredFSTypes = "^devfs$"
}
}
// Config controls the node_exporter integration.
type Config struct {
Common config.Common `yaml:",inline"`
IncludeExporterMetrics bool `yaml:"include_exporter_metrics,omitempty"`
ProcFSPath string `yaml:"procfs_path,omitempty"`
SysFSPath string `yaml:"sysfs_path,omitempty"`
RootFSPath string `yaml:"rootfs_path,omitempty"`
// Collectors to mark as enabled
EnableCollectors flagext.StringSlice `yaml:"enable_collectors,omitempty"`
// Collectors to mark as disabled
DisableCollectors flagext.StringSlice `yaml:"disable_collectors,omitempty"`
// Overrides the default set of enabled collectors with the collectors
// listed.
SetCollectors flagext.StringSlice `yaml:"set_collectors,omitempty"`
// Collector-specific config options
CPUEnableCPUInfo bool `yaml:"enable_cpu_info_metric,omitempty"`
DiskStatsIgnoredDevices string `yaml:"diskstats_ignored_devices,omitempty"`
FilesystemIgnoredMountPoints string `yaml:"filesystem_ignored_mount_points,omitempty"`
FilesystemIgnoredFSTypes string `yaml:"filesystem_ignored_fs_types,omitempty"`
NetclassIgnoredDevices string `yaml:"netclass_ignored_devices,omitempty"`
NetdevDeviceBlacklist string `yaml:"netdev_device_blacklist,omitempty"`
NetdevDeviceWhitelist string `yaml:"netdev_device_whitelist,omitempty"`
NetstatFields string `yaml:"netstat_fields,omitempty"`
NTPServer string `yaml:"ntp_server,omitempty"`
NTPProtocolVersion int `yaml:"ntp_protocol_version,omitempty"`
NTPServerIsLocal bool `yaml:"ntp_server_is_local,omitempty"`
NTPIPTTL int `yaml:"ntp_ip_ttl,omitempty"`
NTPMaxDistance time.Duration `yaml:"ntp_max_distance,omitempty"`
NTPLocalOffsetTolerance time.Duration `yaml:"ntp_local_offset_tolerance,omitempty"`
PerfCPUS string `yaml:"perf_cpus,omitempty"`
PerfTracepoint flagext.StringSlice `yaml:"perf_tracepoint,omitempty"`
PowersupplyIgnoredSupplies string `yaml:"powersupply_ignored_supplies,omitempty"`
RunitServiceDir string `yaml:"runit_service_dir,omitempty"`
SupervisordURL string `yaml:"supervisord_url,omitempty"`
SystemdUnitWhitelist string `yaml:"systemd_unit_whitelist,omitempty"`
SystemdUnitBlacklist string `yaml:"systemd_unit_blacklist,omitempty"`
SystemdEnableTaskMetrics bool `yaml:"systemd_enable_task_metrics,omitempty"`
SystemdEnableRestartsMetrics bool `yaml:"systemd_enable_restarts_metrics,omitempty"`
SystemdEnableStartTimeMetrics bool `yaml:"systemd_enable_start_time_metrics,omitempty"`
VMStatFields string `yaml:"vmstat_fields,omitempty"`
TextfileDirectory string `yaml:"textfile_directory,omitempty"`
}
// UnmarshalYAML implements yaml.Unmarshaler for Config.
func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {
*c = DefaultConfig
type plain Config
return unmarshal((*plain)(c))
}
// Name returns the name of the integration that this config represents.
func (c *Config) Name() string {
return "node_exporter"
}
// CommonConfig returns the common configs that are shared across all integrations.
func (c *Config) CommonConfig() config.Common {
return c.Common
}
// NewIntegration converts this config into an instance of an integration.
func (c *Config) NewIntegration(l log.Logger) (integrations.Integration, error) {
return New(l, c)
}
func init() {
integrations.RegisterIntegration(&Config{})
}
// MapConfigToNodeExporterFlags takes in a node_exporter Config and converts
// it to the set of flags that node_exporter usually expects when running as a
// separate binary.
func MapConfigToNodeExporterFlags(c *Config) (accepted []string, ignored []string) {
collectors := make(map[string]CollectorState, len(Collectors))
for k, v := range Collectors {
collectors[k] = v
}
// Override the set of defaults with the provided set of collectors if
// set_collectors has at least one element in it.
if len(c.SetCollectors) != 0 {
customDefaults := map[string]struct{}{}
for _, c := range c.SetCollectors {
customDefaults[c] = struct{}{}
}
for k := range collectors {
_, shouldEnable := customDefaults[k]
if shouldEnable {
collectors[k] = CollectorStateEnabled
} else {
collectors[k] = CollectorStateDisabled
}
}
}
// Explicitly disable/enable specific collectors
for _, c := range c.DisableCollectors {
collectors[c] = CollectorStateDisabled
}
for _, c := range c.EnableCollectors {
collectors[c] = CollectorStateEnabled
}
DisableUnavailableCollectors(collectors)
var flags flags
flags.accepted = append(flags.accepted, MapCollectorsToFlags(collectors)...)
flags.add(
"--path.procfs", c.ProcFSPath,
"--path.sysfs", c.SysFSPath,
"--path.rootfs", c.RootFSPath,
)
if collectors[CollectorCPU] {
flags.addBools(map[*bool]string{
&c.CPUEnableCPUInfo: "collector.cpu.info",
})
}
if collectors[CollectorDiskstats] {
flags.add("--collector.diskstats.ignored-devices", c.DiskStatsIgnoredDevices)
}
if collectors[CollectorFilesystem] {
flags.add(
"--collector.filesystem.ignored-mount-points", c.FilesystemIgnoredMountPoints,
"--collector.filesystem.ignored-fs-types", c.FilesystemIgnoredFSTypes,
)
}
if collectors[CollectorNetclass] {
flags.add("--collector.netclass.ignored-devices", c.NetclassIgnoredDevices)
}
if collectors[CollectorNetdev] {
flags.add(
"--collector.netdev.device-blacklist", c.NetdevDeviceBlacklist,
"--collector.netdev.device-whitelist", c.NetdevDeviceWhitelist,
)
}
if collectors[CollectorNetstat] {
flags.add("--collector.netstat.fields", c.NetstatFields)
}
if collectors[CollectorNTP] {
flags.add(
"--collector.ntp.server", c.NTPServer,
"--collector.ntp.protocol-version", fmt.Sprintf("%d", c.NTPProtocolVersion),
"--collector.ntp.ip-ttl", fmt.Sprintf("%d", c.NTPIPTTL),
"--collector.ntp.max-distance", c.NTPMaxDistance.String(),
"--collector.ntp.local-offset-tolerance", c.NTPLocalOffsetTolerance.String(),
)
flags.addBools(map[*bool]string{
&c.NTPServerIsLocal: "collector.ntp.server-is-local",
})
}
if collectors[CollectorPerf] {
flags.add("--collector.perf.cpus", c.PerfCPUS)
for _, tp := range c.PerfTracepoint {
flags.add("--collector.perf.tracepoint", tp)
}
}
if collectors[CollectorPowersuppply] {
flags.add("--collector.powersupply.ignored-supplies", c.PowersupplyIgnoredSupplies)
}
if collectors[CollectorRunit] {
flags.add("--collector.runit.servicedir", c.RunitServiceDir)
}
if collectors[CollectorSupervisord] {
flags.add("--collector.supervisord.url", c.SupervisordURL)
}
if collectors[CollectorSystemd] {
flags.add(
"--collector.systemd.unit-whitelist", c.SystemdUnitWhitelist,
"--collector.systemd.unit-blacklist", c.SystemdUnitBlacklist,
)
flags.addBools(map[*bool]string{
&c.SystemdEnableTaskMetrics: "collector.systemd.enable-task-metrics",
&c.SystemdEnableRestartsMetrics: "collector.systemd.enable-restarts-metrics",
&c.SystemdEnableStartTimeMetrics: "collector.systemd.enable-start-time-metrics",
})
}
if collectors[CollectorVMStat] {
flags.add("--collector.vmstat.fields", c.VMStatFields)
}
if collectors[CollectorTextfile] {
flags.add("--collector.textfile.directory", c.TextfileDirectory)
}
return flags.accepted, flags.ignored
}
type flags struct {
accepted []string
ignored []string
}
// add pushes new flags as key value pairs. If the flag isn't registered with kingpin,
// it will be ignored.
func (f *flags) add(kvp ...string) {
if (len(kvp) % 2) != 0 {
panic("missing value for added flag")
}
for i := 0; i < len(kvp); i += 2 {
key := kvp[i+0]
value := kvp[i+1]
rawFlag := strings.TrimPrefix(key, "--")
if kingpin.CommandLine.GetFlag(rawFlag) == nil {
f.ignored = append(f.ignored, rawFlag)
continue
}
f.accepted = append(f.accepted, key, value)
}
}
func (f *flags) addBools(m map[*bool]string) {
for setting, key := range m {
// The flag might not exist on this platform, so skip it if it's not
// defined.
if kingpin.CommandLine.GetFlag(key) == nil {
f.ignored = append(f.ignored, key)
continue
}
if *setting {
f.accepted = append(f.accepted, "--"+key)
} else {
f.accepted = append(f.accepted, "--no-"+key)
}
}
}