Skip to content
This repository has been archived by the owner on May 7, 2024. It is now read-only.

Commit

Permalink
Improve host FQDN detection (#6)
Browse files Browse the repository at this point in the history
Get fqdn on go since os.Hostname doesn't have it

Co-Authored-By: jespada <espada.jorge@gmail.com>
  • Loading branch information
2 people authored and falzm committed Apr 30, 2019
1 parent 97c4780 commit 41534a6
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 5 deletions.
39 changes: 39 additions & 0 deletions config/fqdn.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package config

import (
"net"
"os"
"strings"
"github.com/pkg/errors"
)

// Get Fully Qualified Domain Name
// returns the host FQDN or an error
// Original idea from: https://github.com/Showmax/go-fqdn/blob/master/fqdn.go
func GetFQDN() (string, error) {
hostname, err := os.Hostname()
if err != nil {
return "unknown", errors.Wrapf(err, "hostname unknown")
}

addrs, err := net.LookupIP(hostname)
if err != nil {
return hostname, nil
}

for _, addr := range addrs {
if ipv4 := addr.To4(); ipv4 != nil {
ip, err := ipv4.MarshalText()
if err != nil {
return hostname, nil
}
hosts, err := net.LookupAddr(string(ip))
if err != nil || len(hosts) == 0 {
return hostname, nil
}
fqdn := hosts[0]
return strings.TrimSuffix(fqdn, "."), nil // return fqdn without trailing dot
}
}
return hostname, nil
}
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ collectd.org v0.3.0 h1:iNBHGw1VvPJxH2B6RiFWFZ+vsjo1lCdRszBeOuwGi00=
collectd.org v0.3.0/go.mod h1:A/8DzQBkF6abtvrT2j/AU/4tiBgJWYyh0y/oB/4MlWE=
github.com/certifi/gocertifi v0.0.0-20190105021004-abcd57078448 h1:8tNk6SPXzLDnATTrWoI5Bgw9s/x4uf0kmBpk21NZgI4=
github.com/certifi/gocertifi v0.0.0-20190105021004-abcd57078448/go.mod h1:GJKEexRPVJrBSOjoqN5VNOIKJ5Q3RViH6eu3puDRwx4=
github.com/exoscale/go-metrics v0.0.0-20180729161012-6a0b1c6c28ec h1:A4NuISi8rZso9vY8WWHv9IbruBMMi9hRVajfAqRtxT0=
github.com/exoscale/go-metrics v0.0.0-20180729161012-6a0b1c6c28ec/go.mod h1:UzBqd2WjB5W7Uf7bOm1uOiYkXV7USjnD8Hy3nV2qZQM=
github.com/getsentry/raven-go v0.2.0 h1:no+xWJRb5ZI7eE8TWgIq1jLulQiIoLG0IfYxv5JYMGs=
github.com/getsentry/raven-go v0.2.0/go.mod h1:KungGk8q33+aIAZUIVWZDr2OfAEBsO49PX4NzFV5kcQ=
Expand Down
5 changes: 2 additions & 3 deletions metrics/collectd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package metrics

import (
"context"
"os"
"path"
"strings"
"time"
Expand All @@ -12,7 +11,7 @@ import (
"github.com/pkg/errors"
"github.com/rcrowley/go-metrics"

"github.com/exoscale/go-reporter/config"
"github.com/exoscale/go-reporter/config"
)

var separator = "."
Expand Down Expand Up @@ -74,7 +73,7 @@ func (c *CollectdConfiguration) initExporter(m *Metrics) error {
func collectdReportOnce(r metrics.Registry, client *network.Client, interval time.Duration,
prefix string, excluded []string) {
ctx := context.Background()
hostname, err := os.Hostname()
hostname, err := config.GetFQDN()
if err != nil {
return
}
Expand Down
3 changes: 1 addition & 2 deletions metrics/collectd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"math/rand"
"net"
"os"
"testing"
"time"

Expand Down Expand Up @@ -213,7 +212,7 @@ L:
continue
}

hostname, _ := os.Hostname()
hostname, _ := config.GetFQDN()
if v.Host != hostname {
t.Errorf("Received metric %+v has hostname == %v, expected %v",
v, v.Host, hostname)
Expand Down

0 comments on commit 41534a6

Please sign in to comment.