-
Notifications
You must be signed in to change notification settings - Fork 445
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
internal/osinfo: setup for kernel info (#2933)
Signed-off-by: Eliott Bouhana <[email protected]>
- Loading branch information
1 parent
f126bcf
commit 4b56ee5
Showing
10 changed files
with
166 additions
and
168 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +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 2022 Datadog, Inc. | ||
// Copyright 2024 Datadog, Inc. | ||
|
||
// Package osinfo provides information about the current operating system release | ||
package osinfo | ||
|
||
import ( | ||
"runtime" | ||
) | ||
|
||
// Modified in init functions to provide OS-specific information | ||
var ( | ||
osName = runtime.GOOS | ||
osVersion = "unknown" | ||
arch = runtime.GOARCH | ||
kernelName = "unknown" | ||
kernelRelease = "unknown" | ||
kernelVersion = "unknown" | ||
) | ||
|
||
// 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() | ||
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() | ||
return osVersion | ||
} | ||
|
||
// Architecture returns the architecture of the operating system. | ||
func Architecture() string { | ||
return arch | ||
} | ||
|
||
// KernelName returns the name of the kernel. | ||
func KernelName() string { | ||
return kernelName | ||
} | ||
|
||
// KernelRelease returns the release of the kernel. | ||
func KernelRelease() string { | ||
return kernelRelease | ||
} | ||
|
||
// KernelVersion returns the version of the kernel. | ||
func KernelVersion() string { | ||
return kernelVersion | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// 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 2024 Datadog, Inc. | ||
|
||
package osinfo_test | ||
|
||
import ( | ||
"runtime" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"golang.org/x/mod/semver" | ||
|
||
"gopkg.in/DataDog/dd-trace-go.v1/internal/osinfo" | ||
) | ||
|
||
func Test(t *testing.T) { | ||
var ( | ||
osName = osinfo.OSName() | ||
osVersion = osinfo.OSVersion() | ||
arch = osinfo.Architecture() | ||
kernelName = osinfo.KernelName() | ||
kernelRelease = osinfo.KernelRelease() | ||
kernelVersion = osinfo.KernelVersion() | ||
) | ||
|
||
t.Logf("OS Name: %s\n", osName) | ||
t.Logf("OS Version: %s\n", osVersion) | ||
t.Logf("Architecture: %s\n", arch) | ||
t.Logf("Kernel Name: %s\n", kernelName) | ||
t.Logf("Kernel Release: %s\n", kernelRelease) | ||
t.Logf("Kernel Version: %s\n", kernelVersion) | ||
|
||
switch runtime.GOOS { | ||
case "linux": | ||
require.Equal(t, "Linux", kernelName) | ||
require.Truef(t, semver.IsValid("v"+kernelRelease), "invalid kernel version: %s", kernelRelease) | ||
case "darwin": | ||
require.Equal(t, "Darwin", kernelName) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// 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 2024 Datadog, Inc. | ||
|
||
//go:build unix | ||
|
||
package osinfo | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"os" | ||
"os/exec" | ||
"runtime" | ||
"strings" | ||
|
||
"golang.org/x/sys/unix" | ||
) | ||
|
||
func init() { | ||
// Change the default values for backwards compatibility on scenarios | ||
if runtime.GOOS == "linux" { | ||
osName = "Linux (Unknown Distribution)" | ||
kernelName = "Linux" | ||
} | ||
|
||
if runtime.GOOS == "darwin" { | ||
kernelName = "Darwin" | ||
out, err := exec.Command("sw_vers", "-productVersion").Output() | ||
if err != nil { | ||
return | ||
} | ||
|
||
osVersion = string(bytes.Trim(out, "\n")) | ||
} | ||
|
||
var uts unix.Utsname | ||
if err := unix.Uname(&uts); err == nil { | ||
kernelName = string(bytes.TrimRight(uts.Sysname[:], "\x00")) | ||
kernelVersion = string(bytes.TrimRight(uts.Version[:], "\x00")) | ||
kernelRelease = strings.SplitN(strings.TrimRight(string(uts.Release[:]), "\x00"), "-", 2)[0] | ||
|
||
// Backwards compatibility on how data is reported for freebsd | ||
if runtime.GOOS == "freebsd" { | ||
osVersion = kernelRelease | ||
} | ||
} | ||
|
||
f, err := os.Open("/etc/os-release") | ||
if err != nil { | ||
return | ||
} | ||
|
||
defer f.Close() | ||
scanner := bufio.NewScanner(f) | ||
for scanner.Scan() { | ||
parts := strings.SplitN(scanner.Text(), "=", 2) | ||
switch parts[0] { | ||
case "NAME": | ||
osName = strings.Trim(parts[1], "\"") | ||
case "VERSION": | ||
osVersion = strings.Trim(parts[1], "\"") | ||
case "VERSION_ID": | ||
if osVersion == "" { // Fallback to VERSION_ID if VERSION is not set | ||
osVersion = strings.Trim(parts[1], "\"") | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.