summaryrefslogtreecommitdiffstats
path: root/src/go/collectors/go.d.plugin/modules/systemdunits/collect_units.go
blob: 0cf97af03f739c05a2a0b958bf9b485fb5dcbc41 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// SPDX-License-Identifier: GPL-3.0-or-later

//go:build linux
// +build linux

package systemdunits

import (
	"context"
	"fmt"
	"strconv"
	"strings"

	"github.com/coreos/go-systemd/v22/dbus"
)

const transientProperty = "Transient"

const (
	// https://www.freedesktop.org/software/systemd/man/systemd.html
	unitStateActive       = "active"
	unitStateInactive     = "inactive"
	unitStateActivating   = "activating"
	unitStateDeactivating = "deactivating"
	unitStateFailed       = "failed"
)

var unitStates = []string{
	unitStateActive,
	unitStateActivating,
	unitStateFailed,
	unitStateInactive,
	unitStateDeactivating,
}

func (s *SystemdUnits) collectUnits(mx map[string]int64, conn systemdConnection) error {
	var units []dbus.UnitStatus
	var err error

	if s.systemdVersion >= 230 {
		// https://github.com/systemd/systemd/pull/3142
		units, err = s.getLoadedUnitsByPatterns(conn)
	} else {
		units, err = s.getLoadedUnits(conn)
	}
	if err != nil {
		return err
	}

	seen := make(map[string]bool)

	for _, unit := range units {
		name, typ, ok := extractUnitNameType(unit.Name)
		if !ok {
			continue
		}

		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)
		}

		for _, s := range unitStates {
			mx[fmt.Sprintf("unit_%s_%s_state_%s", name, typ, s)] = 0
		}
		mx[fmt.Sprintf("unit_%s_%s_state_%s", name, typ, unit.ActiveState)] = 1
	}

	for k := range s.seenUnits {
		if !seen[k] {
			delete(s.seenUnits, k)
			if name, typ, ok := extractUnitNameType(k); ok {
				s.removeUnitCharts(name, typ)
			}
		}
	}

	for k := range s.unitTransient {
		if !seen[k] {
			delete(s.unitTransient, k)
		}
	}

	return nil
}

func (s *SystemdUnits) getLoadedUnits(conn systemdConnection) ([]dbus.UnitStatus, error) {
	ctx, cancel := context.WithTimeout(context.Background(), s.Timeout.Duration())
	defer cancel()

	s.Debugf("calling function 'ListUnits'")
	units, err := conn.ListUnitsContext(ctx)
	if err != nil {
		return nil, fmt.Errorf("error on ListUnits: %v", err)
	}

	for i := range units {
		units[i].Name = cleanUnitName(units[i].Name)
	}

	loaded := units[:0]
	for _, unit := range units {
		if unit.LoadState == "loaded" && s.unitSr.MatchString(unit.Name) {
			loaded = append(loaded, unit)
		}
	}

	s.Debugf("got total/loaded %d/%d units", len(units), len(loaded))

	return loaded, nil
}

func (s *SystemdUnits) getLoadedUnitsByPatterns(conn systemdConnection) ([]dbus.UnitStatus, error) {
	ctx, cancel := context.WithTimeout(context.Background(), s.Timeout.Duration())
	defer cancel()

	s.Debugf("calling function 'ListUnitsByPatterns'")

	units, err := conn.ListUnitsByPatternsContext(ctx, unitStates, s.Include)
	if err != nil {
		return nil, fmt.Errorf("error on ListUnitsByPatterns: %v", err)
	}

	for i := range units {
		units[i].Name = cleanUnitName(units[i].Name)
	}

	loaded := units[:0]
	for _, unit := range units {
		if unit.LoadState == "loaded" {
			loaded = append(loaded, unit)
		}
	}
	s.Debugf("got total/loaded %d/%d units", len(units), len(loaded))

	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 {
		return "", "", false
	}
	return name[:idx], name[idx+1:], true
}

func cleanUnitName(name string) string {
	// dev-disk-by\x2duuid-DE44\x2dCEE0.device => dev-disk-by-uuid-DE44-CEE0.device
	if strings.IndexByte(name, '\\') == -1 {
		return name
	}
	v, err := strconv.Unquote("\"" + name + "\"")
	if err != nil {
		return name
	}
	return v
}