github.com/jmitchell/nomad@v0.1.3-0.20151007230021-7ab84c2862d8/client/fingerprint/host.go (about)

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