github.com/maier/nomad@v0.4.1-0.20161110003312-a9e3d0b8549d/client/fingerprint/host.go (about)

     1  package fingerprint
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"os/exec"
     7  	"runtime"
     8  	"strings"
     9  
    10  	"github.com/hashicorp/nomad/client/config"
    11  	"github.com/hashicorp/nomad/nomad/structs"
    12  	"github.com/shirou/gopsutil/host"
    13  )
    14  
    15  // HostFingerprint is used to fingerprint the host
    16  type HostFingerprint struct {
    17  	StaticFingerprinter
    18  	logger *log.Logger
    19  }
    20  
    21  // NewHostFingerprint is used to create a Host fingerprint
    22  func NewHostFingerprint(logger *log.Logger) Fingerprint {
    23  	f := &HostFingerprint{logger: logger}
    24  	return f
    25  }
    26  
    27  func (f *HostFingerprint) Fingerprint(cfg *config.Config, node *structs.Node) (bool, error) {
    28  	hostInfo, err := host.Info()
    29  	if err != nil {
    30  		f.logger.Println("[WARN] Error retrieving host information: ", err)
    31  		return false, err
    32  	}
    33  
    34  	node.Attributes["os.name"] = hostInfo.Platform
    35  	node.Attributes["os.version"] = hostInfo.PlatformVersion
    36  
    37  	node.Attributes["kernel.name"] = runtime.GOOS
    38  	node.Attributes["kernel.version"] = ""
    39  
    40  	if runtime.GOOS != "windows" {
    41  		out, err := exec.Command("uname", "-r").Output()
    42  		if err != nil {
    43  			return false, fmt.Errorf("Failed to run uname: %s", err)
    44  		}
    45  		node.Attributes["kernel.version"] = strings.Trim(string(out), "\n")
    46  	}
    47  
    48  	node.Attributes["unique.hostname"] = hostInfo.Hostname
    49  
    50  	return true, nil
    51  }