github.com/netdata/go.d.plugin@v0.58.1/modules/systemdunits/systemdunits.go (about) 1 // SPDX-License-Identifier: GPL-3.0-or-later 2 3 //go:build linux 4 // +build linux 5 6 package systemdunits 7 8 import ( 9 _ "embed" 10 "time" 11 12 "github.com/netdata/go.d.plugin/agent/module" 13 "github.com/netdata/go.d.plugin/pkg/matcher" 14 "github.com/netdata/go.d.plugin/pkg/web" 15 ) 16 17 //go:embed "config_schema.json" 18 var configSchema string 19 20 func init() { 21 module.Register("systemdunits", module.Creator{ 22 JobConfigSchema: configSchema, 23 Defaults: module.Defaults{ 24 UpdateEvery: 10, // gathering systemd units can be a CPU intensive op 25 }, 26 Create: func() module.Module { return New() }, 27 }) 28 } 29 30 func New() *SystemdUnits { 31 return &SystemdUnits{ 32 Config: Config{ 33 Include: []string{ 34 "*.service", 35 }, 36 Timeout: web.Duration{Duration: time.Second * 2}, 37 }, 38 39 charts: &module.Charts{}, 40 client: newSystemdDBusClient(), 41 units: make(map[string]bool), 42 } 43 } 44 45 type Config struct { 46 Include []string `yaml:"include"` 47 Timeout web.Duration `yaml:"timeout"` 48 } 49 50 type SystemdUnits struct { 51 module.Base 52 Config `yaml:",inline"` 53 54 client systemdClient 55 conn systemdConnection 56 57 systemdVersion int 58 units map[string]bool 59 sr matcher.Matcher 60 61 charts *module.Charts 62 } 63 64 func (s *SystemdUnits) Init() bool { 65 err := s.validateConfig() 66 if err != nil { 67 s.Errorf("config validation: %v", err) 68 return false 69 } 70 71 sr, err := s.initSelector() 72 if err != nil { 73 s.Errorf("init selector: %v", err) 74 return false 75 } 76 s.sr = sr 77 78 s.Debugf("unit names patterns: %v", s.Include) 79 s.Debugf("timeout: %s", s.Timeout) 80 return true 81 } 82 83 func (s *SystemdUnits) Check() bool { 84 return len(s.Collect()) > 0 85 } 86 87 func (s *SystemdUnits) Charts() *module.Charts { 88 return s.charts 89 } 90 91 func (s *SystemdUnits) Collect() map[string]int64 { 92 ms, err := s.collect() 93 if err != nil { 94 s.Error(err) 95 } 96 97 if len(ms) == 0 { 98 return nil 99 } 100 return ms 101 } 102 103 func (s *SystemdUnits) Cleanup() { 104 s.closeConnection() 105 }