Help us shape the future of Go! Take the 2026 Go Developer Survey

sysmon

package
v0.37.1 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package sysmon reads host resource figures for bentoo-tray: memory from /proc/meminfo, pressure stall information from /proc/pressure, free disk space and running emerge processes. It returns data and wrapped errors only; it never logs and applies no thresholds, which are the caller's policy.

Index

Constants

View Source
const (
	DefaultEmergeMaxRead = 4 << 20
	DefaultEmergeMaxLine = 64 << 10
)

Default bounds of an EmergeFollower read.

Variables

View Source
var ErrNoPressure = errors.New("pressure stall information unavailable")

ErrNoPressure reports that the kernel exposes no pressure stall information file for a resource. The error returned with it also wraps the underlying fs.ErrNotExist; a pressure file that exists but fails to read or parse is a different error.

Functions

This section is empty.

Types

type Disk

type Disk struct {
	Path       string // the watched path as configured
	Dev        uint64 // st_dev of Path, the key that tells filesystems apart
	Size, Free uint64 // bytes; Free = Bavail * Bsize
}

Disk is the space of the filesystem holding a watched path.

type EmergeEvent

type EmergeEvent struct {
	Kind   EmergeEventKind
	Atom   string
	Status int
}

EmergeEvent is one recognised emerge.log line. Atom is set for Started and Completed ("category/package-version"), Status for ExitFailed.

func ParseEmergeLine

func ParseEmergeLine(line []byte) (EmergeEvent, bool)

ParseEmergeLine reads one emerge.log line, without its newline. ok is false for every line that is not one of the four events, including an event line whose atom is not a plain category/package-version.

type EmergeEventKind

type EmergeEventKind int

EmergeEventKind is what one emerge.log line reports.

const (
	EmergeStarted    EmergeEventKind = iota + 1 // ">>> emerge (i of n) <atom> to <root>"
	EmergeCompleted                             // "::: completed emerge (i of n) <atom> to <root>"
	EmergeExitOK                                // "*** exiting successfully."
	EmergeExitFailed                            // "*** exiting unsuccessfully with status '<n>'."
)

The emerge.log events a failed-build check needs.

type EmergeFollower

type EmergeFollower struct {
	Path    string
	MaxRead int
	MaxLine int
	// contains filtered or unexported fields
}

EmergeFollower reads the lines appended to an emerge log since Start. It survives rotation by truncation (the size drops below the offset) and by replacement (the inode changes): either restarts at the new beginning. MaxRead bounds the bytes one Read consumes and MaxLine the length of a line; a longer line is discarded whole. Zero bounds take the defaults. It returns errors and never logs; it is not safe for concurrent use.

func (*EmergeFollower) Read

func (f *EmergeFollower) Read() ([]EmergeEvent, error)

Read returns the events of the complete lines appended since the last Read, in order. A follower whose Start failed starts now, at the end of the log, and returns no event, so lines written before the log became readable are never reported. A log that cannot be read is an error naming the path; when it comes back as a new file it is read from its beginning.

func (*EmergeFollower) Start

func (f *EmergeFollower) Start() error

Start positions the follower at the current end of the log, so only lines appended afterwards are read.

type Memory

type Memory struct {
	Total, Available, SwapTotal, SwapFree uint64
}

Memory is a /proc/meminfo reading, every field in bytes.

func ParseMeminfo

func ParseMeminfo(b []byte) (Memory, error)

ParseMeminfo reads MemTotal, MemAvailable, SwapTotal and SwapFree from the text of /proc/meminfo. Keys are matched whole, so SwapCached or MemFree is never read in place of a wanted key. MemTotal and MemAvailable are required: a missing MemAvailable is an error, never a fallback to MemFree. The swap keys read as zero when absent.

type Mount

type Mount struct {
	Point    string // the mount point, unescaped
	Source   string // the mounted device or pseudo source, unescaped
	FSType   string
	ReadOnly bool // `ro` in the per-mount options or in the superblock options
}

Mount is one line of the kernel mount table.

func ParseMountinfo

func ParseMountinfo(data []byte) ([]Mount, error)

ParseMountinfo parses a mountinfo table (proc(5)). The fields after the variable-length optional list are found from its " - " separator, never by position. A malformed line makes the whole parse an error naming its line number, so a caller never mistakes a shortened table for unmounted filesystems.

type Pressure

type Pressure struct {
	SomeAvg60, FullAvg60 float64
	HasFull              bool
}

Pressure is the avg60 figures of a /proc/pressure file. HasFull reports whether the file carried a full line at all, so an all-zero full line is told apart from an absent one.

func ParsePressure

func ParsePressure(b []byte) (Pressure, error)

ParsePressure reads the avg60 figures of a /proc/pressure file. The some and full lines are told apart by their first word and avg60 is found by its key, never by position. The some line is required; the full line is optional (older kernels expose none for cpu) and sets HasFull when present.

type Reader

type Reader struct {
	Root   string
	Statfs func(path string, st *syscall.Statfs_t) error
}

Reader reads host figures under Root ("/" in production, a testdata tree in tests). Statfs defaults to syscall.Statfs when nil; tests inject a fake.

func (Reader) Disk

func (r Reader) Disk(path string) (Disk, error)

Disk reports the size and the unprivileged free space of the filesystem holding path. Free counts Bavail, not Bfree: the reserved blocks Bfree includes belong to root. Dev is the path's st_dev rather than the statfs Fsid, which several filesystems report as zero. A block size that is not positive is an error rather than a nonsense figure.

func (Reader) EmergeRunning

func (r Reader) EmergeRunning() (bool, error)

EmergeRunning reports whether any process under Root/proc runs emerge, judged from its cmdline (comm reads python3 for the python-exec wrapper). A process whose cmdline cannot be read, typically because it exited during the scan, is skipped; failing to list Root/proc itself is an error.

func (Reader) Memory

func (r Reader) Memory() (Memory, error)

Memory reads and parses Root/proc/meminfo.

func (Reader) Mounts

func (r Reader) Mounts() ([]Mount, error)

Mounts reads <Root>/proc/self/mountinfo. A table that cannot be read or parsed is an error naming the file; no partial table is returned.

func (Reader) Pressure

func (r Reader) Pressure(resource string) (Pressure, error)

Pressure reads and parses Root/proc/pressure/<resource>. Only "cpu" and "memory" are accepted, so no other name can reach the filesystem. An absent file yields an error wrapping both ErrNoPressure and its cause.

Jump to

Keyboard shortcuts

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