diff --git a/host/host_darwin.go b/host/host_darwin.go index 5cf22b7c..9f2b6b4e 100644 --- a/host/host_darwin.go +++ b/host/host_darwin.go @@ -10,7 +10,6 @@ import ( "os" "os/exec" "runtime" - "strconv" "strings" "sync/atomic" "time" @@ -68,9 +67,9 @@ func InfoWithContext(ctx context.Context) (*InfoStat, error) { ret.Procs = uint64(len(procs)) } - values, err := common.DoSysctrlWithContext(ctx, "kern.uuid") - if err == nil && len(values) == 1 && values[0] != "" { - ret.HostID = strings.ToLower(values[0]) + uuid, err := unix.Sysctl("kern.uuid") + if err == nil && uuid != "" { + ret.HostID = strings.ToLower(uuid) } return ret, nil @@ -84,24 +83,22 @@ func BootTime() (uint64, error) { } func BootTimeWithContext(ctx context.Context) (uint64, error) { + // https://github.com/AaronO/dashd/blob/222e32ef9f7a1f9bea4a8da2c3627c4cb992f860/probe/probe_darwin.go t := atomic.LoadUint64(&cachedBootTime) if t != 0 { return t, nil } - values, err := common.DoSysctrlWithContext(ctx, "kern.boottime") + value, err := unix.Sysctl("kern.boottime") if err != nil { return 0, err } - // ex: { sec = 1392261637, usec = 627534 } Thu Feb 13 12:20:37 2014 - v := strings.Replace(values[2], ",", "", 1) - boottime, err := strconv.ParseInt(v, 10, 64) - if err != nil { - return 0, err - } - t = uint64(boottime) - atomic.StoreUint64(&cachedBootTime, t) + bytes := []byte(value[:]) + var boottime uint64 + boottime = uint64(bytes[0]) + uint64(bytes[1])*256 + uint64(bytes[2])*256*256 + uint64(bytes[3])*256*256*256 - return t, nil + atomic.StoreUint64(&cachedBootTime, boottime) + + return boottime, nil } func uptime(boot uint64) uint64 { @@ -113,7 +110,7 @@ func Uptime() (uint64, error) { } func UptimeWithContext(ctx context.Context) (uint64, error) { - boot, err := BootTime() + boot, err := BootTimeWithContext(ctx) if err != nil { return 0, err }