diff options
author | Ilya Mashchenko <ilya@netdata.cloud> | 2024-05-21 22:28:19 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-05-21 22:28:19 +0300 |
commit | e54b6164dd9f9c897e99ed17f802f014275f7578 (patch) | |
tree | 271532099ba8869049b5233ef1e952e694b952e8 /src/go/collectors/go.d.plugin/modules/systemdunits/collect_units.go | |
parent | 1b61aa9d215613c4c284fbfd5ddfb323d9fe4beb (diff) |
go.d systemdunits add "skip_transient" (#17725)
Diffstat (limited to 'src/go/collectors/go.d.plugin/modules/systemdunits/collect_units.go')
-rw-r--r-- | src/go/collectors/go.d.plugin/modules/systemdunits/collect_units.go | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/src/go/collectors/go.d.plugin/modules/systemdunits/collect_units.go b/src/go/collectors/go.d.plugin/modules/systemdunits/collect_units.go index 4cfc89d604..0cf97af03f 100644 --- a/src/go/collectors/go.d.plugin/modules/systemdunits/collect_units.go +++ b/src/go/collectors/go.d.plugin/modules/systemdunits/collect_units.go @@ -14,6 +14,8 @@ import ( "github.com/coreos/go-systemd/v22/dbus" ) +const transientProperty = "Transient" + const ( // https://www.freedesktop.org/software/systemd/man/systemd.html unitStateActive = "active" @@ -55,6 +57,20 @@ func (s *SystemdUnits) collectUnits(mx map[string]int64, conn systemdConnection) seen[unit.Name] = true + if s.SkipTransient { + if _, ok := s.unitTransient[unit.Name]; !ok { + prop, err := s.getUnitTransientProperty(conn, unit.Name) + if err != nil { + return err + } + prop = strings.Trim(prop, "\"") + s.unitTransient[unit.Name] = prop == "true" + } + if s.unitTransient[unit.Name] { + continue + } + } + if !s.seenUnits[unit.Name] { s.seenUnits[unit.Name] = true s.addUnitCharts(name, typ) @@ -75,6 +91,12 @@ func (s *SystemdUnits) collectUnits(mx map[string]int64, conn systemdConnection) } } + for k := range s.unitTransient { + if !seen[k] { + delete(s.unitTransient, k) + } + } + return nil } @@ -130,6 +152,20 @@ func (s *SystemdUnits) getLoadedUnitsByPatterns(conn systemdConnection) ([]dbus. return loaded, nil } +func (s *SystemdUnits) getUnitTransientProperty(conn systemdConnection, unit string) (string, error) { + ctx, cancel := context.WithTimeout(context.Background(), s.Timeout.Duration()) + defer cancel() + + s.Debugf("calling function 'GetUnitProperty' for unit '%s'", unit) + + prop, err := conn.GetUnitPropertyContext(ctx, unit, transientProperty) + if err != nil { + return "", fmt.Errorf("error on GetUnitProperty: %v", err) + } + + return prop.Value.String(), nil +} + func extractUnitNameType(name string) (string, string, bool) { idx := strings.LastIndexByte(name, '.') if idx <= 0 { |