159 lines
3.3 KiB
Go
159 lines
3.3 KiB
Go
package sysstats
|
|
|
|
import (
|
|
"bufio"
|
|
"context"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// cpuUsage samples the aggregate "cpu" line of /proc/stat twice and returns
|
|
// the busy share of the elapsed ticks.
|
|
func cpuUsage(ctx context.Context, window time.Duration) (*float64, error) {
|
|
idle1, total1, err := readCPUTimes()
|
|
if err != nil {
|
|
return nil, nil
|
|
}
|
|
|
|
select {
|
|
case <-ctx.Done():
|
|
return nil, ctx.Err()
|
|
case <-time.After(window):
|
|
}
|
|
|
|
idle2, total2, err := readCPUTimes()
|
|
if err != nil || total2 <= total1 {
|
|
return nil, nil
|
|
}
|
|
busy := (total2 - total1) - (idle2 - idle1)
|
|
v := percent(busy, total2-total1)
|
|
return &v, nil
|
|
}
|
|
|
|
func readCPUTimes() (idle, total uint64, err error) {
|
|
data, err := os.ReadFile("/proc/stat")
|
|
if err != nil {
|
|
return 0, 0, err
|
|
}
|
|
line, _, _ := strings.Cut(string(data), "\n")
|
|
fields := strings.Fields(line)
|
|
if len(fields) < 5 || fields[0] != "cpu" {
|
|
return 0, 0, os.ErrInvalid
|
|
}
|
|
// user nice system idle iowait irq softirq steal [guest guest_nice]
|
|
// guest time is already counted in user/nice, so stop at steal.
|
|
for i, f := range fields[1:] {
|
|
if i >= 8 {
|
|
break
|
|
}
|
|
v, err := strconv.ParseUint(f, 10, 64)
|
|
if err != nil {
|
|
return 0, 0, err
|
|
}
|
|
total += v
|
|
if i == 3 || i == 4 { // idle, iowait
|
|
idle += v
|
|
}
|
|
}
|
|
return idle, total, nil
|
|
}
|
|
|
|
func memory() *Memory {
|
|
f, err := os.Open("/proc/meminfo")
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
defer f.Close()
|
|
|
|
info := make(map[string]uint64)
|
|
sc := bufio.NewScanner(f)
|
|
for sc.Scan() {
|
|
key, rest, ok := strings.Cut(sc.Text(), ":")
|
|
if !ok {
|
|
continue
|
|
}
|
|
fields := strings.Fields(rest)
|
|
if len(fields) == 0 {
|
|
continue
|
|
}
|
|
v, err := strconv.ParseUint(fields[0], 10, 64)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
info[key] = v * 1024 // values are in kB
|
|
}
|
|
|
|
total := info["MemTotal"]
|
|
available, ok := info["MemAvailable"]
|
|
if !ok {
|
|
available = info["MemFree"] + info["Buffers"] + info["Cached"]
|
|
}
|
|
used := total - min(available, total)
|
|
|
|
return &Memory{
|
|
TotalBytes: total,
|
|
UsedBytes: used,
|
|
AvailableBytes: available,
|
|
UsagePercent: percent(used, total),
|
|
SwapTotalBytes: info["SwapTotal"],
|
|
SwapUsedBytes: info["SwapTotal"] - min(info["SwapFree"], info["SwapTotal"]),
|
|
LimitBytes: cgroupMemoryLimit(),
|
|
}
|
|
}
|
|
|
|
// cgroupMemoryLimit reads the container memory limit (cgroup v2, then v1).
|
|
// Very large v1 values mean "no limit".
|
|
func cgroupMemoryLimit() *uint64 {
|
|
for _, path := range []string{
|
|
"/sys/fs/cgroup/memory.max",
|
|
"/sys/fs/cgroup/memory/memory.limit_in_bytes",
|
|
} {
|
|
data, err := os.ReadFile(path)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
v, err := strconv.ParseUint(strings.TrimSpace(string(data)), 10, 64)
|
|
if err != nil || v >= 1<<60 {
|
|
return nil // "max" or effectively unlimited
|
|
}
|
|
return &v
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func loadAvg() *[3]float64 {
|
|
data, err := os.ReadFile("/proc/loadavg")
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
fields := strings.Fields(string(data))
|
|
if len(fields) < 3 {
|
|
return nil
|
|
}
|
|
var out [3]float64
|
|
for i := range out {
|
|
v, err := strconv.ParseFloat(fields[i], 64)
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
out[i] = v
|
|
}
|
|
return &out
|
|
}
|
|
|
|
func hostUptime() *int64 {
|
|
data, err := os.ReadFile("/proc/uptime")
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
first, _, _ := strings.Cut(string(data), " ")
|
|
v, err := strconv.ParseFloat(first, 64)
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
s := int64(v)
|
|
return &s
|
|
}
|