Skip to content

Commit

Permalink
Discover host ip address netmasks (#346)
Browse files Browse the repository at this point in the history
* Improve host network interfaces discovery

* Simplify netmasks field
  • Loading branch information
arbulu89 authored Jul 22, 2024
1 parent 08a60b8 commit 5208bcb
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 25 deletions.
1 change: 1 addition & 0 deletions internal/core/hosts/discovered_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
22 changes: 15 additions & 7 deletions internal/discovery/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,15 @@ 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
}

host := hosts.DiscoveredHost{
OSVersion: getOSVersion(),
HostIPAddresses: ipAddresses,
Netmasks: netmasks,
HostName: d.host,
CPUCount: getLogicalCPUs(),
SocketCount: getCPUSocketCount(),
Expand All @@ -76,27 +77,34 @@ 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()
if err != nil {
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 {
Expand Down
1 change: 1 addition & 0 deletions internal/discovery/mocks/discovered_host_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
41 changes: 23 additions & 18 deletions test/fixtures/discovery/host/expected_published_host_discovery.json
Original file line number Diff line number Diff line change
@@ -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"
}
}

0 comments on commit 5208bcb

Please sign in to comment.