diff --git a/host/host_linux.go b/host/host_linux.go index b0ed30a..24d829f 100644 --- a/host/host_linux.go +++ b/host/host_linux.go @@ -5,13 +5,14 @@ package host import ( "bytes" "encoding/binary" + "fmt" "io/ioutil" "os" "os/exec" "regexp" "runtime" + "strconv" "strings" - "syscall" "unsafe" common "github.com/shirou/gopsutil/common" @@ -54,12 +55,27 @@ func HostInfo() (*HostInfoStat, error) { return ret, nil } +// BootTime returns the system boot time expressed in seconds since the epoch. func BootTime() (uint64, error) { - sysinfo := &syscall.Sysinfo_t{} - if err := syscall.Sysinfo(sysinfo); err != nil { + lines, err := common.ReadLines("/proc/stat") + if err != nil { return 0, err } - return uint64(sysinfo.Uptime), nil + for _, line := range lines { + if strings.HasPrefix(line, "btime") { + f := strings.Fields(line) + if len(f) != 2 { + return 0, fmt.Errorf("wrong btime format") + } + b, err := strconv.ParseInt(f[1], 10, 64) + if err != nil { + return 0, err + } + return uint64(b), nil + } + } + + return 0, fmt.Errorf("could not find btime") } func Users() ([]UserStat, error) { diff --git a/host/host_test.go b/host/host_test.go index 4c6fbc9..eb69829 100644 --- a/host/host_test.go +++ b/host/host_test.go @@ -22,7 +22,7 @@ func TestBoot_time(t *testing.T) { t.Errorf("error %v", err) } if v == 0 { - t.Errorf("Could not boot time %v", v) + t.Errorf("Could not get boot time %v", v) } }