@@ -16,123 +16,37 @@ package discovery
1616import (
1717 "context"
1818 "fmt"
19- "strings"
2019 "time"
2120
22- systemd "github.com/coreos/go-systemd/v22/dbus"
2321 "github.com/go-kit/log"
2422 "github.com/go-kit/log/level"
25- systemd2 "github.com/marselester/systemd"
23+ systemd "github.com/marselester/systemd"
2624 "github.com/prometheus/common/model"
2725)
2826
2927type SystemdConfig struct {}
3028
31- type SystemdDiscoverer struct {
32- logger log. Logger
29+ func NewSystemdConfig () * SystemdConfig {
30+ return & SystemdConfig {}
3331}
3432
3533func (c * SystemdConfig ) Name () string {
3634 return "systemd"
3735}
3836
39- func NewSystemdConfig () * SystemdConfig {
40- return & SystemdConfig {}
41- }
42-
4337func (c * SystemdConfig ) NewDiscoverer (d DiscovererOptions ) (Discoverer , error ) {
4438 return & SystemdDiscoverer {
4539 logger : d .Logger ,
4640 }, nil
4741}
4842
49- func (c * SystemdDiscoverer ) Run (ctx context.Context , up chan <- []* Group ) error {
50- conn , err := systemd .NewWithContext (ctx )
51- if err != nil {
52- return fmt .Errorf ("failed to connect to systemd D-Bus API, %w" , err )
53- }
54- defer conn .Close ()
55-
56- isSubStateChanged := func (u1 , u2 * systemd.UnitStatus ) bool {
57- return u1 .SubState != u2 .SubState
58- }
59-
60- isNotService := func (name string ) bool {
61- return ! strings .HasSuffix (name , ".service" )
62- }
63-
64- updateCh , errCh := conn .SubscribeUnitsCustom (5 * time .Second , 0 , isSubStateChanged , isNotService )
65-
66- for {
67- select {
68- case update := <- updateCh :
69- var groups []* Group
70-
71- for unit , status := range update {
72- if status == nil || status .SubState != "running" {
73- groups = append (groups , & Group {Source : unit })
74- continue
75- }
76-
77- mainPIDProperty , err := conn .GetServicePropertyContext (ctx , unit , "MainPID" )
78- if err != nil {
79- level .Warn (c .logger ).Log ("msg" , "failed to get MainPID property for service" , "err" , err , "unit" , unit )
80- continue
81- }
82-
83- pid , ok := mainPIDProperty .Value .Value ().(uint32 )
84- if ! ok {
85- level .Warn (c .logger ).Log ("msg" , "failed to assert type of PID" , "unit" , unit )
86- continue
87- }
88-
89- groups = append (groups , & Group {
90- Targets : []model.LabelSet {{}},
91- Labels : model.LabelSet {
92- model .LabelName ("systemd_unit" ): model .LabelValue (unit ),
93- },
94- Source : unit ,
95- EntryPID : int (pid ),
96- })
97- }
98-
99- select {
100- case <- ctx .Done ():
101- return ctx .Err ()
102- case up <- groups :
103- }
104- case err := <- errCh :
105- level .Warn (c .logger ).Log ("msg" , "received error from systemd D-Bus API" , "err" , err )
106- case <- ctx .Done ():
107- return ctx .Err ()
108- }
109- }
110- }
111-
112- type Systemd2Config struct {}
113-
114- func NewSystemd2Config () * Systemd2Config {
115- return & Systemd2Config {}
116- }
117-
118- func (c * Systemd2Config ) Name () string {
119- return "systemd2"
120- }
121-
122- func (c * Systemd2Config ) NewDiscoverer (d DiscovererOptions ) (Discoverer , error ) {
123- return & Systemd2Discoverer {
124- logger : d .Logger ,
125- prev : make (map [string ]systemd2.Unit ),
126- }, nil
127- }
128-
129- type Systemd2Discoverer struct {
43+ type SystemdDiscoverer struct {
13044 logger log.Logger
131- prev map [string ]systemd2 .Unit
45+ units map [string ]systemd .Unit
13246}
13347
134- func (d * Systemd2Discoverer ) Run (ctx context.Context , up chan <- []* Group ) error {
135- c , err := systemd2 .New ()
48+ func (d * SystemdDiscoverer ) Run (ctx context.Context , up chan <- []* Group ) error {
49+ c , err := systemd .New ()
13650 if err != nil {
13751 return fmt .Errorf ("failed to connect to systemd D-Bus API, %w" , err )
13852 }
@@ -145,7 +59,7 @@ func (d *Systemd2Discoverer) Run(ctx context.Context, up chan<- []*Group) error
14559 for {
14660 select {
14761 case <- time .After (5 * time .Second ):
148- update , err := d .updatedUnits (c )
62+ update , err := d .unitsUpdate (c )
14963 if err != nil {
15064 level .Warn (d .logger ).Log ("msg" , "failed to get units from systemd D-Bus API" , "err" , err )
15165 continue
@@ -189,44 +103,42 @@ func (d *Systemd2Discoverer) Run(ctx context.Context, up chan<- []*Group) error
189103 }
190104}
191105
192- // updatedUnits is like SubscribeUnitsCustom
193- // from github.com/coreos/go-systemd/v22/dbus,
194- // i.e., it returns systemd units if there were any changes detected.
195- func (d * Systemd2Discoverer ) updatedUnits (c * systemd2.Client ) (map [string ]systemd2.Unit , error ) {
196- cur := make (map [string ]systemd2.Unit )
197- err := c .ListUnits (systemd2 .IsService , func (u * systemd2.Unit ) {
106+ // unitsUpdate returns systemd units if there were any changes detected.
107+ func (d * SystemdDiscoverer ) unitsUpdate (c * systemd.Client ) (map [string ]systemd.Unit , error ) {
108+ recent := make (map [string ]systemd.Unit )
109+ err := c .ListUnits (systemd .IsService , func (u * systemd.Unit ) {
198110 // Must copy a unit,
199111 // otherwise it will be modified on the next function call.
200- cur [u .Name ] = * u
112+ recent [u .Name ] = * u
201113 })
202114 if err != nil {
203115 return nil , err
204116 }
205117
206- // Collect all new and changed units.
207- changed := make (map [string ]systemd2 .Unit )
208- for name , unit := range cur {
209- prevUnit , ok := d .prev [ name ]
118+ // Collect new and changed units.
119+ update := make (map [string ]systemd .Unit )
120+ for unitName , unit := range recent {
121+ seenUnit , ok := d .units [ unitName ]
210122 // Is it a new unit or
211123 // the existing one but with an updated substate?
212- if ! ok || prevUnit .SubState != unit .SubState {
213- changed [ name ] = unit
124+ if ! ok || seenUnit .SubState != unit .SubState {
125+ update [ unitName ] = unit
214126 }
215127
216- delete (d .prev , name )
128+ delete (d .units , unitName )
217129 }
218130
219- // Add all deleted units.
220- for name := range d .prev {
221- changed [ name ] = systemd2 .Unit {}
131+ // Indicate that units were deleted .
132+ for unitName := range d .units {
133+ update [ unitName ] = systemd .Unit {}
222134 }
223135
224- d .prev = cur
136+ d .units = recent
225137
226138 // No changes.
227- if len (changed ) == 0 {
139+ if len (update ) == 0 {
228140 return nil , nil //nolint:nilnil
229141 }
230142
231- return changed , nil
143+ return update , nil
232144}
0 commit comments