Documentation
¶
Overview ¶
Package hoststats reads real host CPU and memory usage from a procfs directory. /proc/stat and /proc/meminfo already reflect the host even from inside a container, so the process's own /proc works out of the box; binding the host's procfs to /host/proc is an explicit alternative.
It is shared by the control plane and the node agent, so the figures both sides produce and the payload the agent pushes cannot drift apart.
Index ¶
- Variables
- func Available(procPath string) bool
- func CPUPercent(a, b CPUTimes) float64
- func MemFromVals(vals map[string]uint64) (total, avail uint64, err error)
- func ParseMemLine(line string) (key string, kb uint64, ok bool)
- func ReadLoad1(procPath string) (float64, error)
- func ReadMem(procPath string) (total, avail uint64, err error)
- func ReadUptime(procPath string) (uint64, error)
- type CPUTimes
- type Report
- type Sampler
- type Stats
Constants ¶
This section is empty.
Variables ¶
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 ¶
Available reports whether procPath exposes a readable /proc/stat, i.e. host stats can be read from it.
func CPUPercent ¶
CPUPercent is the busy share of the interval between two samples, clamped to [0, 100]. A counter that went backwards reads as 0.
func MemFromVals ¶
MemFromVals resolves total and available memory from parsed meminfo keys (bytes). MemAvailable is preferred; the MemFree+Buffers+Cached sum is the fallback for kernels without it.
func ParseMemLine ¶
ParseMemLine splits one meminfo row into its key and kB value.
func ReadMem ¶
ReadMem parses MemTotal and MemAvailable (bytes) from meminfo. Falls back to MemFree+Buffers+Cached for kernels without MemAvailable.
func ReadUptime ¶
ReadUptime reads the host's uptime in whole seconds from procPath/uptime.
Types ¶
type CPUTimes ¶
CPUTimes is the aggregate "cpu" row of /proc/stat, in jiffies.
func ParseCPULine ¶
ParseCPULine reads the aggregate "cpu" row of /proc/stat. Shared with the remote sampler, which gets the same line from a helper container's stdout rather than from a file.
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 is what a node agent pushes to POST /api/v1/agent/stats. It carries no identity and no timestamp: the control plane takes the node from the bearer token and stamps receipt time itself.
type Sampler ¶
type Sampler struct {
ProcPath string
// contains filtered or unexported fields
}
Sampler measures CPU over the whole interval between successive calls rather than a short fixed window, so a bursty workload is averaged over what actually happened since the last report.
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.