Skip to content

Commit

Permalink
Discover FQDN inside the host discovery loop (#313)
Browse files Browse the repository at this point in the history
* Add go-fqdn library

* Update tests

* Add FQDN discovery to the host loop
  • Loading branch information
dottorblaster authored Jan 15, 2024
1 parent 24bf295 commit 55335a2
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 37 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@ require (
)

require (
github.com/Showmax/go-fqdn v1.0.0
github.com/d-tux/go-fstab v0.0.0-20141204152952-eb4090f26517
github.com/iancoleman/strcase v0.2.0
github.com/moby/sys/mountinfo v0.6.2
github.com/spf13/afero v1.9.5
github.com/tredoe/osutil v1.0.6
golang.org/x/mod v0.9.0
github.com/moby/sys/mountinfo v0.6.2
)

require (
Expand All @@ -45,7 +46,6 @@ require (
github.com/golang/protobuf v1.5.3 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb // indirect
github.com/huandu/xstrings v1.4.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jinzhu/copier v0.3.5 // indirect
github.com/magiconair/properties v1.8.7 // indirect
Expand Down
10 changes: 2 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ cloud.google.com/go/storage v1.14.0/go.mod h1:GrKmX003DSIwi9o29oFT7YDnHYwZoctc3f
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/Showmax/go-fqdn v1.0.0 h1:0rG5IbmVliNT5O19Mfuvna9LL7zlHyRfsSvBPZmF9tM=
github.com/Showmax/go-fqdn v1.0.0/go.mod h1:SfrFBzmDCtCGrnHhoDjuvFnKsWjEQX/Q9ARZvOrJAko=
github.com/bufbuild/protocompile v0.4.0 h1:LbFKd2XowZvQ/kajzguUp2DC9UEIQhIq77fZZlaQsNA=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/chigopher/pathlib v0.15.0 h1:1pg96WL3iC1/YyWV4UJSl3E0GBf4B+h5amBtsbAAieY=
github.com/chigopher/pathlib v0.15.0/go.mod h1:3+YPPV21mU9vyw8Mjp+F33CyCfE6iOzinpiqBcccv7I=
github.com/chigopher/pathlib v1.0.0 h1:SbsCrFX4vDf4M2d8mT/RTzuVlKOjTKoPHK0HidsQFak=
github.com/chigopher/pathlib v1.0.0/go.mod h1:3+YPPV21mU9vyw8Mjp+F33CyCfE6iOzinpiqBcccv7I=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
Expand Down Expand Up @@ -155,8 +155,6 @@ github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb h1:b5rjCoWHc7eqmAS
github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM=
github.com/hooklift/gowsdl v0.5.0 h1:DE8RevqhGPLchumV/V7OwbCzfJ8lcozFg1uWC/ESCBQ=
github.com/hooklift/gowsdl v0.5.0/go.mod h1:9kRc402w9Ci/Mek5a1DNgTmU14yPY8fMumxNVvxhis4=
github.com/huandu/xstrings v1.4.0 h1:D17IlohoQq4UcpqD7fDk80P7l+lwAmlFaBHgOipl2FU=
github.com/huandu/xstrings v1.4.0/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
github.com/iancoleman/strcase v0.2.0 h1:05I4QRnGpI0m37iZQRuskXh+w77mr6Z41lwQzuHLwW0=
github.com/iancoleman/strcase v0.2.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
Expand Down Expand Up @@ -221,8 +219,6 @@ github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/spf13/afero v1.9.5 h1:stMpOSZFs//0Lv29HduCmli3GUfpFoF3Y1Q/aXj/wVM=
github.com/spf13/afero v1.9.5/go.mod h1:UBogFpq8E9Hx+xc5CNTTEpTnuHVmXDwZcZcE1eb/UhQ=
github.com/spf13/afero v1.10.0 h1:EaGW2JJh15aKOejeuJ+wpFSHnbd7GE6Wvp3TsNhb6LY=
github.com/spf13/afero v1.10.0/go.mod h1:UBogFpq8E9Hx+xc5CNTTEpTnuHVmXDwZcZcE1eb/UhQ=
github.com/spf13/cast v1.5.1 h1:R+kOtfhWQE6TVQzY+4D7wJLBgkdVasCEFxSUBYBYIlA=
github.com/spf13/cast v1.5.1/go.mod h1:b9PdjNptOpzXr7Rq1q9gJML/2cdGQAo69NKzQ10KN48=
github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I=
Expand Down Expand Up @@ -262,8 +258,6 @@ github.com/trento-project/contracts/go v0.0.0-20230823130307-95ed2147fa9d h1:bmE
github.com/trento-project/contracts/go v0.0.0-20230823130307-95ed2147fa9d/go.mod h1:FBvM/rVn4R7/2VdYrCNPaumbZ/vdhtT0L5Om3RZJgQg=
github.com/vektra/mockery/v2 v2.32.3 h1:ugtC6b34me/NenXXNR0Pr8UOKTllYodxyeyq0B/ak98=
github.com/vektra/mockery/v2 v2.32.3/go.mod h1:9lREs4VEeQiUS3rizYQx1saxHu2JiIhThP0q9+fDegM=
github.com/vektra/mockery/v2 v2.36.0 h1:TO4tST9f7lm4DHybPI2MC4Vb74bL89PtWvRhpNPAVo4=
github.com/vektra/mockery/v2 v2.36.0/go.mod h1:diB13hxXG6QrTR0ol2Rk8s2dRMftzvExSvPDKr+IYKk=
github.com/wagslane/go-rabbitmq v0.10.0 h1:y9Bw8Q/9gOvsHfjMOGQjCW3033aYTKabxDm8eyjUGjs=
github.com/wagslane/go-rabbitmq v0.10.0/go.mod h1:u6xM1V7OO4D0szUy/F6Bya/9r0lLae/2FXBijkAQmn0=
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down
17 changes: 9 additions & 8 deletions internal/core/hosts/discovered_host.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package hosts

type DiscoveredHost struct {
OSVersion string `json:"os_version"`
HostIPAddresses []string `json:"ip_addresses"`
HostName string `json:"hostname"`
CPUCount int `json:"cpu_count"`
SocketCount int `json:"socket_count"`
TotalMemoryMB int `json:"total_memory_mb"`
AgentVersion string `json:"agent_version"`
InstallationSource string `json:"installation_source"`
OSVersion string `json:"os_version"`
HostIPAddresses []string `json:"ip_addresses"`
HostName string `json:"hostname"`
CPUCount int `json:"cpu_count"`
SocketCount int `json:"socket_count"`
TotalMemoryMB int `json:"total_memory_mb"`
AgentVersion string `json:"agent_version"`
InstallationSource string `json:"installation_source"`
FullyQualifiedDomainName *string `json:"fully_qualified_domain_name,omitempty"`
}
38 changes: 29 additions & 9 deletions internal/discovery/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strconv"
"time"

"github.com/Showmax/go-fqdn"
"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/host"
"github.com/shirou/gopsutil/mem"
Expand All @@ -26,7 +27,11 @@ type HostDiscovery struct {
interval time.Duration
}

func NewHostDiscovery(collectorClient collector.Client, hostname string, config DiscoveriesConfig) Discovery {
func NewHostDiscovery(
collectorClient collector.Client,
hostname string,
config DiscoveriesConfig,
) Discovery {
return HostDiscovery{
id: HostDiscoveryID,
collectorClient: collectorClient,
Expand All @@ -51,14 +56,15 @@ func (d HostDiscovery) Discover(ctx context.Context) (string, error) {
}

host := hosts.DiscoveredHost{
OSVersion: getOSVersion(),
HostIPAddresses: ipAddresses,
HostName: d.host,
CPUCount: getLogicalCPUs(),
SocketCount: getCPUSocketCount(),
TotalMemoryMB: getTotalMemoryMB(),
AgentVersion: version.Version,
InstallationSource: version.InstallationSource,
OSVersion: getOSVersion(),
HostIPAddresses: ipAddresses,
HostName: d.host,
CPUCount: getLogicalCPUs(),
SocketCount: getCPUSocketCount(),
TotalMemoryMB: getTotalMemoryMB(),
AgentVersion: version.Version,
InstallationSource: version.InstallationSource,
FullyQualifiedDomainName: getHostFQDN(),
}

err = d.collectorClient.Publish(ctx, d.id, host)
Expand Down Expand Up @@ -93,6 +99,20 @@ func getHostIPAddresses() ([]string, error) {
return ipAddrList, nil
}

func getHostFQDN() *string {

fqdn, err := fqdn.FqdnHostname()
if err != nil {
log.Errorf("could not get the fully qualified domain name of the machine")
}

if len(fqdn) == 0 {
return nil
}

return &fqdn
}

func getOSVersion() string {
infoStat, err := host.Info()
if err != nil {
Expand Down
19 changes: 11 additions & 8 deletions internal/discovery/mocks/discovered_host_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@ package mocks
import "github.com/trento-project/agent/internal/core/hosts"

func NewDiscoveredHostMock() hosts.DiscoveredHost {
fqdn := "com.example.trento.host"

return hosts.DiscoveredHost{
OSVersion: "15-SP2",
HostIPAddresses: []string{"10.1.1.4", "10.1.1.5", "10.1.1.6"},
HostName: "thehostnamewherethediscoveryhappened",
CPUCount: 2,
SocketCount: 1,
TotalMemoryMB: 4096,
AgentVersion: "trento-agent-version",
InstallationSource: "Community",
OSVersion: "15-SP2",
HostIPAddresses: []string{"10.1.1.4", "10.1.1.5", "10.1.1.6"},
HostName: "thehostnamewherethediscoveryhappened",
CPUCount: 2,
SocketCount: 1,
TotalMemoryMB: 4096,
AgentVersion: "trento-agent-version",
InstallationSource: "Community",
FullyQualifiedDomainName: &fqdn,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"socket_count": 1,
"total_memory_mb": 4096,
"agent_version": "trento-agent-version",
"installation_source": "Community"
"installation_source": "Community",
"fully_qualified_domain_name": "com.example.trento.host"
}
}
}

0 comments on commit 55335a2

Please sign in to comment.