Skip to content

Commit

Permalink
Implement node exporter target auto-detection
Browse files Browse the repository at this point in the history
  • Loading branch information
arbulu89 committed Nov 27, 2024
1 parent 424cb1d commit 8ab2c9f
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 19 deletions.
3 changes: 2 additions & 1 deletion internal/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type Config struct {
DiscoveriesConfig *discovery.DiscoveriesConfig
FactsServiceURL string
PluginsFolder string
PrometheusTargets discovery.PrometheusTargets
}

// NewAgent returns a new instance of Agent with the given configuration
Expand All @@ -48,7 +49,7 @@ func NewAgent(config *Config) (*Agent, error) {
discovery.NewSAPSystemsDiscovery(collectorClient, *config.DiscoveriesConfig),
discovery.NewCloudDiscovery(collectorClient, *config.DiscoveriesConfig),
discovery.NewSubscriptionDiscovery(collectorClient, config.InstanceName, *config.DiscoveriesConfig),
discovery.NewHostDiscovery(collectorClient, config.InstanceName, *config.DiscoveriesConfig),
discovery.NewHostDiscovery(collectorClient, config.InstanceName, config.PrometheusTargets, *config.DiscoveriesConfig),
discovery.NewSaptuneDiscovery(collectorClient, *config.DiscoveriesConfig),
}

Expand Down
21 changes: 11 additions & 10 deletions internal/core/hosts/discovered_host.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
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"`
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"`
PrometheusTargets map[string]string `json:"prometheus_targets"`
}
55 changes: 47 additions & 8 deletions internal/discovery/host.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package discovery

import (
"bytes"
"context"
"fmt"
"net"
"sort"
"strconv"
"time"

Expand All @@ -20,23 +22,31 @@ import (
const HostDiscoveryID string = "host_discovery"
const HostDiscoveryMinPeriod time.Duration = 1 * time.Second

const NodeExporterName string = "node_exporter"
const NodeExporterPort int = 9100

type PrometheusTargets map[string]string

type HostDiscovery struct {
id string
collectorClient collector.Client
host string
interval time.Duration
id string
collectorClient collector.Client
host string
prometheusTargets PrometheusTargets
interval time.Duration
}

func NewHostDiscovery(
collectorClient collector.Client,
hostname string,
prometheusTargets PrometheusTargets,
config DiscoveriesConfig,
) Discovery {
return HostDiscovery{
id: HostDiscoveryID,
collectorClient: collectorClient,
host: hostname,
interval: config.DiscoveriesPeriodsConfig.Host,
id: HostDiscoveryID,
collectorClient: collectorClient,
host: hostname,
prometheusTargets: prometheusTargets,
interval: config.DiscoveriesPeriodsConfig.Host,
}
}

Expand All @@ -55,6 +65,8 @@ func (d HostDiscovery) Discover(ctx context.Context) (string, error) {
return "", err
}

updatedPrometheusTargets := updatePrometheusTargets(d.prometheusTargets, ipAddresses)

host := hosts.DiscoveredHost{
OSVersion: getOSVersion(),
HostIPAddresses: ipAddresses,
Expand All @@ -66,6 +78,7 @@ func (d HostDiscovery) Discover(ctx context.Context) (string, error) {
AgentVersion: version.Version,
InstallationSource: version.InstallationSource,
FullyQualifiedDomainName: getHostFQDN(),
PrometheusTargets: updatedPrometheusTargets,
}

err = d.collectorClient.Publish(ctx, d.id, host)
Expand Down Expand Up @@ -107,6 +120,32 @@ func getNetworksData() ([]string, []int, error) {
return ipAddrList, netmasks, nil
}

func updatePrometheusTargets(targets PrometheusTargets, ipAddresses []string) PrometheusTargets {
// Get lowest IP address value to replace empty exporter targets
ips := make([]net.IP, 0, len(ipAddresses))
for _, ip := range ipAddresses {
parsedIp := net.ParseIP(ip)
if parsedIp.To4() != nil && !parsedIp.IsLoopback() {
ips = append(ips, parsedIp)
}

}

sort.Slice(ips, func(i, j int) bool {
return bytes.Compare(ips[i], ips[j]) < 0
})

// Replace exporter values if they are not given by the user
nodeExporterTarget, ok := targets[NodeExporterName]
if !ok || nodeExporterTarget == "" {
nodeExporterTarget = fmt.Sprintf("%s:%d", ips[0], NodeExporterPort)
}

return PrometheusTargets{
NodeExporterName: nodeExporterTarget,
}
}

func getHostFQDN() *string {

fqdn, err := fqdn.FqdnHostname()
Expand Down

0 comments on commit 8ab2c9f

Please sign in to comment.