From 59094cd5b7d13a0165f0a935bb4a83d4ff99d206 Mon Sep 17 00:00:00 2001 From: Sean Chittenden Date: Thu, 11 Aug 2016 00:51:07 -0700 Subject: [PATCH] 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. --- host/host.go | 2 +- host/host_darwin.go | 5 +++++ host/host_freebsd.go | 5 +++++ host/host_linux.go | 5 +++++ host/host_test.go | 3 ++- 5 files changed, 18 insertions(+), 2 deletions(-) diff --git a/host/host.go b/host/host.go index 1a6545b..548e92e 100644 --- a/host/host.go +++ b/host/host.go @@ -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 { diff --git a/host/host_darwin.go b/host/host_darwin.go index 199ea61..43c49b2 100644 --- a/host/host_darwin.go +++ b/host/host_darwin.go @@ -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 } diff --git a/host/host_freebsd.go b/host/host_freebsd.go index 2159ae8..58a06c2 100644 --- a/host/host_freebsd.go +++ b/host/host_freebsd.go @@ -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 } diff --git a/host/host_linux.go b/host/host_linux.go index 3d73242..a9f3749 100644 --- a/host/host_linux.go +++ b/host/host_linux.go @@ -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 } diff --git a/host/host_test.go b/host/host_test.go index d73d187..18832ab 100644 --- a/host/host_test.go +++ b/host/host_test.go @@ -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) }