From 5da14042f70711ea5cf66e034699730335462f66 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 5 May 2024 14:08:03 +0200 Subject: Merging upstream version 1.45.3+dfsg. Signed-off-by: Daniel Baumann --- .../go.d.plugin/agent/hostinfo/hostinfo.go | 39 ++++++++++++++++++++ .../go.d.plugin/agent/hostinfo/hostinfo_common.go | 7 ++++ .../go.d.plugin/agent/hostinfo/hostinfo_linux.go | 42 ++++++++++++++++++++++ 3 files changed, 88 insertions(+) create mode 100644 src/go/collectors/go.d.plugin/agent/hostinfo/hostinfo.go create mode 100644 src/go/collectors/go.d.plugin/agent/hostinfo/hostinfo_common.go create mode 100644 src/go/collectors/go.d.plugin/agent/hostinfo/hostinfo_linux.go (limited to 'src/go/collectors/go.d.plugin/agent/hostinfo') diff --git a/src/go/collectors/go.d.plugin/agent/hostinfo/hostinfo.go b/src/go/collectors/go.d.plugin/agent/hostinfo/hostinfo.go new file mode 100644 index 000000000..48508a1c8 --- /dev/null +++ b/src/go/collectors/go.d.plugin/agent/hostinfo/hostinfo.go @@ -0,0 +1,39 @@ +// SPDX-License-Identifier: GPL-3.0-or-later + +package hostinfo + +import ( + "bytes" + "context" + "os" + "os/exec" + "time" +) + +var Hostname = getHostname() + +func getHostname() string { + path, err := exec.LookPath("hostname") + if err != nil { + return "" + } + + ctx, cancel := context.WithTimeout(context.Background(), time.Second*2) + defer cancel() + + bs, err := exec.CommandContext(ctx, path).Output() + if err != nil { + return "" + } + + return string(bytes.TrimSpace(bs)) +} + +var ( + envKubeHost = os.Getenv("KUBERNETES_SERVICE_HOST") + envKubePort = os.Getenv("KUBERNETES_SERVICE_PORT") +) + +func IsInsideK8sCluster() bool { + return envKubeHost != "" && envKubePort != "" +} diff --git a/src/go/collectors/go.d.plugin/agent/hostinfo/hostinfo_common.go b/src/go/collectors/go.d.plugin/agent/hostinfo/hostinfo_common.go new file mode 100644 index 000000000..69bbf5c78 --- /dev/null +++ b/src/go/collectors/go.d.plugin/agent/hostinfo/hostinfo_common.go @@ -0,0 +1,7 @@ +// SPDX-License-Identifier: GPL-3.0-or-later + +//go:build !linux + +package hostinfo + +var SystemdVersion int diff --git a/src/go/collectors/go.d.plugin/agent/hostinfo/hostinfo_linux.go b/src/go/collectors/go.d.plugin/agent/hostinfo/hostinfo_linux.go new file mode 100644 index 000000000..db2005f00 --- /dev/null +++ b/src/go/collectors/go.d.plugin/agent/hostinfo/hostinfo_linux.go @@ -0,0 +1,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 +} -- cgit v1.2.3