From 5208bcb564c75b4501bbc3adf981f065f0d0efa0 Mon Sep 17 00:00:00 2001 From: Xabier Arbulu Insausti Date: Mon, 22 Jul 2024 09:48:48 +0200 Subject: [PATCH] Discover host ip address netmasks (#346) * Improve host network interfaces discovery * Simplify netmasks field --- internal/core/hosts/discovered_host.go | 1 + internal/discovery/host.go | 22 ++++++---- .../discovery/mocks/discovered_host_mock.go | 1 + .../expected_published_host_discovery.json | 41 +++++++++++-------- 4 files changed, 40 insertions(+), 25 deletions(-) diff --git a/internal/core/hosts/discovered_host.go b/internal/core/hosts/discovered_host.go index fb4ec510..723312b7 100644 --- a/internal/core/hosts/discovered_host.go +++ b/internal/core/hosts/discovered_host.go @@ -3,6 +3,7 @@ package hosts type DiscoveredHost struct { 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"` diff --git a/internal/discovery/host.go b/internal/discovery/host.go index 69ddd2a6..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) { - ipAddresses, err := getHostIPAddresses() + ipAddresses, netmasks, err := getNetworksData() if err != nil { return "", err } @@ -58,6 +58,7 @@ func (d HostDiscovery) Discover(ctx context.Context) (string, error) { host := hosts.DiscoveredHost{ OSVersion: getOSVersion(), HostIPAddresses: ipAddresses, + Netmasks: netmasks, HostName: d.host, CPUCount: getLogicalCPUs(), SocketCount: getCPUSocketCount(), @@ -76,13 +77,14 @@ func (d HostDiscovery) Discover(ctx context.Context) (string, error) { return fmt.Sprintf("Host with name: %s successfully discovered", d.host), nil } -func getHostIPAddresses() ([]string, error) { +func getNetworksData() ([]string, []int, error) { interfaces, err := net.Interfaces() if err != nil { - return nil, err + return nil, nil, err } ipAddrList := make([]string, 0) + netmasks := make([]int, 0) for _, inter := range interfaces { addrs, err := inter.Addrs() @@ -90,13 +92,19 @@ func getHostIPAddresses() ([]string, error) { continue } - for _, ipaddr := range addrs { - ipv4Addr, _, _ := net.ParseCIDR(ipaddr.String()) - ipAddrList = append(ipAddrList, ipv4Addr.String()) + for _, addr := range addrs { + cidrAddr, ip, err := net.ParseCIDR(addr.String()) + if err != nil { + continue + } + + ipAddrList = append(ipAddrList, cidrAddr.String()) + mask, _ := ip.Mask.Size() + netmasks = append(netmasks, mask) } } - return 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 b6320ddd..ed4842b3 100644 --- a/internal/discovery/mocks/discovered_host_mock.go +++ b/internal/discovery/mocks/discovered_host_mock.go @@ -8,6 +8,7 @@ func NewDiscoveredHostMock() hosts.DiscoveredHost { return hosts.DiscoveredHost{ 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 3472c9f6..a6b49735 100644 --- a/test/fixtures/discovery/host/expected_published_host_discovery.json +++ b/test/fixtures/discovery/host/expected_published_host_discovery.json @@ -1,19 +1,24 @@ { - "agent_id": "779cdd70-e9e2-58ca-b18a-bf3eb3f71244", - "discovery_type": "host_discovery", - "payload": { - "os_version": "15-SP2", - "ip_addresses": [ - "10.1.1.4", - "10.1.1.5", - "10.1.1.6" - ], - "hostname": "thehostnamewherethediscoveryhappened", - "cpu_count": 2, - "socket_count": 1, - "total_memory_mb": 4096, - "agent_version": "trento-agent-version", - "installation_source": "Community", - "fully_qualified_domain_name": "com.example.trento.host" - } -} + "agent_id": "779cdd70-e9e2-58ca-b18a-bf3eb3f71244", + "discovery_type": "host_discovery", + "payload": { + "os_version": "15-SP2", + "ip_addresses": [ + "10.1.1.4", + "10.1.1.5", + "10.1.1.6" + ], + "netmasks": [ + 24, + 16, + 32 + ], + "hostname": "thehostnamewherethediscoveryhappened", + "cpu_count": 2, + "socket_count": 1, + "total_memory_mb": 4096, + "agent_version": "trento-agent-version", + "installation_source": "Community", + "fully_qualified_domain_name": "com.example.trento.host" + } +} \ No newline at end of file