diff options
Diffstat (limited to 'vendor/github.com/shirou/gopsutil/host/host_linux.go')
-rw-r--r-- | vendor/github.com/shirou/gopsutil/host/host_linux.go | 43 |
1 files changed, 31 insertions, 12 deletions
diff --git a/vendor/github.com/shirou/gopsutil/host/host_linux.go b/vendor/github.com/shirou/gopsutil/host/host_linux.go index 5324258..0e09f15 100644 --- a/vendor/github.com/shirou/gopsutil/host/host_linux.go +++ b/vendor/github.com/shirou/gopsutil/host/host_linux.go @@ -109,10 +109,14 @@ func BootTimeWithContext(ctx context.Context) (uint64, error) { if err != nil { return 0, err } + statFile := "stat" if system == "lxc" && role == "guest" { // if lxc, /proc/uptime is used. statFile = "uptime" + } else if system == "docker" && role == "guest" { + // also docker, guest + statFile = "uptime" } filename := common.HostProc(statFile) @@ -120,20 +124,35 @@ func BootTimeWithContext(ctx context.Context) (uint64, error) { if err != nil { return 0, err } - 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 + + if statFile == "stat" { + 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 + } + t = uint64(b) + atomic.StoreUint64(&cachedBootTime, t) + return t, nil } - t = uint64(b) - atomic.StoreUint64(&cachedBootTime, t) - return t, nil } + } else if statFile == "uptime" { + if len(lines) != 1 { + return 0, fmt.Errorf("wrong uptime format") + } + f := strings.Fields(lines[0]) + b, err := strconv.ParseFloat(f[0], 64) + if err != nil { + return 0, err + } + t = uint64(time.Now().Unix()) - uint64(b) + atomic.StoreUint64(&cachedBootTime, t) + return t, nil } return 0, fmt.Errorf("could not find btime") |