Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add HostID to Host #237

Merged
merged 4 commits into from
Aug 11, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion host/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type InfoStat struct {
PlatformVersion string `json:"platformVersion"`
VirtualizationSystem string `json:"virtualizationSystem"`
VirtualizationRole string `json:"virtualizationRole"` // guest or host

HostID string `json:"hostid"` // ex: uuid
}

type UserStat struct {
Expand Down
5 changes: 5 additions & 0 deletions host/host_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ func Info() (*InfoStat, error) {
ret.Procs = uint64(len(procs))
}

values, err := common.DoSysctrl("kern.uuid")
if err == nil && len(values) == 1 && values[0] != "" {
ret.HostID = values[0]
}

return ret, nil
}

Expand Down
5 changes: 5 additions & 0 deletions host/host_freebsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ func Info() (*InfoStat, error) {
ret.Procs = uint64(len(procs))
}

values, err := common.DoSysctrl("kern.hostuuid")
if err == nil && len(values) == 1 && values[0] != "" {
ret.HostID = values[0]
}

return ret, nil
}

Expand Down
16 changes: 16 additions & 0 deletions host/host_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,22 @@ func Info() (*InfoStat, error) {
ret.Procs = numProcs
}

sysProductUUID := common.HostSys("class/dmi/id/product_uuid")
switch {
case common.PathExists(sysProductUUID):
lines, err := common.ReadLines(sysProductUUID)
if err == nil && len(lines) > 0 && lines[0] != "" {
ret.HostID = lines[0]
break
}
fallthrough
default:
values, err := common.DoSysctrl("kernel.random.boot_id")
if err == nil && len(values) == 1 && values[0] != "" {
ret.HostID = values[0]
}
}

return ret, nil
}

Expand Down
3 changes: 2 additions & 1 deletion host/host_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@ func TestHostInfoStat_String(t *testing.T) {
OS: "linux",
Platform: "ubuntu",
BootTime: 1447040000,
HostID: "edfd25ff-3c9c-b1a4-e660-bd826495ad35",
}
e := `{"hostname":"test","uptime":3000,"bootTime":1447040000,"procs":100,"os":"linux","platform":"ubuntu","platformFamily":"","platformVersion":"","virtualizationSystem":"","virtualizationRole":""}`
e := `{"hostname":"test","uptime":3000,"bootTime":1447040000,"procs":100,"os":"linux","platform":"ubuntu","platformFamily":"","platformVersion":"","virtualizationSystem":"","virtualizationRole":"","hostid":"edfd25ff-3c9c-b1a4-e660-bd826495ad35"}`
if e != fmt.Sprintf("%v", v) {
t.Errorf("HostInfoStat string is invalid: %v", v)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (

var (
Timeout = 3 * time.Second
TimeoutErr = errors.New("Command timed out.")
ErrTimeout = errors.New("Command timed out.")
)

type Invoker interface {
Expand Down Expand Up @@ -338,6 +338,6 @@ func WaitTimeout(c *exec.Cmd, timeout time.Duration) error {
}
// wait for the command to return after killing it
<-done
return TimeoutErr
return ErrTimeout
}
}
26 changes: 25 additions & 1 deletion internal/common/common_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,31 @@

package common

import "os"
import (
"os"
"os/exec"
"strings"
)

func DoSysctrl(mib string) ([]string, error) {
err := os.Setenv("LC_ALL", "C")
if err != nil {
return []string{}, err
}
sysctl, err := exec.LookPath("/sbin/sysctl")
if err != nil {
return []string{}, err
}
out, err := exec.Command(sysctl, "-n", mib).Output()
if err != nil {
return []string{}, err
}
v := strings.Replace(string(out), "{ ", "", 1)
v = strings.Replace(string(v), " }", "", 1)
values := strings.Fields(string(v))

return values, nil
}

func NumProcs() (uint64, error) {
f, err := os.Open(HostProc())
Expand Down