Skip to content

Commit

Permalink
Add ip_addresses_netmasks field to host discovery
Browse files Browse the repository at this point in the history
  • Loading branch information
rtorrero committed Jun 20, 2024
1 parent 58fd08c commit e239f6d
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 4 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"`
HostIPAddressesNetmasks []string `json:"ip_addresses_netmasks"`
HostName string `json:"hostname"`
CPUCount int `json:"cpu_count"`
SocketCount int `json:"socket_count"`
Expand Down
11 changes: 7 additions & 4 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, ipAddressesNetmasks, err := getHostIPAddresses()
if err != nil {
return "", err
}

host := hosts.DiscoveredHost{
OSVersion: getOSVersion(),
HostIPAddresses: ipAddresses,
HostIPAddressesNetmasks: ipAddressesNetmasks,
HostName: d.host,
CPUCount: getLogicalCPUs(),
SocketCount: getCPUSocketCount(),
Expand All @@ -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 getHostIPAddresses() ([]string, []string, error) {
interfaces, err := net.Interfaces()
if err != nil {
return nil, err
return nil, nil, err
}

ipAddrList := make([]string, 0)
ipAddrNetmaskList := make([]string, 0)

for _, inter := range interfaces {
addrs, err := inter.Addrs()
Expand All @@ -93,10 +95,11 @@ func getHostIPAddresses() ([]string, error) {
for _, ipaddr := range addrs {
ipv4Addr, _, _ := net.ParseCIDR(ipaddr.String())
ipAddrList = append(ipAddrList, ipv4Addr.String())
ipAddrNetmaskList = append(ipAddrNetmaskList, ipaddr.String())
}
}

return ipAddrList, nil
return ipAddrList, ipAddrNetmaskList, 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"},
HostIPAddressesNetmasks: []string{"10.1.1.4/16", "10.1.1.5/24", "10.1.1.6/32"},
HostName: "thehostnamewherethediscoveryhappened",
CPUCount: 2,
SocketCount: 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
"10.1.1.5",
"10.1.1.6"
],
"ip_addresses_netmasks" :[
"10.1.1.4/16",
"10.1.1.5/24",
"10.1.1.6/32"
],
"hostname": "thehostnamewherethediscoveryhappened",
"cpu_count": 2,
"socket_count": 1,
Expand Down

0 comments on commit e239f6d

Please sign in to comment.