about summary refs log tree commit diff
path: root/main/osinfo
diff options
context:
space:
mode:
Diffstat (limited to 'main/osinfo')
-rw-r--r--main/osinfo/osinfo.go21
-rw-r--r--main/osinfo/osinfo_darwin.go24
-rw-r--r--main/osinfo/osinfo_default.go21
-rw-r--r--main/osinfo/osinfo_freebsd.go24
-rw-r--r--main/osinfo/osinfo_linux.go52
-rw-r--r--main/osinfo/osinfo_windows.go54
6 files changed, 196 insertions, 0 deletions
diff --git a/main/osinfo/osinfo.go b/main/osinfo/osinfo.go
new file mode 100644
index 0000000..7519a91
--- /dev/null
+++ b/main/osinfo/osinfo.go
@@ -0,0 +1,21 @@
+// Unless explicitly stated otherwise all files in this repository are licensed
+// under the Apache License Version 2.0.
+// This product includes software developed at Datadog (https://www.datadoghq.com/).
+// Copyright 2022 Datadog, Inc.
+
+// Package osinfo provides information about the current operating system release
+package osinfo
+
+// OSName returns the name of the operating system, including the distribution
+// for Linux when possible.
+func OSName() string {
+	// call out to OS-specific implementation
+	return osName()
+}
+
+// OSVersion returns the operating system release, e.g. major/minor version
+// number and build ID.
+func OSVersion() string {
+	// call out to OS-specific implementation
+	return osVersion()
+}
diff --git a/main/osinfo/osinfo_darwin.go b/main/osinfo/osinfo_darwin.go
new file mode 100644
index 0000000..32ead5f
--- /dev/null
+++ b/main/osinfo/osinfo_darwin.go
@@ -0,0 +1,24 @@
+// Unless explicitly stated otherwise all files in this repository are licensed
+// under the Apache License Version 2.0.
+// This product includes software developed at Datadog (https://www.datadoghq.com/).
+// Copyright 2016 Datadog, Inc.
+
+package osinfo
+
+import (
+	"os/exec"
+	"runtime"
+	"strings"
+)
+
+func osName() string {
+	return runtime.GOOS
+}
+
+func osVersion() string {
+	out, err := exec.Command("sw_vers", "-productVersion").Output()
+	if err != nil {
+		return "unknown"
+	}
+	return strings.Trim(string(out), "\n")
+}
diff --git a/main/osinfo/osinfo_default.go b/main/osinfo/osinfo_default.go
new file mode 100644
index 0000000..72d70d3
--- /dev/null
+++ b/main/osinfo/osinfo_default.go
@@ -0,0 +1,21 @@
+// Unless explicitly stated otherwise all files in this repository are licensed
+// under the Apache License Version 2.0.
+// This product includes software developed at Datadog (https://www.datadoghq.com/).
+// Copyright 2016 Datadog, Inc.
+
+//go:build !windows && !linux && !darwin && !freebsd
+// +build !windows,!linux,!darwin,!freebsd
+
+package osinfo
+
+import (
+	"runtime"
+)
+
+func osName() string {
+	return runtime.GOOS
+}
+
+func osVersion() string {
+	return "unknown"
+}
diff --git a/main/osinfo/osinfo_freebsd.go b/main/osinfo/osinfo_freebsd.go
new file mode 100644
index 0000000..543f2ff
--- /dev/null
+++ b/main/osinfo/osinfo_freebsd.go
@@ -0,0 +1,24 @@
+// Unless explicitly stated otherwise all files in this repository are licensed
+// under the Apache License Version 2.0.
+// This product includes software developed at Datadog (https://www.datadoghq.com/).
+// Copyright 2016 Datadog, Inc.
+
+package osinfo
+
+import (
+	"os/exec"
+	"runtime"
+	"strings"
+)
+
+func osName() string {
+	return runtime.GOOS
+}
+
+func osVersion() string {
+	out, err := exec.Command("uname", "-r").Output()
+	if err != nil {
+		return "unknown"
+	}
+	return strings.Split(string(out), "-")[0]
+}
diff --git a/main/osinfo/osinfo_linux.go b/main/osinfo/osinfo_linux.go
new file mode 100644
index 0000000..96d1e66
--- /dev/null
+++ b/main/osinfo/osinfo_linux.go
@@ -0,0 +1,52 @@
+// Unless explicitly stated otherwise all files in this repository are licensed
+// under the Apache License Version 2.0.
+// This product includes software developed at Datadog (https://www.datadoghq.com/).
+// Copyright 2016 Datadog, Inc.
+
+package osinfo
+
+import (
+	"bufio"
+	"os"
+	"strings"
+)
+
+func osName() string {
+	f, err := os.Open("/etc/os-release")
+	if err != nil {
+		return "Linux (Unknown Distribution)"
+	}
+	defer f.Close()
+	s := bufio.NewScanner(f)
+	name := "Linux (Unknown Distribution)"
+	for s.Scan() {
+		parts := strings.SplitN(s.Text(), "=", 2)
+		switch parts[0] {
+		case "NAME":
+			name = strings.Trim(parts[1], "\"")
+		}
+	}
+	return name
+}
+
+func osVersion() string {
+	f, err := os.Open("/etc/os-release")
+	if err != nil {
+		return "unknown"
+	}
+	defer f.Close()
+	s := bufio.NewScanner(f)
+	version := "unknown"
+	for s.Scan() {
+		parts := strings.SplitN(s.Text(), "=", 2)
+		switch parts[0] {
+		case "VERSION":
+			version = strings.Trim(parts[1], "\"")
+		case "VERSION_ID":
+			if version == "" {
+				version = strings.Trim(parts[1], "\"")
+			}
+		}
+	}
+	return version
+}
diff --git a/main/osinfo/osinfo_windows.go b/main/osinfo/osinfo_windows.go
new file mode 100644
index 0000000..659bd9c
--- /dev/null
+++ b/main/osinfo/osinfo_windows.go
@@ -0,0 +1,54 @@
+// Unless explicitly stated otherwise all files in this repository are licensed
+// under the Apache License Version 2.0.
+// This product includes software developed at Datadog (https://www.datadoghq.com/).
+// Copyright 2016 Datadog, Inc.
+
+package osinfo
+
+import (
+	"fmt"
+	"runtime"
+	"strings"
+
+	"golang.org/x/sys/windows/registry"
+)
+
+func osName() string {
+	return runtime.GOOS
+}
+
+func osVersion() string {
+	k, err := registry.OpenKey(registry.LOCAL_MACHINE, `SOFTWARE\Microsoft\Windows NT\CurrentVersion`, registry.QUERY_VALUE)
+	if err != nil {
+		return "unknown"
+	}
+	defer k.Close()
+
+	var version strings.Builder
+
+	maj, _, err := k.GetIntegerValue("CurrentMajorVersionNumber")
+	if err == nil {
+		version.WriteString(fmt.Sprintf("%d", maj))
+		min, _, err := k.GetIntegerValue("CurrentMinorVersionNumber")
+		if err == nil {
+			version.WriteString(fmt.Sprintf(".%d", min))
+		}
+	} else {
+		version.WriteString("unknown")
+	}
+
+	ed, _, err := k.GetStringValue("EditionID")
+	if err == nil {
+		version.WriteString(" " + ed)
+	} else {
+		version.WriteString(" Unknown Edition")
+	}
+
+	build, _, err := k.GetStringValue("CurrentBuild")
+	if err == nil {
+		version.WriteString(" Build " + build)
+	} else {
+		version.WriteString(" Unknown Build")
+	}
+	return version.String()
+}