2015-08-26 21:27:44 +00:00
|
|
|
package fingerprint
|
|
|
|
|
|
|
|
import (
|
2015-08-28 00:37:37 +00:00
|
|
|
"fmt"
|
2015-08-26 21:27:44 +00:00
|
|
|
"log"
|
2015-08-28 00:37:37 +00:00
|
|
|
"os/exec"
|
|
|
|
"runtime"
|
2015-10-15 23:40:07 +00:00
|
|
|
"strings"
|
2015-08-26 21:27:44 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/client/config"
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
"github.com/shirou/gopsutil/host"
|
|
|
|
)
|
|
|
|
|
|
|
|
// HostFingerprint is used to fingerprint the host
|
|
|
|
type HostFingerprint struct {
|
2015-11-05 21:46:02 +00:00
|
|
|
StaticFingerprinter
|
2015-08-26 21:27:44 +00:00
|
|
|
logger *log.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewHostFingerprint is used to create a Host fingerprint
|
|
|
|
func NewHostFingerprint(logger *log.Logger) Fingerprint {
|
2015-08-27 00:16:34 +00:00
|
|
|
f := &HostFingerprint{logger: logger}
|
2015-08-26 21:27:44 +00:00
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *HostFingerprint) Fingerprint(cfg *config.Config, node *structs.Node) (bool, error) {
|
|
|
|
hostInfo, err := host.HostInfo()
|
|
|
|
if err != nil {
|
|
|
|
f.logger.Println("[WARN] Error retrieving host information: ", err)
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
node.Attributes["os.name"] = hostInfo.Platform
|
|
|
|
node.Attributes["os.version"] = hostInfo.PlatformVersion
|
2015-08-28 00:37:37 +00:00
|
|
|
|
|
|
|
node.Attributes["kernel.name"] = runtime.GOOS
|
|
|
|
node.Attributes["kernel.version"] = ""
|
|
|
|
|
|
|
|
if runtime.GOOS != "windows" {
|
|
|
|
out, err := exec.Command("uname", "-r").Output()
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("Failed to run uname: %s", err)
|
|
|
|
}
|
2015-10-15 07:54:21 +00:00
|
|
|
node.Attributes["kernel.version"] = strings.Trim(string(out), "\n")
|
2015-08-28 00:37:37 +00:00
|
|
|
}
|
|
|
|
|
2015-08-26 21:27:44 +00:00
|
|
|
node.Attributes["hostname"] = hostInfo.Hostname
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|