Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(inputs.systemd_units): Handle disabled multi-instance units correctly #14987

Merged
merged 2 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion plugins/inputs/systemd_units/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ See the [CONFIGURATION.md][CONFIGURATION.md] for more details.
# details = false

## Timeout for state-collection
# timeout = "1s"
# timeout = "5s"
```

This plugin supports two modes of operation:
Expand Down
2 changes: 1 addition & 1 deletion plugins/inputs/systemd_units/sample.conf
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@
# details = false

## Timeout for state-collection
# timeout = "1s"
# timeout = "5s"
2 changes: 1 addition & 1 deletion plugins/inputs/systemd_units/systemd_units.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ func (*SystemdUnits) SampleConfig() string {

func init() {
inputs.Add("systemd_units", func() telegraf.Input {
return &SystemdUnits{Timeout: config.Duration(time.Second)}
return &SystemdUnits{Timeout: config.Duration(5 * time.Second)}
})
}
20 changes: 19 additions & 1 deletion plugins/inputs/systemd_units/systemd_units_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,14 @@ func (s *SystemdUnits) Gather(acc telegraf.Accumulator) error {
continue
}
seen[name] = true

// Detect disabled multi-instance units and declare them as static
_, suffix, found := strings.Cut(name, "@")
instance, _, _ := strings.Cut(suffix, ".")
if found && instance == "" {
static = append(static, name)
continue
}
disabled = append(disabled, name)
case "static":
// Make sure we filter already loaded static multi-instance units
Expand Down Expand Up @@ -280,7 +288,17 @@ func (s *SystemdUnits) Gather(acc telegraf.Accumulator) error {
// Filter units of the wrong type
props, err := s.client.GetUnitTypePropertiesContext(ctx, state.Name, s.UnitType)
if err != nil {
continue
// Skip units returning "Unknown interface" errors as those indicate
// that the unit is of the wrong type.
if strings.Contains(err.Error(), "Unknown interface") {
continue
}
// For other units we make up properties, usually those are
// disabled multi-instance units
props = map[string]interface{}{
"StatusErrno": int64(-1),
"NRestarts": uint64(0),
}
}

u := unitInfo{
Expand Down
Loading