sysstat

package
v0.1.90-dev.1 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2026 License: AGPL-3.0 Imports: 8 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package sysstat reads what a Linux host and the current process are using: CPU (with the parts lost to softirq, steal and I/O wait), memory and swap, connection tracking, sockets, the NIC's link speed, and the process's own CPU, memory, threads and file descriptors. The node agent reports it with every stats batch; the panel shows its own.

The parsers here are pure and build everywhere, so they are tested on any OS. The readers (read.go) take a /proc and /sys root so tests can point them at a fixture; on a system without /proc they return zeros, which callers treat as "not known".

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Clamp100

func Clamp100(f float64) float64

Clamp100 keeps a percentage in 0..100.

func DiskUsage

func DiskUsage(path string) (total, used uint64)

DiskUsage is the size and the used bytes of the file system holding path (0, 0 when it cannot be read).

func ParseOpenFilesLimit

func ParseOpenFilesLimit(s string) uint64

ParseOpenFilesLimit reads the soft "Max open files" from /proc/<pid>/limits (0 = unknown or unlimited).

func ParseStatusRSS

func ParseStatusRSS(s string) uint64

ParseStatusRSS reads VmRSS from /proc/<pid>/status, in bytes.

func ParseUint

func ParseUint(s string) (uint64, bool)

ParseUint reads a file holding one number, such as nf_conntrack_count. ok = false for an empty file or a negative number (a NIC's speed is -1 when the driver does not know it).

Types

type CPUShare

type CPUShare struct {
	// Busy is everything but idle and I/O wait: user, system, irq, softirq and steal.
	Busy float64
	// Softirq is the part of Busy spent in softirq (packet processing, a kernel WireGuard/AmneziaWG).
	Softirq float64
	// Steal is the part of Busy the hypervisor gave to other guests: a busy neighbour on the same host.
	Steal float64
	// IOWait is time idle while waiting for disk; it is not in Busy.
	IOWait float64
}

CPUShare is how the whole machine's CPU time was spent between two samples, in percent of all CPUs.

func CPUDelta

func CPUDelta(prev, cur CPUTimes) CPUShare

CPUDelta turns two samples into shares. Counters that went backwards (a reset) give zeros.

type CPUTimes

type CPUTimes struct {
	Total, Idle, IOWait, Softirq, Steal uint64
}

CPUTimes is the aggregate "cpu" line of /proc/stat, in USER_HZ ticks summed over all CPUs: user nice system idle iowait irq softirq steal [guest guest_nice]. guest time is already inside user, so it is not added to Total.

func ParseCPU

func ParseCPU(stat string) (CPUTimes, bool)

ParseCPU reads the aggregate "cpu" line of /proc/stat.

type GoRuntime

type GoRuntime struct {
	Goroutines uint64
	// HeapBytes is memory in live and not yet swept heap objects; TotalBytes is all memory the runtime mapped from the OS
	// (heap, stacks, its own metadata), which is close to what the process costs beyond its binary.
	HeapBytes, TotalBytes uint64
}

GoRuntime is what the Go runtime of this process holds.

func ReadGoRuntime

func ReadGoRuntime() GoRuntime

ReadGoRuntime reads the runtime's own counters without stopping the world.

type Host

type Host struct {
	CPU   CPUShare
	Load1 float64
	Mem   Mem
	// Connection tracking: entries and limit (0, 0 = not loaded).
	ConntrackCount, ConntrackMax uint64
	Sockets                      Sockets
}

Host is one sample of the machine.

type HostSampler

type HostSampler struct {
	R Reader
	// contains filtered or unexported fields
}

HostSampler takes Host samples; CPU shares are over the time since the previous Sample (zero on the first). Safe for concurrent use.

func (*HostSampler) Sample

func (h *HostSampler) Sample() Host

Sample reads the machine now.

type Mem

type Mem struct {
	Total, Avail, SwapTotal, SwapFree uint64
}

Mem is /proc/meminfo's totals in bytes (0 when missing).

func ParseMeminfo

func ParseMeminfo(s string) Mem

ParseMeminfo reads MemTotal, MemAvailable, SwapTotal and SwapFree.

func (Mem) SwapUsed

func (m Mem) SwapUsed() uint64

SwapUsed is SwapTotal minus SwapFree.

func (Mem) Used

func (m Mem) Used() uint64

Used is Total minus Avail, the memory the system could not hand to a new program without swapping.

type ProcSampler

type ProcSampler struct {
	R   Reader
	PID string
	// contains filtered or unexported fields
}

ProcSampler samples one process (PID "self" when empty). Safe for concurrent use.

func (*ProcSampler) Sample

func (p *ProcSampler) Sample() Process

Sample reads the process now; CPUPct is over the time since the previous Sample (zero on the first).

type ProcStat

type ProcStat struct {
	// CPUTicks is utime plus stime, in USER_HZ ticks: the same unit as CPUTimes, so the two can be divided.
	CPUTicks uint64
	Threads  uint32
}

ProcStat is what /proc/<pid>/stat says about a process.

func ParseProcStat

func ParseProcStat(s string) (ProcStat, bool)

ParseProcStat reads /proc/<pid>/stat. The command name (field 2) is in parentheses and may hold spaces and ")", so the fields are counted after the last ")".

type Process

type Process struct {
	// CPUPct is the process's CPU time over the interval in percent of the whole machine (all CPUs), so it can be set
	// against the host's Busy: 25 on a 4-CPU machine is one CPU fully used.
	CPUPct  float64
	RSS     uint64
	Threads uint32
	// Open file descriptors (sockets are among them) and the soft limit (0 = not known).
	FDs, FDLimit uint64
}

Process is one sample of a process.

type Reader

type Reader struct {
	Proc, Sys string
}

Reader reads under a /proc and a /sys root. The zero value reads the real ones.

func (Reader) Conntrack

func (r Reader) Conntrack() (count, max uint64)

Conntrack returns the connection-tracking table's entries and its limit (0, 0 where netfilter's conntrack is not loaded). When the table is full the kernel drops new connections ("nf_conntrack: table full, dropping packet"), which looks to people like a node that stopped answering.

func (Reader) NICSpeedMbps

func (r Reader) NICSpeedMbps(iface string) uint32

NICSpeedMbps is the link speed of an interface in Mbit/s, 0 when the driver does not say (most virtual NICs).

func (Reader) Sockets

func (r Reader) Sockets() Sockets

Sockets returns the IPv4 and IPv6 socket counts added together.

type Sockets

type Sockets struct {
	// TCP sockets in use (established and listening), in TIME_WAIT, and orphaned (closed by the program, not yet by the peer).
	TCP, TCPTimeWait, TCPOrphan uint32
	UDP                         uint32
}

Sockets are the counts of /proc/net/sockstat and sockstat6 added together.

func ParseSockstat

func ParseSockstat(s string) Sockets

ParseSockstat reads "TCP: inuse N orphan N tw N ..." and "UDP: inuse N ..." (sockstat), or "TCP6: inuse N" and "UDP6: inuse N" (sockstat6). Call it on each file and Add the results.

func (Sockets) Add

func (s Sockets) Add(o Sockets) Sockets

Add sums two counts (IPv4 and IPv6).

Jump to

Keyboard shortcuts

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