summaryrefslogtreecommitdiffstats
path: root/src/go/collectors/go.d.plugin/agent/hostinfo/hostinfo_linux.go
blob: db2005f00787bd00e216a01a5b0e778b42a5b492 (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
// SPDX-License-Identifier: GPL-3.0-or-later

//go:build linux

package hostinfo

import (
	"context"
	"regexp"
	"strconv"

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

var SystemdVersion = getSystemdVersion()

func getSystemdVersion() int {
	var reVersion = regexp.MustCompile(`[0-9][0-9][0-9]`)

	conn, err := dbus.NewWithContext(context.Background())
	if err != nil {
		return 0
	}
	defer conn.Close()

	version, err := conn.GetManagerProperty("Version")
	if err != nil {
		return 0
	}

	major := reVersion.FindString(version)
	if major == "" {
		return 0
	}

	ver, err := strconv.Atoi(major)
	if err != nil {
		return 0
	}

	return ver
}