github.com/kardianos/nomad@v0.1.3-0.20151022182107-b13df73ee850/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  	logger *log.Logger
    18  }
    19  
    20  // NewHostFingerprint is used to create a Host fingerprint
    21  func NewHostFingerprint(logger *log.Logger) Fingerprint {
    22  	f := &HostFingerprint{logger: logger}
    23  	return f
    24  }
    25  
    26  func (f *HostFingerprint) Fingerprint(cfg *config.Config, node *structs.Node) (bool, error) {
    27  	hostInfo, err := host.HostInfo()
    28  	if err != nil {
    29  		f.logger.Println("[WARN] Error retrieving host information: ", err)
    30  		return false, err
    31  	}
    32  
    33  	node.Attributes["os.name"] = hostInfo.Platform
    34  	node.Attributes["os.version"] = hostInfo.PlatformVersion
    35  
    36  	node.Attributes["kernel.name"] = runtime.GOOS
    37  	node.Attributes["kernel.version"] = ""
    38  
    39  	if runtime.GOOS != "windows" {
    40  		out, err := exec.Command("uname", "-r").Output()
    41  		if err != nil {
    42  			return false, fmt.Errorf("Failed to run uname: %s", err)
    43  		}
    44  		node.Attributes["kernel.version"] = strings.Trim(string(out), "\n")
    45  	}
    46  
    47  	node.Attributes["hostname"] = hostInfo.Hostname
    48  
    49  	return true, nil
    50  }