Skip to content

Commit

Permalink
Host: add os.type (OSInfo.Type) from ECS 1.8 (#87)
Browse files Browse the repository at this point in the history
Adds a Type field to OSInfo, to align with ECS 1.8. Follows the ECS
rules:

"Use the os.type field to categorize the operating system into one of
the broad commercial families.

One of these following values should be used (lowercase):
 - linux
 - macos
 - unix
 - windows

If the OS you’re dealing with is not in the list,
the field should not be populated.
  • Loading branch information
adriansr authored Jan 14, 2021
1 parent 18ea948 commit fe39609
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- Added os.type field to host info. [#87](https://github.com/elastic/go-sysinfo/pull/87)

### Changed

### Deprecated
Expand Down
1 change: 1 addition & 0 deletions providers/aix/os_aix.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func getOSInfo() (*types.OSInfo, error) {
build := strings.SplitN(string(procVersion), "\n", 4)[2]

return &types.OSInfo{
Type: "unix",
Family: "aix",
Platform: "aix",
Name: "aix",
Expand Down
1 change: 1 addition & 0 deletions providers/darwin/os.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ func getOSInfo(data []byte) (*types.OSInfo, error) {
}

return &types.OSInfo{
Type: "macos",
Family: "darwin",
Platform: "darwin",
Name: productName,
Expand Down
1 change: 1 addition & 0 deletions providers/darwin/os_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func TestOperatingSystem(t *testing.T) {
t.Fatal(err)
}

assert.Equal(t, "macos", osInfo.Type)
assert.Equal(t, "darwin", osInfo.Family)
assert.Equal(t, "darwin", osInfo.Platform)
assert.Equal(t, "Mac OS X", osInfo.Name)
Expand Down
6 changes: 5 additions & 1 deletion providers/linux/os.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ func parseOSRelease(content []byte) (*types.OSInfo, error) {

func makeOSInfo(osRelease map[string]string) (*types.OSInfo, error) {
os := &types.OSInfo{
Type: "linux",
Platform: osRelease["ID"],
Name: osRelease["NAME"],
Version: osRelease["VERSION"],
Expand Down Expand Up @@ -238,7 +239,10 @@ func parseDistribRelease(platform string, content []byte) (*types.OSInfo, error)
var (
line = string(bytes.TrimSpace(content))
keys = distribReleaseRegexp.SubexpNames()
os = &types.OSInfo{Platform: platform}
os = &types.OSInfo{
Type: "linux",
Platform: platform,
}
)

for i, m := range distribReleaseRegexp.FindStringSubmatch(line) {
Expand Down
9 changes: 9 additions & 0 deletions providers/linux/os_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func TestOperatingSystem(t *testing.T) {
t.Fatal(err)
}
assert.Equal(t, types.OSInfo{
Type: "linux",
Family: "redhat",
Platform: "amzn",
Name: "Amazon Linux AMI",
Expand All @@ -50,6 +51,7 @@ func TestOperatingSystem(t *testing.T) {
t.Fatal(err)
}
assert.Equal(t, types.OSInfo{
Type: "linux",
Family: "redhat",
Platform: "centos",
Name: "CentOS",
Expand All @@ -66,6 +68,7 @@ func TestOperatingSystem(t *testing.T) {
t.Fatal(err)
}
assert.Equal(t, types.OSInfo{
Type: "linux",
Family: "redhat",
Platform: "centos",
Name: "CentOS Linux",
Expand All @@ -83,6 +86,7 @@ func TestOperatingSystem(t *testing.T) {
t.Fatal(err)
}
assert.Equal(t, types.OSInfo{
Type: "linux",
Family: "debian",
Platform: "debian",
Name: "Debian GNU/Linux",
Expand All @@ -98,6 +102,7 @@ func TestOperatingSystem(t *testing.T) {
t.Fatal(err)
}
assert.Equal(t, types.OSInfo{
Type: "linux",
Family: "debian",
Platform: "raspbian",
Name: "Raspbian GNU/Linux",
Expand All @@ -113,6 +118,7 @@ func TestOperatingSystem(t *testing.T) {
t.Fatal(err)
}
assert.Equal(t, types.OSInfo{
Type: "linux",
Family: "redhat",
Platform: "rhel",
Name: "Red Hat Enterprise Linux Server",
Expand All @@ -129,6 +135,7 @@ func TestOperatingSystem(t *testing.T) {
t.Fatal(err)
}
assert.Equal(t, types.OSInfo{
Type: "linux",
Family: "debian",
Platform: "ubuntu",
Name: "Ubuntu",
Expand All @@ -146,6 +153,7 @@ func TestOperatingSystem(t *testing.T) {
t.Fatal(err)
}
assert.Equal(t, types.OSInfo{
Type: "linux",
Family: "debian",
Platform: "ubuntu",
Name: "Ubuntu",
Expand All @@ -163,6 +171,7 @@ func TestOperatingSystem(t *testing.T) {
t.Fatal(err)
}
assert.Equal(t, types.OSInfo{
Type: "linux",
Family: "redhat",
Platform: "fedora",
Name: "Fedora",
Expand Down
1 change: 1 addition & 0 deletions providers/windows/os_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func OperatingSystem() (*types.OSInfo, error) {
defer k.Close()

osInfo := &types.OSInfo{
Type: "windows",
Family: "windows",
Platform: "windows",
}
Expand Down
1 change: 1 addition & 0 deletions types/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ func (host HostInfo) Uptime() time.Duration {

// OSInfo contains basic OS information
type OSInfo struct {
Type string `json:"type"` // OS Type (one of linux, macos, unix, windows).
Family string `json:"family"` // OS Family (e.g. redhat, debian, freebsd, windows).
Platform string `json:"platform"` // OS platform (e.g. centos, ubuntu, windows).
Name string `json:"name"` // OS Name (e.g. Mac OS X, CentOS).
Expand Down

0 comments on commit fe39609

Please sign in to comment.