Skip to content

Commit

Permalink
Add HostID to the Host InfoStat struct returned from host.Info().
Browse files Browse the repository at this point in the history
On supported hosts the value returned is a UUID (case preserving
from the value of the underlying OS).

For Linux this is generated once, randomly per boot.  For FreeBSD and
Darwin this is a more durable value that should persist across reboots.
  • Loading branch information
sean- committed Aug 11, 2016
1 parent e4f857a commit 59094cd
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 2 deletions.
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
5 changes: 5 additions & 0 deletions host/host_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ func Info() (*InfoStat, error) {
ret.Procs = numProcs
}

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

0 comments on commit 59094cd

Please sign in to comment.