Skip to content

Commit

Permalink
Ability to override the hostname (useful for containers)
Browse files Browse the repository at this point in the history
  • Loading branch information
melchor629 committed Oct 8, 2019
1 parent 7abd22d commit 631b9cb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 19 deletions.
19 changes: 13 additions & 6 deletions infping.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,11 @@ func main() {
createDatabaseIfNotExist(influxClient)

hosts := viper.GetStringSlice("hosts")
hostname := viper.GetString("hostname")
fpingConfig := prepareFpingConfiguration()

log.Printf("Launching fping with hosts: %s", strings.Join(hosts, ", "))
err := runAndRead(hosts, influxClient, fpingConfig)
err := runAndRead(hosts, influxClient, fpingConfig, hostname)

if err != nil {
log.Fatal("Failed when obtaining and storing pings", err)
Expand All @@ -72,11 +73,7 @@ func parsePrefixTemplate(tplString string) (string, error) {
}

params := prefixTemplateParams{}
params.Hostname, err = os.Hostname()
params.Hostname = strings.ToLower(params.Hostname)
if err != nil {
return "", err
}
params.Hostname = strings.ToLower(viper.GetString("hostname"))

splitHostname := strings.Split(params.Hostname, ".")
reverseAny(splitHostname)
Expand Down Expand Up @@ -115,6 +112,7 @@ func setDefaults() {
viper.SetDefault("fping.period", "1000")
viper.SetDefault("fping.custom", map[string]string{})
viper.SetDefault("hosts", []string{"localhost"})
viper.SetDefault("hostname", mustHostname())
}

func readConfiguration() {
Expand Down Expand Up @@ -230,3 +228,12 @@ func prepareFpingConfiguration() map[string]string {

return fpingConfig
}

// mustHostname returns the local hostname or throws an error
func mustHostname() string {
name, err := os.Hostname()
if err != nil {
panic("unable to find hostname " + err.Error())
}
return strings.ToLower(name)
}
16 changes: 3 additions & 13 deletions parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,12 @@ package main
import (
"bufio"
"log"
"os"
"os/exec"
"strconv"
"strings"
"time"
)

var hostname = mustHostname()
var lastTime = time.Now()

// Point represents the fping results for a single host
Expand All @@ -49,7 +47,7 @@ type Point struct {
}

// runAndRead executes fping, parses the output into a Point, and then writes it to Influx
func runAndRead(hosts []string, con Client, fpingConfig map[string]string) error {
func runAndRead(hosts []string, con Client, fpingConfig map[string]string, hostname string) error {
runner, err := createRunner(hosts, fpingConfig)
if err != nil {
return err
Expand All @@ -73,7 +71,7 @@ func runAndRead(hosts []string, con Client, fpingConfig map[string]string) error
if len(fields) == 1 {
handleInvalidOutput(fields)
} else {
handleValidOutput(fields, con)
handleValidOutput(fields, con, hostname)
}
}

Expand Down Expand Up @@ -107,7 +105,7 @@ func handleInvalidOutput(fields []string) {
}
}

func handleValidOutput(fields []string, con Client) {
func handleValidOutput(fields []string, con Client, hostname string) {
host := fields[0]
data := fields[4]
dataSplit := strings.Split(data, "/")
Expand Down Expand Up @@ -156,11 +154,3 @@ func mustFloat(data string) float64 {
return flt
}

// mustHostname returns the local hostname or throws an error
func mustHostname() string {
name, err := os.Hostname()
if err != nil {
panic("unable to find hostname " + err.Error())
}
return strings.ToLower(name)
}

0 comments on commit 631b9cb

Please sign in to comment.