提交 444ee723 编写于 作者: C Cameron Sparr

Fix HostInfo.Uptime, which has been returning boot time

Also adding a HostInfo.BootTime field for holding the boot time
上级 dcbb29ae
......@@ -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
......
......@@ -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
......
......@@ -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) {
......
......@@ -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"
......
......@@ -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()
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册