hostmetrics

package
v0.1.21 Latest Latest
Warning

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

Go to latest
Published: Mar 18, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package hostmetrics collects host-level metrics (disk, RAM, CPU load, swap) via /proc reads and syscall.Statfs on Linux, with zero-value stubs on other platforms.

Index

Constants

View Source
const (
	DiskWarnPct      = 70.0
	DiskCritPct      = 85.0
	RAMWarnPct       = 80.0
	RAMCritPct       = 90.0
	SwapWarnPct      = 25.0
	SwapCritPct      = 50.0
	CPULoadWarnRatio = 0.7
	CPULoadCritRatio = 0.9
)

Threshold constants for alert evaluation.

Variables

This section is empty.

Functions

func ParseLoadAvg

func ParseLoadAvg(data []byte) (load1, load5, load15 float64, err error)

ParseLoadAvg parses the contents of /proc/loadavg and returns the 1-minute, 5-minute, and 15-minute load averages. Exported so tests can feed canned data without touching the filesystem.

func ParseMemInfo

func ParseMemInfo(data []byte) (total, available, swapTotal, swapFree uint64, err error)

ParseMemInfo parses the contents of /proc/meminfo and returns MemTotal, MemAvailable, SwapTotal, and SwapFree in bytes. Exported so tests can feed canned data without touching the filesystem.

Types

type Alert

type Alert struct {
	Resource  string     `json:"resource"`
	Level     AlertLevel `json:"level"`
	Message   string     `json:"message"`
	Value     float64    `json:"value"`
	Threshold float64    `json:"threshold"`
}

Alert represents a threshold breach for a monitored resource.

func EvaluateAlerts

func EvaluateAlerts(s Snapshot) []Alert

EvaluateAlerts checks the snapshot against threshold constants and returns any triggered alerts. Exported for testing.

type AlertLevel

type AlertLevel string

AlertLevel classifies the severity of a resource alert.

const (
	// AlertWarn indicates a resource is approaching its limit.
	AlertWarn AlertLevel = "warning"
	// AlertCritical indicates a resource is at or near its limit.
	AlertCritical AlertLevel = "critical"
)

type Collector

type Collector struct {
	// contains filtered or unexported fields
}

Collector gathers host-level metrics on a 30-second interval.

func NewCollector

func NewCollector(rootPath string) *Collector

NewCollector creates a Collector that reads disk stats from rootPath.

func (*Collector) History

func (c *Collector) History(since time.Time) []Snapshot

History returns snapshots collected since the given time, oldest first.

func (*Collector) Run

func (c *Collector) Run(ctx context.Context) error

Run starts the background collection loop. It blocks until ctx is cancelled.

func (*Collector) Snapshot

func (c *Collector) Snapshot() Snapshot

Snapshot returns the most recent metrics snapshot (thread-safe).

type Snapshot

type Snapshot struct {
	CollectedAt time.Time `json:"collected_at"`

	// Disk
	DiskTotalBytes uint64  `json:"disk_total_bytes"`
	DiskUsedBytes  uint64  `json:"disk_used_bytes"`
	DiskPercent    float64 `json:"disk_percent"`

	// RAM
	RAMTotalBytes uint64  `json:"ram_total_bytes"`
	RAMUsedBytes  uint64  `json:"ram_used_bytes"`
	RAMAvailBytes uint64  `json:"ram_avail_bytes"`
	RAMPercent    float64 `json:"ram_percent"`

	// Swap
	SwapTotalBytes uint64  `json:"swap_total_bytes"`
	SwapUsedBytes  uint64  `json:"swap_used_bytes"`
	SwapPercent    float64 `json:"swap_percent"`

	// CPU
	LoadAvg1  float64 `json:"load_avg_1"`
	LoadAvg5  float64 `json:"load_avg_5"`
	LoadAvg15 float64 `json:"load_avg_15"`
	NumCPU    int     `json:"num_cpu"`

	// Alerts
	Alerts []Alert `json:"alerts,omitempty"`
}

Snapshot captures host-level metrics at a point in time.

Jump to

Keyboard shortcuts

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