Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Architecture and Uname variable #207

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 38 additions & 4 deletions src/core/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/shirou/gopsutil/v3/host"
"github.com/shirou/gopsutil/v3/process"
log "github.com/sirupsen/logrus"
"golang.org/x/sys/unix"

"github.com/nginx/agent/sdk/v2/files"
"github.com/nginx/agent/sdk/v2/proto"
Expand Down Expand Up @@ -97,10 +98,10 @@ func (env *EnvironmentType) NewHostInfo(agentVersion string, tags *[]string, con
DisplayName: hostInformation.Hostname,
OsType: hostInformation.OS,
Uuid: env.GetSystemUUID(),
Uname: hostInformation.KernelArch,
Uname: env.GetUnixName(),
Partitons: diskPartitions(),
Network: env.networks(),
Processor: processors(),
Processor: processors(hostInformation.KernelArch),
Release: releaseInfo(),
Tags: *tags,
AgentAccessibleDirs: configDirs,
Expand All @@ -112,6 +113,33 @@ func (env *EnvironmentType) NewHostInfo(agentVersion string, tags *[]string, con
return env.host
}

/* Returns string containing
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 definition.
https://cs.opensource.google/search?q=utsname&ss=go%2Fx%2Fsys&start=11

TODO :- Make this function platform agnotic to pull uname (uname -a).
*/
func (env *EnvironmentType) GetUnixName() string {
var utsname unix.Utsname
err := unix.Uname(&utsname)
if(err != nil) {
log.Warnf("Unable to read Uname. Error: %v", err)
return ""
}
sysName := string(utsname.Sysname[:bytes.IndexByte(utsname.Sysname[:], 0)])
nodeName := string(utsname.Nodename[:bytes.IndexByte(utsname.Nodename[:], 0)])
release := string(utsname.Release[:bytes.IndexByte(utsname.Release[:], 0)])
version := string(utsname.Version[:bytes.IndexByte(utsname.Version[:], 0)])
machine := string(utsname.Machine[:bytes.IndexByte(utsname.Machine[:], 0)])
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 @@ -477,7 +505,7 @@ func (env *EnvironmentType) Processes() (result []Process) {
return processList
}

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 @@ -494,7 +522,13 @@ func processors() (res []*proto.CpuInfo) {
// https://stackoverflow.com/questions/21151765/cannot-unmarshal-string-into-go-value-of-type-int64
Model: item.Model,
Cores: item.Cores,
Architecture: item.Family,
/*
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
*/
//Homogeneous value of architecture.
Architecture: architecture,
Cpus: int32(len(cpus)),
Mhz: item.Mhz,
// TODO - check if this is correct
Expand Down
7 changes: 6 additions & 1 deletion src/core/environment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,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.Equal(t, env.GetUnixName(), host.Uname)
assert.NotEmpty(t, host.Release)
assert.Equal(t, tags, host.Tags)
}
Expand Down Expand Up @@ -370,9 +372,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.GreaterOrEqual(t, processorInfo[0].GetArchitecture(), "arm64")
}

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