diff --git a/host/host.go b/host/host.go index 523b634..241ceb2 100644 --- a/host/host.go +++ b/host/host.go @@ -9,6 +9,7 @@ import ( type HostInfoStat struct { Hostname string `json:"hostname"` Uptime uint64 `json:"uptime"` + BootTime uint64 `json:"boot_time"` Procs uint64 `json:"procs"` // number of processes OS string `json:"os"` // ex: freebsd, linux Platform string `json:"platform"` // ex: ubuntu, linuxmint diff --git a/host/host_darwin.go b/host/host_darwin.go index 2a3a02d..069c6fa 100644 --- a/host/host_darwin.go +++ b/host/host_darwin.go @@ -11,6 +11,7 @@ import ( "runtime" "strconv" "strings" + "time" "unsafe" "github.com/shirou/gopsutil/internal/common" @@ -39,14 +40,10 @@ func HostInfo() (*HostInfoStat, error) { ret.VirtualizationRole = role } - values, err := common.DoSysctrl("kern.boottime") + boot, err := BootTime() if err == nil { - // ex: { sec = 1392261637, usec = 627534 } Thu Feb 13 12:20:37 2014 - v := strings.Replace(values[2], ",", "", 1) - t, err := strconv.ParseUint(v, 10, 64) - if err != nil { - ret.Uptime = t - } + ret.BootTime = boot + ret.Uptime = uptime(boot) } return ret, nil @@ -59,7 +56,6 @@ func BootTime() (uint64, error) { } // 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 @@ -68,6 +64,18 @@ func BootTime() (uint64, error) { return uint64(boottime), nil } +func uptime(boot uint64) uint64 { + return uint64(time.Now().Unix()) - boot +} + +func Uptime() (uint64, error) { + boot, err := BootTime() + if err != nil { + return 0, err + } + return uptime(boot), nil +} + func Users() ([]UserStat, error) { utmpfile := "/var/run/utmpx" var ret []UserStat diff --git a/host/host_freebsd.go b/host/host_freebsd.go index 95a7154..69e2197 100644 --- a/host/host_freebsd.go +++ b/host/host_freebsd.go @@ -11,6 +11,7 @@ import ( "runtime" "strconv" "strings" + "time" "unsafe" "github.com/shirou/gopsutil/internal/common" @@ -45,14 +46,10 @@ func HostInfo() (*HostInfoStat, error) { ret.VirtualizationRole = role } - values, err := common.DoSysctrl("kern.boottime") + boot, err := BootTime() if err == nil { - // ex: { sec = 1392261637, usec = 627534 } Thu Feb 13 12:20:37 2014 - v := strings.Replace(values[2], ",", "", 1) - t, err := strconv.ParseUint(v, 10, 64) - if err == nil { - ret.Uptime = t - } + ret.BootTime = boot + ret.Uptime = uptime(boot) } return ret, nil @@ -74,6 +71,18 @@ func BootTime() (int64, error) { return boottime, nil } +func uptime(boot uint64) uint64 { + return uint64(time.Now().Unix()) - boot +} + +func Uptime() (uint64, error) { + boot, err := BootTime() + if err != nil { + return 0, err + } + return uptime(boot), nil +} + func Users() ([]UserStat, error) { utmpfile := "/var/run/utx.active" if !common.PathExists(utmpfile) { diff --git a/host/host_linux.go b/host/host_linux.go index 0ac9ea8..91b1530 100644 --- a/host/host_linux.go +++ b/host/host_linux.go @@ -13,6 +13,7 @@ import ( "runtime" "strconv" "strings" + "time" "unsafe" "github.com/shirou/gopsutil/internal/common" @@ -46,9 +47,10 @@ func HostInfo() (*HostInfoStat, error) { ret.VirtualizationSystem = system ret.VirtualizationRole = role } - uptime, err := BootTime() + boot, err := BootTime() if err == nil { - ret.Uptime = uptime + ret.BootTime = boot + ret.Uptime = uptime(boot) } return ret, nil @@ -78,6 +80,18 @@ func BootTime() (uint64, error) { return 0, fmt.Errorf("could not find btime") } +func uptime(boot uint64) uint64 { + return uint64(time.Now().Unix()) - boot +} + +func Uptime() (uint64, error) { + boot, err := BootTime() + if err != nil { + return 0, err + } + return uptime(boot), nil +} + func Users() ([]UserStat, error) { utmpfile := "/var/run/utmp" diff --git a/host/host_windows.go b/host/host_windows.go index 51f2d60..13890bf 100644 --- a/host/host_windows.go +++ b/host/host_windows.go @@ -47,9 +47,10 @@ func HostInfo() (*HostInfoStat, error) { return ret, err } - ret.Uptime, err = BootTime() - if err != nil { - return ret, err + boot, err := BootTime() + if err == nil { + ret.BootTime = boot + ret.Uptime = uptime(boot) } procs, err := process.Pids() @@ -87,6 +88,18 @@ func BootTime() (uint64, error) { return uint64(now.Sub(t).Seconds()), nil } +func uptime(boot uint64) uint64 { + return uint64(time.Now().Unix()) - boot +} + +func Uptime() (uint64, error) { + boot, err := BootTime() + if err != nil { + return 0, err + } + return uptime(boot), nil +} + func GetPlatformInformation() (platform string, family string, version string, err error) { if osInfo == nil { _, err = GetOSInfo()