Skip to content

Commit

Permalink
Fix architecture and uname (#239)
Browse files Browse the repository at this point in the history
  • Loading branch information
achawla2012 authored Mar 15, 2023
1 parent 38f4544 commit 34f8052
Show file tree
Hide file tree
Showing 12 changed files with 114 additions and 22 deletions.
2 changes: 1 addition & 1 deletion docs/proto/proto.md
Original file line number Diff line number Diff line change
Expand Up @@ -1005,7 +1005,7 @@ Represents the host system information
| display_name | [string](#string) | | Display Name |
| os_type | [string](#string) | | OS type (e.g. freebsd, linux, etc) |
| uuid | [string](#string) | | Host UUID |
| uname | [string](#string) | | The native cpu architecture queried at runtime, as returned by `uname -m` or empty string in case of error |
| uname | [string](#string) | | The native cpu architecture queried at runtime, as returned by `uname -a` or empty string in case of error |
| partitons | [DiskPartition](#f5-nginx-agent-sdk-DiskPartition) | repeated | List of disk partitions |
| network | [Network](#f5-nginx-agent-sdk-Network) | | Network information |
| processor | [CpuInfo](#f5-nginx-agent-sdk-CpuInfo) | repeated | List of CPU processor information |
Expand Down
2 changes: 1 addition & 1 deletion sdk/proto/host.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion sdk/proto/host.proto
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ message HostInfo {
string os_type = 5 [(gogoproto.jsontag) = "os-type"];
// Host UUID
string uuid = 6 [(gogoproto.jsontag) = "uuid"];
// The native cpu architecture queried at runtime, as returned by `uname -m` or empty string in case of error
// The native cpu architecture queried at runtime, as returned by `uname -a` or empty string in case of error
string uname = 7 [(gogoproto.jsontag) = "uname"];
// List of disk partitions
repeated DiskPartition partitons = 8 [(gogoproto.jsontag) = "disk_partitions"];
Expand Down
55 changes: 49 additions & 6 deletions src/core/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,10 @@ func (env *EnvironmentType) NewHostInfo(agentVersion string, tags *[]string, con
DisplayName: hostInformation.Hostname,
OsType: hostInformation.OS,
Uuid: env.GetSystemUUID(),
Uname: hostInformation.KernelArch,
Uname: getUnixName(),
Partitons: diskPartitions(),
Network: env.networks(),
Processor: processors(),
Processor: processors(hostInformation.KernelArch),
Release: releaseInfo(),
Tags: *tags,
AgentAccessibleDirs: configDirs,
Expand All @@ -121,6 +121,46 @@ func (env *EnvironmentType) NewHostInfo(agentVersion string, tags *[]string, con
return env.host
}

// getUnixName returns details about this operating system formatted as "sysname
// nodename release version machine". Returns "" if unix name cannot be
// determined.
//
// - sysname: Name of the operating system implementation.
// - nodename: Network name of this machine.
// - release: Release level of the operating system.
// - version: Version level of the operating system.
// - machine: Machine hardware platform.
//
// Different platforms have different [Utsname] struct definitions.
//
// TODO :- Make this function platform agnostic to pull uname (uname -a).
//
// [Utsname]: https://cs.opensource.google/search?q=utsname&ss=go%2Fx%2Fsys&start=1
func getUnixName() string {
var utsname unix.Utsname
err := unix.Uname(&utsname)
if err != nil {
log.Warnf("Unable to read Uname. Error: %v", err)
return ""
}

toStr := func(buf []byte) string {
idx := bytes.IndexByte(buf, 0)
if idx == -1 {
return "unknown"
}
return string(buf[:idx])
}

sysName := toStr(utsname.Sysname[:])
nodeName := toStr(utsname.Nodename[:])
release := toStr(utsname.Release[:])
version := toStr(utsname.Version[:])
machine := toStr(utsname.Machine[:])

return strings.Join([]string{sysName, nodeName, release, version, machine}, " ")
}

func (env *EnvironmentType) GetHostname() string {
hostInformation, err := host.Info()
if err != nil {
Expand Down Expand Up @@ -565,7 +605,7 @@ func callSyscall(mib []int32) ([]byte, uint64, error) {
return buf, length, nil
}

func processors() (res []*proto.CpuInfo) {
func processors(architecture string) (res []*proto.CpuInfo) {
log.Debug("Reading CPU information for dataplane host")
cpus, err := cpu.Info()
if err != nil {
Expand All @@ -580,9 +620,12 @@ func processors() (res []*proto.CpuInfo) {
// TODO: Model is a number
// wait to see if unmarshalling error on control plane side is fixed with switch in models
// https://stackoverflow.com/questions/21151765/cannot-unmarshal-string-into-go-value-of-type-int64
Model: item.Model,
Cores: item.Cores,
Architecture: item.Family,
Model: item.Model,
Cores: item.Cores,
// cpu_info does not provide architecture info.
// Fix was to add KernelArch field in InfoStat struct that returns 'uname -m'
// https://github.com/shirou/gopsutil/issues/737
Architecture: architecture,
Cpus: int32(len(cpus)),
Mhz: item.Mhz,
// TODO - check if this is correct
Expand Down
8 changes: 7 additions & 1 deletion src/core/environment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ package core
import (
"io/ioutil"
"os"
"strings"
"testing"

"github.com/nginx/agent/sdk/v2"
Expand Down Expand Up @@ -369,6 +370,8 @@ func TestNewHostInfo(t *testing.T) {
assert.GreaterOrEqual(t, len(host.Partitons), 1)
assert.GreaterOrEqual(t, len(host.Network.Interfaces), 1)
assert.GreaterOrEqual(t, len(host.Processor), 1)
assert.NotEmpty(t, host.Processor[0].Architecture)
assert.GreaterOrEqual(t, len(strings.Split(host.Uname, " ")), 5)
assert.NotEmpty(t, host.Release)
assert.Equal(t, tags, host.Tags)
}
Expand Down Expand Up @@ -408,9 +411,12 @@ func TestVirtualization(t *testing.T) {
}

func TestProcessors(t *testing.T) {
processorInfo := processors()
processorInfo := processors("arm64")
// at least one network interface
assert.GreaterOrEqual(t, processorInfo[0].GetCpus(), int32(1))
// non empty architecture
assert.NotEmpty(t, processorInfo[0].GetArchitecture())
assert.Equal(t, "arm64", processorInfo[0].GetArchitecture())
}

func TestParseLscpu(t *testing.T) {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/github.com/nginx/agent/sdk/v2/proto/host.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/github.com/nginx/agent/sdk/v2/proto/host.proto

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 34f8052

Please sign in to comment.