Skip to content

Commit

Permalink
allow NODENAME env var to override hostname for pauditd reporting (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
CSdread authored Apr 23, 2019
1 parent 1d86ccf commit b32f85c
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 6 deletions.
4 changes: 2 additions & 2 deletions pkg/metric/statsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package metric

import (
"fmt"
"os"
"github.com/pantheon-systems/pauditd/pkg/system"
"strconv"
"strings"

Expand Down Expand Up @@ -51,7 +51,7 @@ func Configure(config *viper.Viper) error {
statsSampleRate = float32(sampleRate)
}

hostname, _ := os.Hostname()
hostname := system.GetHostname()
simpleHostName := strings.Split(hostname, ".")[0]
statsPrefix := fmt.Sprintf("pauditd.%s", simpleHostName)
client, err = statsd.New(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package httptransformer
import (
"encoding/json"
"fmt"
"github.com/pantheon-systems/pauditd/pkg/system"
"os"
"regexp"
"strings"
Expand Down Expand Up @@ -89,10 +90,7 @@ func (t NotificationServiceTransformer) Transform(traceID uuid.UUID, body []byte
}

func getHostname() string {
host, err := os.Hostname()
if err != nil {
return ""
}
host := system.GetHostname()

// we want to normallize the hostname,
// only the first part
Expand Down
25 changes: 25 additions & 0 deletions pkg/system/host_data_provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package system

import (
"os"
)

const (
hostnameEnv string = "NODENAME"
)

// GetHostname retrieves the hostname of the system, if no hostname
// is available we return empty string
func GetHostname() string {
var err error

host, ok := os.LookupEnv(hostnameEnv)
if !ok {
host, err = os.Hostname()
if err != nil {
return ""
}
}

return host
}
19 changes: 19 additions & 0 deletions pkg/system/host_data_provider_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package system_test

import (
"github.com/pantheon-systems/pauditd/pkg/system"
"os"
"testing"

"github.com/stretchr/testify/assert"
)

func Test_GetHostnameWithEnvVar(t *testing.T) {
testName := "test-node-name"

os.Setenv("NODENAME", testName)

hostname := system.GetHostname()

assert.Equal(t, testName, hostname)
}

0 comments on commit b32f85c

Please sign in to comment.