diff options
Diffstat (limited to 'collector/systemd_linux.go')
-rw-r--r-- | collector/systemd_linux.go | 44 |
1 files changed, 33 insertions, 11 deletions
diff --git a/collector/systemd_linux.go b/collector/systemd_linux.go index 0827069..57e9594 100644 --- a/collector/systemd_linux.go +++ b/collector/systemd_linux.go | |||
@@ -16,6 +16,7 @@ | |||
16 | package collector | 16 | package collector |
17 | 17 | ||
18 | import ( | 18 | import ( |
19 | "errors" | ||
19 | "fmt" | 20 | "fmt" |
20 | "math" | 21 | "math" |
21 | "regexp" | 22 | "regexp" |
@@ -39,8 +40,10 @@ const ( | |||
39 | ) | 40 | ) |
40 | 41 | ||
41 | var ( | 42 | var ( |
42 | unitWhitelist = kingpin.Flag("collector.systemd.unit-whitelist", "Regexp of systemd units to whitelist. Units must both match whitelist and not match blacklist to be included.").Default(".+").String() | 43 | unitInclude = kingpin.Flag("collector.systemd.unit-include", "Regexp of systemd units to include. Units must both match include and not match exclude to be included.").Default(".+").String() |
43 | unitBlacklist = kingpin.Flag("collector.systemd.unit-blacklist", "Regexp of systemd units to blacklist. Units must both match whitelist and not match blacklist to be included.").Default(".+\\.(automount|device|mount|scope|slice)").String() | 44 | oldUnitInclude = kingpin.Flag("collector.systemd.unit-whitelist", "DEPRECATED: Use --collector.systemd.unit-include").Hidden().String() |
45 | unitExclude = kingpin.Flag("collector.systemd.unit-exclude", "Regexp of systemd units to exclude. Units must both match include and not match exclude to be included.").Default(".+\\.(automount|device|mount|scope|slice)").String() | ||
46 | oldUnitExclude = kingpin.Flag("collector.systemd.unit-blacklist", "DEPRECATED: Use collector.systemd.unit-exclude").Hidden().String() | ||
44 | systemdPrivate = kingpin.Flag("collector.systemd.private", "Establish a private, direct connection to systemd without dbus (Strongly discouraged since it requires root. For testing purposes only).").Hidden().Bool() | 47 | systemdPrivate = kingpin.Flag("collector.systemd.private", "Establish a private, direct connection to systemd without dbus (Strongly discouraged since it requires root. For testing purposes only).").Hidden().Bool() |
45 | enableTaskMetrics = kingpin.Flag("collector.systemd.enable-task-metrics", "Enables service unit tasks metrics unit_tasks_current and unit_tasks_max").Bool() | 48 | enableTaskMetrics = kingpin.Flag("collector.systemd.enable-task-metrics", "Enables service unit tasks metrics unit_tasks_current and unit_tasks_max").Bool() |
46 | enableRestartsMetrics = kingpin.Flag("collector.systemd.enable-restarts-metrics", "Enables service unit metric service_restart_total").Bool() | 49 | enableRestartsMetrics = kingpin.Flag("collector.systemd.enable-restarts-metrics", "Enables service unit metric service_restart_total").Bool() |
@@ -61,8 +64,8 @@ type systemdCollector struct { | |||
61 | socketRefusedConnectionsDesc *prometheus.Desc | 64 | socketRefusedConnectionsDesc *prometheus.Desc |
62 | systemdVersionDesc *prometheus.Desc | 65 | systemdVersionDesc *prometheus.Desc |
63 | systemdVersion int | 66 | systemdVersion int |
64 | unitWhitelistPattern *regexp.Regexp | 67 | unitIncludePattern *regexp.Regexp |
65 | unitBlacklistPattern *regexp.Regexp | 68 | unitExcludePattern *regexp.Regexp |
66 | logger log.Logger | 69 | logger log.Logger |
67 | } | 70 | } |
68 | 71 | ||
@@ -118,8 +121,27 @@ func NewSystemdCollector(logger log.Logger) (Collector, error) { | |||
118 | systemdVersionDesc := prometheus.NewDesc( | 121 | systemdVersionDesc := prometheus.NewDesc( |
119 | prometheus.BuildFQName(namespace, subsystem, "version"), | 122 | prometheus.BuildFQName(namespace, subsystem, "version"), |
120 | "Detected systemd version", []string{}, nil) | 123 | "Detected systemd version", []string{}, nil) |
121 | unitWhitelistPattern := regexp.MustCompile(fmt.Sprintf("^(?:%s)$", *unitWhitelist)) | 124 | |
122 | unitBlacklistPattern := regexp.MustCompile(fmt.Sprintf("^(?:%s)$", *unitBlacklist)) | 125 | if *oldUnitExclude != "" { |
126 | if *unitExclude == "" { | ||
127 | level.Warn(logger).Log("msg", "--collector.systemd.unit-blacklist is DEPRECATED and will be removed in 2.0.0, use --collector.systemd.unit-exclude") | ||
128 | *unitExclude = *oldUnitExclude | ||
129 | } else { | ||
130 | return nil, errors.New("--collector.systemd.unit-blacklist and --collector.systemd.unit-exclude are mutually exclusive") | ||
131 | } | ||
132 | } | ||
133 | if *oldUnitInclude != "" { | ||
134 | if *unitInclude == "" { | ||
135 | level.Warn(logger).Log("msg", "--collector.systemd.unit-whitelist is DEPRECATED and will be removed in 2.0.0, use --collector.systemd.unit-include") | ||
136 | *unitInclude = *oldUnitInclude | ||
137 | } else { | ||
138 | return nil, errors.New("--collector.systemd.unit-whitelist and --collector.systemd.unit-include are mutually exclusive") | ||
139 | } | ||
140 | } | ||
141 | level.Info(logger).Log("msg", "Parsed flag --collector.systemd.unit-include", "flag", *unitInclude) | ||
142 | unitIncludePattern := regexp.MustCompile(fmt.Sprintf("^(?:%s)$", *unitInclude)) | ||
143 | level.Info(logger).Log("msg", "Parsed flag --collector.systemd.unit-exclude", "flag", *unitExclude) | ||
144 | unitExcludePattern := regexp.MustCompile(fmt.Sprintf("^(?:%s)$", *unitExclude)) | ||
123 | 145 | ||
124 | systemdVersion := getSystemdVersion(logger) | 146 | systemdVersion := getSystemdVersion(logger) |
125 | if systemdVersion < minSystemdVersionSystemState { | 147 | if systemdVersion < minSystemdVersionSystemState { |
@@ -141,8 +163,8 @@ func NewSystemdCollector(logger log.Logger) (Collector, error) { | |||
141 | socketRefusedConnectionsDesc: socketRefusedConnectionsDesc, | 163 | socketRefusedConnectionsDesc: socketRefusedConnectionsDesc, |
142 | systemdVersionDesc: systemdVersionDesc, | 164 | systemdVersionDesc: systemdVersionDesc, |
143 | systemdVersion: systemdVersion, | 165 | systemdVersion: systemdVersion, |
144 | unitWhitelistPattern: unitWhitelistPattern, | 166 | unitIncludePattern: unitIncludePattern, |
145 | unitBlacklistPattern: unitBlacklistPattern, | 167 | unitExcludePattern: unitExcludePattern, |
146 | logger: logger, | 168 | logger: logger, |
147 | }, nil | 169 | }, nil |
148 | } | 170 | } |
@@ -169,7 +191,7 @@ func (c *systemdCollector) Update(ch chan<- prometheus.Metric) error { | |||
169 | level.Debug(c.logger).Log("msg", "collectSummaryMetrics took", "duration_seconds", time.Since(begin).Seconds()) | 191 | level.Debug(c.logger).Log("msg", "collectSummaryMetrics took", "duration_seconds", time.Since(begin).Seconds()) |
170 | 192 | ||
171 | begin = time.Now() | 193 | begin = time.Now() |
172 | units := filterUnits(allUnits, c.unitWhitelistPattern, c.unitBlacklistPattern, c.logger) | 194 | units := filterUnits(allUnits, c.unitIncludePattern, c.unitExcludePattern, c.logger) |
173 | level.Debug(c.logger).Log("msg", "filterUnits took", "duration_seconds", time.Since(begin).Seconds()) | 195 | level.Debug(c.logger).Log("msg", "filterUnits took", "duration_seconds", time.Since(begin).Seconds()) |
174 | 196 | ||
175 | var wg sync.WaitGroup | 197 | var wg sync.WaitGroup |
@@ -443,10 +465,10 @@ func summarizeUnits(units []unit) map[string]float64 { | |||
443 | return summarized | 465 | return summarized |
444 | } | 466 | } |
445 | 467 | ||
446 | func filterUnits(units []unit, whitelistPattern, blacklistPattern *regexp.Regexp, logger log.Logger) []unit { | 468 | func filterUnits(units []unit, includePattern, excludePattern *regexp.Regexp, logger log.Logger) []unit { |
447 | filtered := make([]unit, 0, len(units)) | 469 | filtered := make([]unit, 0, len(units)) |
448 | for _, unit := range units { | 470 | for _, unit := range units { |
449 | if whitelistPattern.MatchString(unit.Name) && !blacklistPattern.MatchString(unit.Name) && unit.LoadState == "loaded" { | 471 | if includePattern.MatchString(unit.Name) && !excludePattern.MatchString(unit.Name) && unit.LoadState == "loaded" { |
450 | level.Debug(logger).Log("msg", "Adding unit", "unit", unit.Name) | 472 | level.Debug(logger).Log("msg", "Adding unit", "unit", unit.Name) |
451 | filtered = append(filtered, unit) | 473 | filtered = append(filtered, unit) |
452 | } else { | 474 | } else { |