diff --git a/internal/core/hosts/discovered_host.go b/internal/core/hosts/discovered_host.go index 474c4a61..723312b7 100644 --- a/internal/core/hosts/discovered_host.go +++ b/internal/core/hosts/discovered_host.go @@ -1,25 +1,14 @@ package hosts -type NetworkInterface struct { - Index int `json:"index"` - Name string `json:"name"` - Addresses []Address `json:"addresses"` -} - -type Address struct { - Address string `json:"address"` - Netmask int `json:"netmask"` -} - type DiscoveredHost struct { - OSVersion string `json:"os_version"` - HostIPAddresses []string `json:"ip_addresses"` // deprecated - NetworkInterfaces []NetworkInterface `json:"network_interfaces"` - 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"` + OSVersion string `json:"os_version"` + HostIPAddresses []string `json:"ip_addresses"` + Netmasks []int `json:"netmasks"` + 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"` } diff --git a/internal/discovery/host.go b/internal/discovery/host.go index 4cdd2d1a..12f8bffd 100644 --- a/internal/discovery/host.go +++ b/internal/discovery/host.go @@ -50,7 +50,7 @@ func (d HostDiscovery) GetInterval() time.Duration { // Execute one iteration of a discovery and publish to the collector func (d HostDiscovery) Discover(ctx context.Context) (string, error) { - networkInterfaces, ipAddresses, err := getNetworks() + ipAddresses, netmasks, err := getNetworksData() if err != nil { return "", err } @@ -58,7 +58,7 @@ func (d HostDiscovery) Discover(ctx context.Context) (string, error) { host := hosts.DiscoveredHost{ OSVersion: getOSVersion(), HostIPAddresses: ipAddresses, - NetworkInterfaces: networkInterfaces, + Netmasks: netmasks, HostName: d.host, CPUCount: getLogicalCPUs(), SocketCount: getCPUSocketCount(), @@ -77,16 +77,14 @@ func (d HostDiscovery) Discover(ctx context.Context) (string, error) { return fmt.Sprintf("Host with name: %s successfully discovered", d.host), nil } -// getNetworks still returns ip addresses list in 2nd return value -// for backward compatibility -func getNetworks() ([]hosts.NetworkInterface, []string, error) { +func getNetworksData() ([]string, []int, error) { interfaces, err := net.Interfaces() if err != nil { return nil, nil, err } ipAddrList := make([]string, 0) - networkInterfaces := make([]hosts.NetworkInterface, 0) + netmasks := make([]int, 0) for _, inter := range interfaces { addrs, err := inter.Addrs() @@ -94,12 +92,6 @@ func getNetworks() ([]hosts.NetworkInterface, []string, error) { continue } - newInterface := hosts.NetworkInterface{ - Index: inter.Index, - Name: inter.Name, - Addresses: make([]hosts.Address, 0), - } - for _, addr := range addrs { cidrAddr, ip, err := net.ParseCIDR(addr.String()) if err != nil { @@ -108,17 +100,11 @@ func getNetworks() ([]hosts.NetworkInterface, []string, error) { ipAddrList = append(ipAddrList, cidrAddr.String()) mask, _ := ip.Mask.Size() - newInterface.Addresses = append(newInterface.Addresses, hosts.Address{ - Address: cidrAddr.String(), - Netmask: mask, - }) - + netmasks = append(netmasks, mask) } - - networkInterfaces = append(networkInterfaces, newInterface) } - return networkInterfaces, ipAddrList, nil + return ipAddrList, netmasks, nil } func getHostFQDN() *string { diff --git a/internal/discovery/mocks/discovered_host_mock.go b/internal/discovery/mocks/discovered_host_mock.go index 0ace1d36..ed4842b3 100644 --- a/internal/discovery/mocks/discovered_host_mock.go +++ b/internal/discovery/mocks/discovered_host_mock.go @@ -6,38 +6,9 @@ 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"}, - NetworkInterfaces: []hosts.NetworkInterface{ - { - Index: 0, - Name: "eth0", - Addresses: []hosts.Address{ - { - Address: "10.1.1.4", - Netmask: 24, - }, - { - Address: "10.1.1.5", - Netmask: 16, - }, - { - Address: "10.1.1.6", - Netmask: 32, - }, - }, - }, - { - Index: 1, - Name: "eth1", - Addresses: []hosts.Address{ - { - Address: "10.1.2.4", - Netmask: 24, - }, - }, - }, - }, + OSVersion: "15-SP2", + HostIPAddresses: []string{"10.1.1.4", "10.1.1.5", "10.1.1.6"}, + Netmasks: []int{24, 16, 32}, HostName: "thehostnamewherethediscoveryhappened", CPUCount: 2, SocketCount: 1, diff --git a/test/fixtures/discovery/host/expected_published_host_discovery.json b/test/fixtures/discovery/host/expected_published_host_discovery.json index 69f75b7d..a6b49735 100644 --- a/test/fixtures/discovery/host/expected_published_host_discovery.json +++ b/test/fixtures/discovery/host/expected_published_host_discovery.json @@ -8,35 +8,10 @@ "10.1.1.5", "10.1.1.6" ], - "network_interfaces": [ - { - "index": 0, - "name": "eth0", - "addresses": [ - { - "address": "10.1.1.4", - "netmask": 24 - }, - { - "address": "10.1.1.5", - "netmask": 16 - }, - { - "address": "10.1.1.6", - "netmask": 32 - } - ] - }, - { - "index": 1, - "name": "eth1", - "addresses": [ - { - "address": "10.1.2.4", - "netmask": 24 - } - ] - } + "netmasks": [ + 24, + 16, + 32 ], "hostname": "thehostnamewherethediscoveryhappened", "cpu_count": 2,