Skip to content

Commit c93c811

Browse files
committed
systemd.go: Added systemd_version metric
Fixes: #27 Signed-off-by: Jonathan Davies <[email protected]>
1 parent 25c6295 commit c93c811

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

systemd/systemd.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ var (
5858
type Collector struct {
5959
ctx context.Context
6060
logger log.Logger
61+
systemdVersion *prometheus.Desc
6162
unitCPUTotal *prometheus.Desc
6263
unitState *prometheus.Desc
6364
unitInfo *prometheus.Desc
@@ -84,6 +85,11 @@ type Collector struct {
8485

8586
// NewCollector returns a new Collector exposing systemd statistics.
8687
func NewCollector(logger log.Logger) (*Collector, error) {
88+
systemdVersion := prometheus.NewDesc(
89+
prometheus.BuildFQName(namespace, "", "version"),
90+
"systemd version",
91+
[]string{"version"}, nil,
92+
)
8793
// Type is labeled twice e.g. name="foo.service" and type="service" to maintain compatibility
8894
// with users before we started exporting type label
8995
unitState := prometheus.NewDesc(
@@ -194,6 +200,7 @@ func NewCollector(logger log.Logger) (*Collector, error) {
194200
return &Collector{
195201
ctx: ctx,
196202
logger: logger,
203+
systemdVersion: systemdVersion,
197204
unitCPUTotal: unitCPUTotal,
198205
unitState: unitState,
199206
unitInfo: unitInfo,
@@ -228,6 +235,7 @@ func (c *Collector) Collect(ch chan<- prometheus.Metric) {
228235

229236
// Describe gathers descriptions of Metrics
230237
func (c *Collector) Describe(desc chan<- *prometheus.Desc) {
238+
desc <- c.systemdVersion
231239
desc <- c.unitCPUTotal
232240
desc <- c.unitState
233241
desc <- c.unitInfo
@@ -259,6 +267,15 @@ func (c *Collector) collect(ch chan<- prometheus.Metric) error {
259267
}
260268
defer conn.Close()
261269

270+
systemdVersion := c.getSystemdVersion(conn)
271+
272+
ch <- prometheus.MustNewConstMetric(
273+
c.systemdVersion,
274+
prometheus.GaugeValue,
275+
1,
276+
systemdVersion,
277+
)
278+
262279
allUnits, err := conn.ListUnitsContext(c.ctx)
263280
if err != nil {
264281
return errors.Wrap(err, "could not get list of systemd units from dbus")
@@ -620,3 +637,17 @@ func (c *Collector) filterUnits(units []dbus.UnitStatus, includePattern, exclude
620637

621638
return filtered
622639
}
640+
641+
func (c *Collector) getSystemdVersion(conn *dbus.Conn) string {
642+
version, err := conn.GetManagerProperty("Version")
643+
644+
if err != nil {
645+
level.Debug(c.logger).Log("msg", "Unable to get systemd version property, defaulting to 0")
646+
return "0"
647+
}
648+
649+
version = strings.TrimPrefix(strings.TrimSuffix(version, `"`), `"`)
650+
level.Debug(c.logger).Log("msg", "Got systemd version", "version", version)
651+
652+
return version
653+
}

0 commit comments

Comments
 (0)