hoststats

package
v1.10.26 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 10, 2026 License: AGPL-3.0, Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package hoststats reads host CPU and memory from procfs, shared by control plane and agent so figures can't drift.

Index

Constants

This section is empty.

Variables

View Source
var ErrNoBaseline = errors.New("hoststats: no cpu baseline yet")

ErrNoBaseline is returned by Sampler.Sample until it has a previous CPU reading to measure against.

Functions

func Available

func Available(procPath string) bool

func CPUPercent

func CPUPercent(a, b CPUTimes) float64

A counter that went backwards reads as 0.

func MemFromVals

func MemFromVals(vals map[string]uint64) (total, avail uint64, err error)

func ParseMemLine

func ParseMemLine(line string) (key string, kb uint64, ok bool)

ParseMemLine splits one meminfo row into its key and kB value.

func ReadLoad1

func ReadLoad1(procPath string) (float64, error)

ReadLoad1 reads the one-minute load average from procPath/loadavg.

func ReadMem

func ReadMem(procPath string) (total, avail uint64, err error)

Falls back to MemFree+Buffers+Cached for kernels without MemAvailable.

func ReadUptime

func ReadUptime(procPath string) (uint64, error)

ReadUptime reads the host's uptime in whole seconds from procPath/uptime.

Types

type CPUTimes

type CPUTimes struct {
	Total uint64
	Idle  uint64 // idle + iowait
}

CPUTimes is the aggregate "cpu" row of /proc/stat, in jiffies.

func ParseCPULine

func ParseCPULine(line string) (CPUTimes, error)

Shared with the remote sampler, which gets the line from a helper container's stdout.

func ReadCPU

func ReadCPU(procPath string) (CPUTimes, error)

ReadCPU reads the aggregate CPU counters from procPath/stat.

type Report

type Report struct {
	CPUPercent    float64 `json:"cpu_percent"`
	MemTotalBytes uint64  `json:"mem_total_bytes"`
	MemUsedBytes  uint64  `json:"mem_used_bytes"`
	Load1         float64 `json:"load1"`
	UptimeSeconds uint64  `json:"uptime_s"`
}

Report carries no identity or timestamp: the control plane takes the node from the token and stamps receipt.

func (Report) Stats

func (r Report) Stats() Stats

Stats converts the report into a snapshot.

type Sampler

type Sampler struct {
	ProcPath string
	// contains filtered or unexported fields
}

Sampler averages CPU over the whole interval since the last call, so bursty workloads are measured honestly.

func (*Sampler) Sample

func (s *Sampler) Sample() (Report, error)

The first call, or one after counters went backwards, only records a baseline and returns ErrNoBaseline.

type Stats

type Stats struct {
	CPUPercent    float64 `json:"cpu_percent"`
	MemTotalBytes uint64  `json:"mem_total_bytes"`
	MemUsedBytes  uint64  `json:"mem_used_bytes"`
	MemPercent    float64 `json:"mem_percent"`
}

Stats is a point-in-time host resource snapshot.

func Read

func Read(ctx context.Context, procPath string) (Stats, error)

func StatsFrom

func StatsFrom(c1, c2 CPUTimes, memTotal, memAvail uint64) Stats

StatsFrom assembles a snapshot from two CPU samples and a memory reading.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL