You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
themis/uptime_linux.go

33 lines
726 B

//go:build linux
package themis
import (
"bytes"
"context"
"fmt"
"os"
"strconv"
"time"
"github.com/rs/zerolog/log"
)
// Uptime returns the time elapsed since the start of the current process ID.
func Uptime(ctx context.Context) (time.Duration, error) {
raw, err := os.ReadFile("/proc/uptime")
if err != nil {
return 0, fmt.Errorf("failed to read uptime from OS: %w", err)
}
log.Debug().Ctx(ctx).Str("raw", string(raw)).Msg("reading /proc/uptime to get server up time")
i := bytes.IndexRune(raw, ' ')
up, err := strconv.ParseFloat(string(raw[:i]), 64)
if err != nil {
return 0, fmt.Errorf("failed to parse uptime from OS: %w", err)
}
return time.Duration(int(up*1000) * int(time.Millisecond)), nil
}