Add HostID to the Host InfoStat struct returned from host.Info().

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.
pull/237/head
Sean Chittenden 9 years ago
parent e4f857a9ca
commit 59094cd5b7
No known key found for this signature in database
GPG Key ID: 4EBC9DC16C2E5E16

@ -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 {

@ -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
}

@ -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
}

@ -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
}

@ -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)
}

Loading…
Cancel
Save